SlideShare a Scribd company logo
1 of 57
Introduction to Trees
Linear Lists and Trees
• Linear lists (arrays, linked list)are useful for serially ordered
data
– (e1,e2,e3,…,en)
– Days of week
– Months in a year
– Students in a class
• Trees are useful for hierarchically ordered data
– directory structure
– Parenthetical Listing
– family trees:
• all descendants of a particular person
• all ancestors born after year 1800 of a particular person
Parenthetical Listing
A (B (C D) E F (G H I) )
 Tree
 A tree is a collection of Nodes
 and a finite set of directed lines called branches, that connect the nodes.
 Each node can have 0 or more children
 A node can have at most one parent
 Tree defined recursively
 A tree consists of a distinguished node r, called the root, and zero or more
non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by
a directed edge from r.
 A tree is a collection of N nodes, one of which is the root and N-1 edges.
 The number of branches associated with a node is the degree of the
node.
 Binary tree
 Tree with 0–2 children per node
 Terminology
 Root  no parent
 Leaf  no child
 Interior  non-leaf
 Height  distance from root to leaf
 The root of each sub tree is said to be a child of r and r is
said to be the parent of each sub tree root.
 Siblings  nodes with the same parent
• Depth (of node): the length of the unique path from the root to a
node.
• Depth (of tree): The depth of a tree is equal to the depth of its
deepest leaf.
• Height (of node): the length of the longest path from a node to a
leaf.
– All leaves have a height of 0
– The height of the root is equal to the depth of the tree
• When the branch is directed toward the node, it is indegree branch.
• When the branch is directed away from the node, it is an outdegree
branch.
• The sum of the indegree and outdegree branches is the degree of the
node.
Recursive definition of a tree
• A tree is a set of nodes that either:
- is empty or
- has a designated node, called the root, from
which hierarchically descend zero or more sub
trees, which are also trees.
9
Binary trees
• A binary tree is a tree in which no node can
have more than two subtrees; the maximum
outdegree for a node is two.
• In other words, a node can have zero, one, or
two subtrees.
• These subtrees are designated as the left
subtree and the right subtree.
Picture of a binary tree
a
b c
d e
g h i
l
f
j k
Tree traversals
Preorder traversal
• In preorder, the root is visited first
• Here’s a preorder traversal to print out all
the elements in the binary tree:
A
B C
D E F G
A B D E C F G
Example1
Example2
Inorder traversal
• In inorder, the root is visited in the middle
• Here’s an inorder traversal to print out all
the elements in the binary tree:
Example1
Example1
Example2
Postorder traversal
• In Postorder, the root is visited last
• Here’s a Postorder traversal to print out all
the elements in the binary tree:
Example1
Example1
Example2
Arithmetic Expression Tree
• Binary tree for an arithmetic expression
– internal nodes: operators
– leaves: operands
• Example: arithmetic expression tree for the expression
((2  (5 - 1)) + (3  2))
+

