SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Lesson 4
Data Structure
Arrays
• Built-in in most programming languages
• Two kinds (programmer responsibility):
• Unordered: attendance tracking
• Ordered: high scorers
• Operations:
• Insertion
• Deletion
• Search
List
• The List models a sequence of positions
storing arbitrary objects
• It establishes a before/after relation
between positions
• Generic methods:
• size(), isEmpty()
• Accessor methods:
• first(), last()
• prev(p), next(p)
• Update methods:
• replace(p, e)
• insertBefore(p, e),
• insertAfter(p, e),
• insertFirst(e),
• insertLast(e)
• remove(p)
Singly Linked Lists
• A singly linked list is a concrete data structure
consisting of a sequence of nodes
• Each node stores
• element
• link to the next node
Inserting at the Head
1.Allocate a new node
2.Insert new element
3.Make new node point to
old head
4.Update head to point to
new node
Removing at the Head
1.Update head to point to
next node in the list
2.Disallocate the former
first node
Doubly Linked List
• A doubly linked list is often more convenient!
• Nodes store:
• element
• link to the previous node
• link to the next node
• Special trailer and header nodes
Insertion
• We visualize operation insertAfter(p, X), which returns position q
Deletion
• We visualize remove(p), where p == last()
Stack
• The stack is a very common data structure used in
programs which has lot of potential
• Hold object, usually all of the same type
• Follows the concept of LIFO
Stack
• Main stack operations:
• push(object): inserts an element
• pop(): removes and returns the last inserted
element
• Auxiliary stack operations:
• top(): returns the last inserted element without
removing it
• size(): returns the number of elements stored
• isEmpty(): returns a Boolean value indicating
whether no elements are stored
Stack Example
Operation output stack
push(8) - (8)
push(3) - (3, 8)
pop() 3 (8)
push(2) - (2, 8)
push(5) - (5, 2, 8)
top() 5 (5, 2, 8)
pop() 5 (2, 8)
pop() 2 (8)
pop() 8 ()
pop() error ()
push(9) - (9)
push(1) - (1, 9)
Application of Stack
• Direct applications
★ Page-visited history in a Web browser
★ Undo sequence in a text editor
• Indirect applications
★ Auxiliary data structure for algorithms
★ Component of other data structures
Queue
• The stack is a very common data structure used in
programs which has lot of potential
• Hold object, usually all of the same type
• Follows the concept of FIFO
• Insertions are at the rear of the queue and removals
are at the front of the queue
Queue
• Main queue operations:
• enqueue(o): inserts element o at the end of the queue
• dequeue(): removes and returns the element at the
front of the queue
• Auxiliary queue operations:
• front(): returns the element at the front without removing
it
• size(): returns the number of elements stored
• isEmpty(): returns a Boolean value indicating whether
no elements are stored
Queue Example
Operation output stack
enqueue(5) - (5)
enqueue(3) - (5, 3)
dequeue() 5 (3)
enqueue(7) - (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() error ()
isEmpty() TRUE ()
enqueue(9) - (9)
size() 1 (9)
Application of Queue
• Direct applications
★ Waiting lists
★ Access to shared resources (e.g., printer)
• Indirect applications
★ Auxiliary data structure for algorithms
★ Component of other data structures
Tree
• An abstract model of a
hierarchical structure
• A tree consists of nodes
with a parent-child relation
• Applications:
• Organization charts
• File systems
Tree Terminology
• Root: node without parent (A)
• Internal node: node with at least one child (A, B, C,
F)
• Leaf (aka External node): node without children (E,
I, J, K, G, H, D)
• Ancestors of a node: parent, grandparent, great-
grandparent, etc.
• Depth of a node: number of ancestors
• Height of a tree: maximum depth of any node (3)
• Descendant of a node: child, grandchild, great-
grandchild, etc.
• Subtree: tree consisting of a node and its
descendants
Tree Exercise
• What is the size of the tree (number of nodes)?
• Classify each node of the tree as a root, leaf, or
internal node
• List the ancestors of nodes B, F, G, and A.
Which are the parents?
• List the descendants of nodes B, F, G, and A.
Which are the children?
• List the depths of nodes B, F, G, and A.
• What is the height of the tree?
• Draw the subtrees that are rooted at node F
and at node K.
Tree
• Generic methods:
• integer size()
• boolean isEmpty()
• objectIterator elements()
• positionIterator positions()
• Accessor methods:
• position root()
• position parent(p)
• positionIterator children(p)
• Query methods:
• boolean isInternal(p)
• boolean isLeaf (p)
• boolean isRoot(p)
• Update methods:
• swapElements(p, q)
• object replaceElement(p, o)
Depth and Height
• v : a node of a tree T.
• The depth of v is the number
of ancestors of v, excluding v
itself.
• The height of a node v in a
tree T is defined recursively:
• If v is an external node, then
the height of v is 0
• Otherwise, the height of v is
one plus the maximum
height of a child of v.
Preorder Traversal
• A traversal visits the nodes of a
tree in a systematic manner
• In a preorder traversal, a node is
visited before its descendants
• Application: Table of content
Postorder Traversal
• In a postorder traversal, a node
is visited after its descendants
• Application: compute space
used by files in a directory and
its subdirectories
Data Structure for Trees
• A node is represented by an object
storing
• Element
• Parent node
• Sequence of children nodes
Binary Tree
• A binary tree is a tree with the following
properties:
• Each internal node has two children
• The children of a node are an ordered pair
• We call the children of an internal node left
child and right child
• Applications:
• arithmetic expressions
• decision processes
• searching
Binary Tree
• The BinaryTree extends the Tree, i.e., it inherits all the
methods of the Tree
• Update methods may be defined by data structures
implementing the BinaryTree
• Additional methods:
• position leftChild(p)
• position rightChild(p)
• position sibling(p)
Arithmetic Expression Tree
• Binary tree associated with an arithmetic expression
• internal nodes: operators
• leaves: operands
• Example: arithmetic expression tree for the expression (2 × (a − 1) + (3 × b))
Decision Tree
• Binary tree associated with a decision process
• internal nodes: questions with yes/no answer
• leaves: decisions
• Example: shooting (robots playing football)
Properties of Binary Trees
Inorder Traversal
• In an inorder traversal, a
node is visited after its left
subtree and before its right
subtree
Inorder Traversal –
Application
• Application: draw a binary tree.
• Assign x- and y-coordinates to node v, where
• x(v) = inorder rank of v
• y(v) = depth of v
Exercise
• Draw a (single) binary tree T, such that
• Each internal node of T stores a single character
• A preorder traversal of T yields EXAMFUN
• An inorder traversal of T yields MAFXUEN
Print Arithmetic Expressions
Evaluate Arithmetic
Expressions
Exercise
1.Draw an expression tree that has
• Four leaves, storing the values 1, 5, 6, and 7
• 3 internal nodes, storing operations +, -, *, /(operators can be used more
than once, but each internal node stores only one)
• The value of the root is 21
2. Draw a tree that represents the expression (3*45 + 10*(6-2))-(5/6 + 7*2)
• list all the internal nodes, leaf nodes. What are the height and the size of
the tree?
• list all nodes in the order visited using preorder/postorder/inorder
traversal.
Data Structure for Trees
Data Structure for Binary
Trees

Weitere ähnliche Inhalte

Was ist angesagt?

Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
Arvind Devaraj
 

Was ist angesagt? (20)

Presentation on basics of python
Presentation on basics of pythonPresentation on basics of python
Presentation on basics of python
 
Session 05 cleaning and exploring
Session 05 cleaning and exploringSession 05 cleaning and exploring
Session 05 cleaning and exploring
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
Pandas
PandasPandas
Pandas
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1
 
Fast Exact String Pattern-Matching Algorithm for Fixed Length Patterns
Fast Exact String Pattern-Matching Algorithm for Fixed Length PatternsFast Exact String Pattern-Matching Algorithm for Fixed Length Patterns
Fast Exact String Pattern-Matching Algorithm for Fixed Length Patterns
 
What is data structure
What is data structureWhat is data structure
What is data structure
 
Data engineering and analytics using python
Data engineering and analytics using pythonData engineering and analytics using python
Data engineering and analytics using python
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
R language, an introduction
R language, an introductionR language, an introduction
R language, an introduction
 
R language
R languageR language
R language
 
Data structure
Data structureData structure
Data structure
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
L 15 ct1120
L 15 ct1120L 15 ct1120
L 15 ct1120
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB AcademyR Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academy
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
Session 07 text data.pptx
Session 07 text data.pptxSession 07 text data.pptx
Session 07 text data.pptx
 

Andere mochten auch (8)

Data Structures - Lecture 5 [Stack]
Data Structures - Lecture 5 [Stack]Data Structures - Lecture 5 [Stack]
Data Structures - Lecture 5 [Stack]
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 

Ähnlich wie Hub102 - Lesson4 - Data Structure

Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 
ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
AlliVinay1
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 

Ähnlich wie Hub102 - Lesson4 - Data Structure (20)

210 trees
210 trees210 trees
210 trees
 
210 trees5
210 trees5210 trees5
210 trees5
 
Advanced c c++
Advanced c c++Advanced c c++
Advanced c c++
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
Basic data analysis using R.
Basic data analysis using R.Basic data analysis using R.
Basic data analysis using R.
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdf
 
Data Structures and Algorithms Fundamentals
Data Structures and Algorithms FundamentalsData Structures and Algorithms Fundamentals
Data Structures and Algorithms Fundamentals
 
ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
chap11.ppt
chap11.pptchap11.ppt
chap11.ppt
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Data structures
Data structuresData structures
Data structures
 

Kürzlich hochgeladen

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Kürzlich hochgeladen (20)

Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

Hub102 - Lesson4 - Data Structure

  • 2. Arrays • Built-in in most programming languages • Two kinds (programmer responsibility): • Unordered: attendance tracking • Ordered: high scorers • Operations: • Insertion • Deletion • Search
  • 3. List • The List models a sequence of positions storing arbitrary objects • It establishes a before/after relation between positions • Generic methods: • size(), isEmpty() • Accessor methods: • first(), last() • prev(p), next(p) • Update methods: • replace(p, e) • insertBefore(p, e), • insertAfter(p, e), • insertFirst(e), • insertLast(e) • remove(p)
  • 4. Singly Linked Lists • A singly linked list is a concrete data structure consisting of a sequence of nodes • Each node stores • element • link to the next node
  • 5. Inserting at the Head 1.Allocate a new node 2.Insert new element 3.Make new node point to old head 4.Update head to point to new node
  • 6. Removing at the Head 1.Update head to point to next node in the list 2.Disallocate the former first node
  • 7. Doubly Linked List • A doubly linked list is often more convenient! • Nodes store: • element • link to the previous node • link to the next node • Special trailer and header nodes
  • 8. Insertion • We visualize operation insertAfter(p, X), which returns position q
  • 9. Deletion • We visualize remove(p), where p == last()
  • 10. Stack • The stack is a very common data structure used in programs which has lot of potential • Hold object, usually all of the same type • Follows the concept of LIFO
  • 11. Stack • Main stack operations: • push(object): inserts an element • pop(): removes and returns the last inserted element • Auxiliary stack operations: • top(): returns the last inserted element without removing it • size(): returns the number of elements stored • isEmpty(): returns a Boolean value indicating whether no elements are stored
  • 12. Stack Example Operation output stack push(8) - (8) push(3) - (3, 8) pop() 3 (8) push(2) - (2, 8) push(5) - (5, 2, 8) top() 5 (5, 2, 8) pop() 5 (2, 8) pop() 2 (8) pop() 8 () pop() error () push(9) - (9) push(1) - (1, 9)
  • 13. Application of Stack • Direct applications ★ Page-visited history in a Web browser ★ Undo sequence in a text editor • Indirect applications ★ Auxiliary data structure for algorithms ★ Component of other data structures
  • 14. Queue • The stack is a very common data structure used in programs which has lot of potential • Hold object, usually all of the same type • Follows the concept of FIFO • Insertions are at the rear of the queue and removals are at the front of the queue
  • 15. Queue • Main queue operations: • enqueue(o): inserts element o at the end of the queue • dequeue(): removes and returns the element at the front of the queue • Auxiliary queue operations: • front(): returns the element at the front without removing it • size(): returns the number of elements stored • isEmpty(): returns a Boolean value indicating whether no elements are stored
  • 16. Queue Example Operation output stack enqueue(5) - (5) enqueue(3) - (5, 3) dequeue() 5 (3) enqueue(7) - (3, 7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() error () isEmpty() TRUE () enqueue(9) - (9) size() 1 (9)
  • 17. Application of Queue • Direct applications ★ Waiting lists ★ Access to shared resources (e.g., printer) • Indirect applications ★ Auxiliary data structure for algorithms ★ Component of other data structures
  • 18. Tree • An abstract model of a hierarchical structure • A tree consists of nodes with a parent-child relation • Applications: • Organization charts • File systems
  • 19. Tree Terminology • Root: node without parent (A) • Internal node: node with at least one child (A, B, C, F) • Leaf (aka External node): node without children (E, I, J, K, G, H, D) • Ancestors of a node: parent, grandparent, great- grandparent, etc. • Depth of a node: number of ancestors • Height of a tree: maximum depth of any node (3) • Descendant of a node: child, grandchild, great- grandchild, etc. • Subtree: tree consisting of a node and its descendants
  • 20. Tree Exercise • What is the size of the tree (number of nodes)? • Classify each node of the tree as a root, leaf, or internal node • List the ancestors of nodes B, F, G, and A. Which are the parents? • List the descendants of nodes B, F, G, and A. Which are the children? • List the depths of nodes B, F, G, and A. • What is the height of the tree? • Draw the subtrees that are rooted at node F and at node K.
  • 21. Tree • Generic methods: • integer size() • boolean isEmpty() • objectIterator elements() • positionIterator positions() • Accessor methods: • position root() • position parent(p) • positionIterator children(p) • Query methods: • boolean isInternal(p) • boolean isLeaf (p) • boolean isRoot(p) • Update methods: • swapElements(p, q) • object replaceElement(p, o)
  • 22. Depth and Height • v : a node of a tree T. • The depth of v is the number of ancestors of v, excluding v itself. • The height of a node v in a tree T is defined recursively: • If v is an external node, then the height of v is 0 • Otherwise, the height of v is one plus the maximum height of a child of v.
  • 23. Preorder Traversal • A traversal visits the nodes of a tree in a systematic manner • In a preorder traversal, a node is visited before its descendants • Application: Table of content
  • 24. Postorder Traversal • In a postorder traversal, a node is visited after its descendants • Application: compute space used by files in a directory and its subdirectories
  • 25. Data Structure for Trees • A node is represented by an object storing • Element • Parent node • Sequence of children nodes
  • 26. Binary Tree • A binary tree is a tree with the following properties: • Each internal node has two children • The children of a node are an ordered pair • We call the children of an internal node left child and right child • Applications: • arithmetic expressions • decision processes • searching
  • 27. Binary Tree • The BinaryTree extends the Tree, i.e., it inherits all the methods of the Tree • Update methods may be defined by data structures implementing the BinaryTree • Additional methods: • position leftChild(p) • position rightChild(p) • position sibling(p)
  • 28. Arithmetic Expression Tree • Binary tree associated with an arithmetic expression • internal nodes: operators • leaves: operands • Example: arithmetic expression tree for the expression (2 × (a − 1) + (3 × b))
  • 29. Decision Tree • Binary tree associated with a decision process • internal nodes: questions with yes/no answer • leaves: decisions • Example: shooting (robots playing football)
  • 31. Inorder Traversal • In an inorder traversal, a node is visited after its left subtree and before its right subtree
  • 32. Inorder Traversal – Application • Application: draw a binary tree. • Assign x- and y-coordinates to node v, where • x(v) = inorder rank of v • y(v) = depth of v
  • 33. Exercise • Draw a (single) binary tree T, such that • Each internal node of T stores a single character • A preorder traversal of T yields EXAMFUN • An inorder traversal of T yields MAFXUEN
  • 36. Exercise 1.Draw an expression tree that has • Four leaves, storing the values 1, 5, 6, and 7 • 3 internal nodes, storing operations +, -, *, /(operators can be used more than once, but each internal node stores only one) • The value of the root is 21 2. Draw a tree that represents the expression (3*45 + 10*(6-2))-(5/6 + 7*2) • list all the internal nodes, leaf nodes. What are the height and the size of the tree? • list all nodes in the order visited using preorder/postorder/inorder traversal.
  • 38. Data Structure for Binary Trees