CS212 Sep2016 09 Tree Traversals
CS212 Sep2016 09 Tree Traversals
Data Structures
and Algorithms (CS212)
Section 09 :
Trees
Eng. Mahmoud Osama Radwan
[email protected] – AAST CCIT
Tree Traversals
Traverse a Tree means visiting all nodes in the tree
15 Breadth-First
(Level-Order)
10 20
Depth-First :
8 12 17 25 Preorder
Inorder
Postorder
Tree Traversals
Breadth-First: visit level by level
15
10 20 15 10 20 8 12 17 25
8 12 17 25
Tree Traversals
Depth-First 1- Preorder (DLR)
15
<root><left><right>
10 20 2- Inorder (LDR)
<left><root><right>
8 12 17 25
3- Postorder (LRD)
<left><right><root>
Tree Traversals
Depth-First 1- Preorder (DLR)
<root><left><right>
15
15 10 8 12 20 17 25
10 20
// Display using Depth-First (Preorder)
void displayDLR(Node *&root){
if(root == NULL){
return;
8 12 17 25 }
printf("%d ",root->data);
displayDLR(root->left);
displayDLR(root->right);
}
Tree Traversals
Depth-First 2- Inorder (LDR)
<left><root><right>
15
8 10 12 15 17 20 25
10 20
// Display using Depth-First (Inorder)
void displayLDR(Node *&root){
if(root == NULL){
return;
8 12 17 25 }
displayLDR(root->left);
printf("%d ",root->data);
displayLDR(root->right);
}
Tree Traversals
Depth-First 3- Postorder (LRD)
<left><right><root>
15
8 12 10 17 25 20 15
10 20
// Display using Depth-First (Postorder)
void displayLRD(Node *&root){
if(root == NULL){
return;
8 12 17 25 }
displayLRD(root->left);
displayLRD(root->right);
printf("%d ",root->data);
}
Print using Breadth-First
Check if tree is empty
void display(Node *&root){
15 if(root == NULL){
printf("Empty BST");
return;
}
10 20
8 12 17 25
Print using Breadth-First
Create queue to save links
void display(Node *&root){
15 if(root == NULL){
printf("Empty BST");
return;
}
10 20 initialize(q);
8 12 17 25
head
tail
q
Print using Breadth-First
Add the root to the queue
void display(Node *&root){
15 if(root == NULL){
printf("Empty BST");
return;
}
10 20 initialize(q);
Add(q,root);
8 12 17 25
head
tail
15
q
Print using Breadth-First
Print the first element of the queue then If there’s a children add
it to the queue , then remove the first element
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
15 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
15 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
15 10 20 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
10 20 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
10 20 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
10 20 8 12 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
20 8 12 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
20 8 12 17 25 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
8 12 17 25 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20 8
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
8 12 17 25 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20 8 12
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
12 17 25 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20 8 12 17
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
17 25 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20 8 12 17 25
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
25 }
Remove(q,current,empty);
q }
Print using Breadth-First
Output: 15 10 20 8 12 17 25
void display(Node *&root){
if(root == NULL){
printf("Empty BST");
return;
15 }
initialize(q);
Add(q,root);
Remove(q,current,empty);
10 20
while(!empty){
printf(“%d ”,current->data);
if(current->left!=NULL){
8 12 17 25 Add(q,current->left);
}
if(current->right!=NULL){
Add(q,current->right);
head
tail
}
Remove(q,current,empty);
q }
END of Section
Thank You
Eng. Mahmoud Osama Radwan
[email protected] – AAST CCIT