-2
5 1
3 2
Write down prefix, Infix & Postfix expression
Write down prefix, Infix & Postfix expression
Binary Search Trees
• Binary Search Tree Property:
– the value stored at the root of a sub tree is greater than any value in its
left sub tree and less than any value in its right sub tree!
• Where is the smallest element?
– leftmost element
• Where is the largest element?
– rightmost element
• How to build & maintain binary trees?
– Insertion
– Deletion
• Maintain key property (invariant)
– Smaller values in left sub tree
– Larger values in right sub tree
Binary Search Trees
• Key property
– Value at node
• Smaller values in left subtree
• Larger values in right subtree
– Example
• X > Y
• X < Z
Y
X
Z
Binary Search Trees
• Examples
Binary search
trees
Not a binary
search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Binary Search Tree – Insertion
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left sub tree for Y
4. If X > Y, insert new leaf X as new right sub tree for Y
• Observations
– O( log(n) ) operation for balanced tree
Example Insertion
• Insert ( 20 )
Binary Search Tree – Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left sub tree
OR smallest value Z on right sub tree
b) Delete replacement value (Y or Z) from sub tree
Observation
- O( log(n) ) operation for balanced tree
Example Deletion (Leaf)
• Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10
with largest
value in left sub
tree
Replacing 5 with
largest value in
left sub tree
Deleting leaf
Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10
with smallest
value in right
sub tree
Deleting leaf Resulting tree
Iterative Search of Binary Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key) // Found it
return n;
if (n->data > key) // In left subtree
n = n->left;
else // In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
Recursive Search of Binary Tree
Node *Find( Node *n, int key) {
if (n == NULL) // Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key) // In left subtree
return Find( n->left, key );
else // In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
Example Binary Searches
• Find ( root, 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
root
Example Binary Searches
• Find (root, 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
Types of Binary Trees
• Degenerate – only one child
• Balanced – “mostly” two children
• Complete – always two children
Degenerate
binary tree
Balanced
binary tree
Complete
binary tree
Introduction to Graph
• What is a graph?
– A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes to
each other
• The set of edges describes relationships among
the vertices
• A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs
• When the edges in a graph have no direction,
the graph is called undirected
Directed vs. undirected graphs (cont.)
• When the edges in a graph have a direction,
the graph is called directed (or digraph)
Warning: if the graph is directed, the order of the vertices in each edge is
important !!
Trees vs graphs
Trees are special cases of graphs!!
Graph terminology
• Adjacent nodes: two nodes are adjacent if they are connected
by an edge
– 5 is adjacent to 7
– 7 is adjacent from 5
• Path: a sequence of vertices that connect two nodes in a
graph
• Complete graph: a graph in which every
vertex is directly connected to
every other vertex
Graph terminology
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
Graph terminology
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
Graph terminology
• Weighted graph: a graph in which each edge
carries a value
Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to represent
the edges
Tree

More Related Content

What's hot

Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data StructuresAmar Jukuntla
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2Rajendran
 
Data Structures - Lecture 2 [Introduction to Data Structures]
Data Structures - Lecture 2 [Introduction to Data Structures]Data Structures - Lecture 2 [Introduction to Data Structures]
Data Structures - Lecture 2 [Introduction to Data Structures]Muhammad Hammad Waseem
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree dsViji B
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graphRai University
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structuresnayanbanik
 
Data structures - unit 1
Data structures - unit 1Data structures - unit 1
Data structures - unit 1SaranyaP45
 
Search tree & graph
Search tree & graphSearch tree & graph
Search tree & graphMichael Jo
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechniclavparmar007
 
Linear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueLinear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueSelvaraj Seerangan
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)FariaFerdowsy
 
Files and data storage
Files and data storageFiles and data storage
Files and data storageZaid Shabbir
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.Education Front
 

What's hot (20)

A binary tree
A binary tree A binary tree
A binary tree
 
Trees
TreesTrees
Trees
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2
 
Data Structures - Lecture 2 [Introduction to Data Structures]
Data Structures - Lecture 2 [Introduction to Data Structures]Data Structures - Lecture 2 [Introduction to Data Structures]
Data Structures - Lecture 2 [Introduction to Data Structures]
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
 
Red black trees
Red black treesRed black trees
Red black trees
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Data structures - unit 1
Data structures - unit 1Data structures - unit 1
Data structures - unit 1
 
Search tree & graph
Search tree & graphSearch tree & graph
Search tree & graph
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechnic
 
Types Of Data Structure
Types Of Data StructureTypes Of Data Structure
Types Of Data Structure
 
Linear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueLinear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and Queue
 
Advanced Trees
Advanced TreesAdvanced Trees
Advanced Trees
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)
 
Files and data storage
Files and data storageFiles and data storage
Files and data storage
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
 

Similar to Tree

Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphRai University
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10sumitbardhan
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.pptSuneel61
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptxRahulAI
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap treezia eagle
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it islogesswarisrinivasan
 

Similar to Tree (20)

Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Tree
TreeTree
Tree
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it is
 

Recently uploaded

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

Tree

  • 2. Linear Lists and Trees • Linear lists (arrays, linked list)are useful for serially ordered data – (e1,e2,e3,…,en) – Days of week – Months in a year – Students in a class • Trees are useful for hierarchically ordered data – directory structure – Parenthetical Listing – family trees: • all descendants of a particular person • all ancestors born after year 1800 of a particular person
  • 3. Parenthetical Listing A (B (C D) E F (G H I) )
  • 4.  Tree  A tree is a collection of Nodes  and a finite set of directed lines called branches, that connect the nodes.  Each node can have 0 or more children  A node can have at most one parent  Tree defined recursively  A tree consists of a distinguished node r, called the root, and zero or more non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by a directed edge from r.  A tree is a collection of N nodes, one of which is the root and N-1 edges.  The number of branches associated with a node is the degree of the node.  Binary tree  Tree with 0–2 children per node
  • 5.  Terminology  Root  no parent  Leaf  no child  Interior  non-leaf  Height  distance from root to leaf  The root of each sub tree is said to be a child of r and r is said to be the parent of each sub tree root.  Siblings  nodes with the same parent
  • 6. • Depth (of node): the length of the unique path from the root to a node. • Depth (of tree): The depth of a tree is equal to the depth of its deepest leaf. • Height (of node): the length of the longest path from a node to a leaf. – All leaves have a height of 0 – The height of the root is equal to the depth of the tree • When the branch is directed toward the node, it is indegree branch. • When the branch is directed away from the node, it is an outdegree branch. • The sum of the indegree and outdegree branches is the degree of the node.
  • 7.
  • 8.
  • 9. Recursive definition of a tree • A tree is a set of nodes that either: - is empty or - has a designated node, called the root, from which hierarchically descend zero or more sub trees, which are also trees. 9
  • 10. Binary trees • A binary tree is a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two. • In other words, a node can have zero, one, or two subtrees. • These subtrees are designated as the left subtree and the right subtree.
  • 11.
  • 12.
  • 13.
  • 14. Picture of a binary tree a b c d e g h i l f j k
  • 16. Preorder traversal • In preorder, the root is visited first • Here’s a preorder traversal to print out all the elements in the binary tree: A B C D E F G A B D E C F G
  • 18.
  • 20.
  • 21. Inorder traversal • In inorder, the root is visited in the middle • Here’s an inorder traversal to print out all the elements in the binary tree:
  • 25.
  • 26. Postorder traversal • In Postorder, the root is visited last • Here’s a Postorder traversal to print out all the elements in the binary tree:
  • 30.
  • 31. Arithmetic Expression Tree • Binary tree for an arithmetic expression – internal nodes: operators – leaves: operands • Example: arithmetic expression tree for the expression ((2  (5 - 1)) + (3  2)) +  -2 5 1 3 2
  • 32. Write down prefix, Infix & Postfix expression
  • 33. Write down prefix, Infix & Postfix expression
  • 34. Binary Search Trees • Binary Search Tree Property: – the value stored at the root of a sub tree is greater than any value in its left sub tree and less than any value in its right sub tree! • Where is the smallest element? – leftmost element • Where is the largest element? – rightmost element • How to build & maintain binary trees? – Insertion – Deletion • Maintain key property (invariant) – Smaller values in left sub tree – Larger values in right sub tree
  • 35. Binary Search Trees • Key property – Value at node • Smaller values in left subtree • Larger values in right subtree – Example • X > Y • X < Z Y X Z
  • 36. Binary Search Trees • Examples Binary search trees Not a binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45
  • 37. Binary Search Tree – Insertion • Algorithm 1. Perform search for value X 2. Search will end at node Y (if X not in tree) 3. If X < Y, insert new leaf X as new left sub tree for Y 4. If X > Y, insert new leaf X as new right sub tree for Y • Observations – O( log(n) ) operation for balanced tree
  • 39. Binary Search Tree – Deletion Algorithm 1. Perform search for value X 2. If X is a leaf, delete X 3. Else // must delete internal node a) Replace with largest value Y on left sub tree OR smallest value Z on right sub tree b) Delete replacement value (Y or Z) from sub tree Observation - O( log(n) ) operation for balanced tree
  • 40. Example Deletion (Leaf) • Delete ( 25 ) 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, delete 5 10 30 2 45
  • 41. Example Deletion (Internal Node) • Delete ( 10 ) 5 10 30 2 25 45 5 5 30 2 25 45 2 5 30 2 25 45 Replacing 10 with largest value in left sub tree Replacing 5 with largest value in left sub tree Deleting leaf
  • 42. Example Deletion (Internal Node) • Delete ( 10 ) 5 10 30 2 25 45 5 25 30 2 25 45 5 25 30 2 45 Replacing 10 with smallest value in right sub tree Deleting leaf Resulting tree
  • 43. Iterative Search of Binary Tree Node *Find( Node *n, int key) { while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; } Node * n = Find( root, 5);
  • 44. Recursive Search of Binary Tree Node *Find( Node *n, int key) { if (n == NULL) // Not found return( n ); else if (n->data == key) // Found it return( n ); else if (n->data > key) // In left subtree return Find( n->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5);
  • 45. Example Binary Searches • Find ( root, 2 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found root
  • 46. Example Binary Searches • Find (root, 25 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found
  • 47. Types of Binary Trees • Degenerate – only one child • Balanced – “mostly” two children • Complete – always two children Degenerate binary tree Balanced binary tree Complete binary tree
  • 48. Introduction to Graph • What is a graph? – A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other • The set of edges describes relationships among the vertices • A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)
  • 49. Directed vs. undirected graphs • When the edges in a graph have no direction, the graph is called undirected
  • 50. Directed vs. undirected graphs (cont.) • When the edges in a graph have a direction, the graph is called directed (or digraph) Warning: if the graph is directed, the order of the vertices in each edge is important !!
  • 51. Trees vs graphs Trees are special cases of graphs!!
  • 52. Graph terminology • Adjacent nodes: two nodes are adjacent if they are connected by an edge – 5 is adjacent to 7 – 7 is adjacent from 5 • Path: a sequence of vertices that connect two nodes in a graph • Complete graph: a graph in which every vertex is directly connected to every other vertex
  • 53. Graph terminology • What is the number of edges in a complete directed graph with N vertices? N * (N-1)
  • 54. Graph terminology • What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2
  • 55. Graph terminology • Weighted graph: a graph in which each edge carries a value
  • 56. Graph implementation • Array-based implementation – A 1D array is used to represent the vertices – A 2D array (adjacency matrix) is used to represent the edges