Tree and Tree Traversal
Tree and Tree Traversal
[email protected] 1
What is a Tree?
• A connected acyclic graph is a tree
• Applications:
Organizational charts
File systems
Programming environments
2
What is a Tree?
3
Tree Terminology
Algorithm
depth(T, v)
if T.isRoot(v) then
return 0
else
return 1 + depth(T,
T.parent(v))
• Running time: O(1 + dv), dv is 5
Height
6
Height
Algorithm height(T, v)
if T.isLeaf(v) then
return 0
else
h=0
for each w εT.children(v)
do
h = max(h, height(T, w))
return 1 + h
•A tree is ordered if there is a linear ordering defined for each child of each node.
•A binary tree is an ordered tree in which every node has at most two
children.
•If each node of a tree has either zero or two children, the tree is called a proper
binary tree.
Postorder Traversal
9
Preorder Traversal (General Tree)
10
Preorder Traversal (Binary Tree)
11
Postorder Traversal (General Tree)
12
Postorder Traversal (Binary Tree)
13
Inorder Traversal
14
Inorder Traversal
15
Preorder, Inorder, Postorder Traversals: Example
16
Construct a Binary Tree only from Preorder,
Inorder or Postorder
• Preoder: f →b →g →i →h
17
Construct a Binary Tree from Preorder & Inorder
• Preoder: f → b → a → d → c → e → g → i → h
• Inorder: a → b → c → d → e → f → i → g → h
18
Construct a Binary Tree from Postorder & Inorder
• Postoder: a → c → e → d → b → i → h → g → f
• Inorder: a → b → c → d → e → f → i → g → h
19
Construct a Binary Tree from Preorder & Postorder
• Preoder: f → b → a → d → c → e → g → i → h
• Postoder: a → c → e → d → b → i → h → g → f
• No unique Tree!!
• Need at least one Inorder traversal sequence to construct Unique tree.
20
Level Order Traversal
•In a level order traversal, every node on a level is visited before going to
a lower level
21
Euler Tour Traversal
22
Euler Tour Traversal
23
(Proper) Binary Tree
• A (proper) binary tree is a tree with the following properties:
Each internal node has two children
The children of a node are an ordered pair
Application:
arithmetic expressions
decision processes
searching
24
Arithmetic Expression Tree
25
Decision Tree
26
Linked Structure for Binary Trees
27
Codes for Creation of Binary Trees
o Read a data in x.
o Allocate memory for a new node and
store the address in
pointer p.
o Store the data x in the node p.
o Recursively create the left subtree of p
and make it the left
child of p.
o Recursively create the right subtree of p
and make it the right 28
Codes for Creation of Binary Trees
#include<stdio.h>
typedef struct TreeNode {
struct TreeNode *left;
int data;
struct TreeNode *right;
} TreeNode;
29
Codes for Creation of Binary Trees
TreeNode * createBinaryTree( ){
TreeNode *p;
int x;
printf("Enter data(-1 for no data): ");
scanf("%d", &x);
if(x == -1) return NULL;
//create current node
p = (TreeNode*) malloc(sizeof(TreeNode));
p->data = x;
//recursively create left and right subtree
printf("Enter left child of %d: \n", x);
p->left = createBinaryTree ( );
printf("Enter right child of %d: \n",x);
p->right = createBinaryTree ();
30
return p; }
Codes for Creation of Binary Tree Traversal
void preorder(TreeNode *t) {
if(t != NULL) {
printf("\n%d", t->data);
preorder(t->left);
preorder(t->right);
}
}
int main() {
TreeNode *root;
root = createBinaryTree ( );
preorder(root);
} 31
LinkeSd Structure for General Trees
Parent node
Children Container: Sequence of children
nodes
32
Thank You
33