SlideShare a Scribd company logo
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

A binary tree
A binary tree A binary tree
A binary tree
Asseged Adi
 
Trees
TreesTrees
Trees
Ali Saleem
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
Amar Jukuntla
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2
Rajendran
 
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 ds
Viji 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 graph
Rai University
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
nayanbanik
 
Red black trees
Red black treesRed black trees
Red black trees
TCHAYE Jude
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
Dr.Umadevi V
 
Data structures - unit 1
Data structures - unit 1Data structures - unit 1
Data structures - unit 1
SaranyaP45
 
Search tree & graph
Search tree & graphSearch tree & graph
Search tree & graph
Michael Jo
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechnic
lavparmar007
 
Types Of Data Structure
Types Of Data StructureTypes Of Data Structure
Types Of Data Structure
Vaishali Chinchkhede
 
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
Selvaraj Seerangan
 
Advanced Trees
Advanced TreesAdvanced Trees
Advanced Trees
Selvaraj 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 storage
Zaid 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

Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
AnuradhaJadiya1
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
Farhana Shaikh
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Tree
TreeTree
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
SeethaDinesh
 
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
Rai 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-10
sumitbardhan
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
Suneel61
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
AdityaK92
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
RahulAI
 
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
zia 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 is
logesswarisrinivasan
 

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

Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 

Recently uploaded (20)

Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 

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