Unit 4 Tree
Unit 4 Tree
Depth: The depth of a node in the tree is the length of the path from the root to that node.
Height : Height of the tree is equal to the depth of the deepest leaf.
Height of tree is maximum level of any node in a tree
root->child[i]->ch_count
Node Degree
? Tree Degr ee: determined by the node with the highest degree
Tree Degree = 3
Binary Tree
B
C
D E
F
Binary Tree
Binary Tr ee- ordered tree with all nodes having at the most 2 children
R ecursive Definition:
A binary tree is either
1. a empty tree or
2. consists of a node , called a root and two children, left and right, each of which are themselves
O rder ed Tree is one in which the children of each node are ordered
Sample Binary Trees
A A A
B B
A
C
B C
Difference Between a Tree & a Binary Tree
No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a
tree.
The subtrees of abinary tree are ordered; those of a tree are not ordered
Binary Tree for Expressions
Types of Binary Trees
? Full Binary Tree
? Strictly Binary Tree
? Complete Binary Tree
? Almost Complete Binary Tree
? Skewed Binary Tree
Full Binary Tree
• A full binary tree of height has exactly -1 nodes. (root at level 1)
A A
B C B C
G H I
F G
Complete Binary Tree
? A complete binary tree of depth d is the strictly binary tree whose all leaves are at level d.
B
C
? Since all leaf nodes in such tree are at level d, the tree contains 2d leaf nodes and 2 d -1 non leaf
nodes
D E F
G
Almost complete binary tree
• A binary tree of depth d is an almost complete binary tree if:
1. Any node nd at level than d-1 has two children
2. For any node nd in the tree with right descendant at level d, nd must have left child and every left
descendant of nd is either a leaf at level d or has two children.
Skewed Binar y Tree-
A skewed binary tree is a binary tree that satisfies the following 2 properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child.
Or
? Using Array
? In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
? To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n + 1.
Tree Representation: Using Array
Tree Representation: Using Linked List
? To display a binary tree, we need to follow some order in which all the nodes of that binary tree must be displayed.
? In any binary tree, displaying order of nodes depends on the traversal method.
? Depth First Traversal : process all descendents of a child before going on to the next child.
In - Order Traversal
Pre - Order Traversal
Post - Order Traversal
? Breadth First Traversal : each level is completely processed before the next level is started.
Level order
Binary Tree Traversals
? In - Order Traversal : The root node is visited between the left child and right child.
? In this traversal, the left child node is visited first, then the root node is visited and later we go for visiting
the right child node.
? This in-order traversal is applicable for every root node of all subtrees in the tree.
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Recursive Inorder Traversal
c) Go to step 3.
Root/Current 10
8 11
Stack
7 9
Example
current = current->left
Root/Current
8 11
7 9
10
Example
current = current->left
8 11
oot/Current
8
7 9
10
Example
current = current->left
8 11
8
7 9
10
Root/Current = NULL
Example As current==NULL we will pop stack and
print the element.
Then move
10 current = current->right
Output: 7
8 11
7 9
8
Root/Current = NULL
10
Example As new current is also equals to NULL ,
we will again pop stack and print the
element.
7 9
Root/Current = NULL
10
Example As new current is also equals to NULL ,
we will again pop stack and print the
element.
7 9
Root/Current = 9
10
Example Push current onto stack
And current=current->left
10
Output: 7 ,8
8 11
7 9
Root/Current = NULL 9
10
Example As current== Null
Pop stack
10 And current=current->right
Output: 7 ,8,9
8 11
7 9
Root/Current = NULL 9
10
Example As current== Null
Pop stack
10 And current=current->right
Output: 7 ,8,9
8 11
7 9
Root/Current = NULL
10
Example As current== Null
Pop stack
i.e. 11
Output: 7 ,8,9,10
8 11
7 9
Root/Current = NULL
Example As current== Null
Pop stack
i.e. 11
Output: 7 ,8,9,10
8 11
7 9 Root/Current=NULL
Example Push current on stack.
current=current->left child
10
Output: 7 ,8,9,10
8 11
Root/Current
7 9
11
Example As current== Null
Pop stack
10
Output: 7 ,8,9,10,11
8 11
7 9
Root/Current=NULL
Binary Tree Traversals
? Pre - Order Traversal : The root node is visited before the left child and right child.
? In this traversal, the root node is visited first, then the left node is visited and later we go for visiting the
right child node.
? This pre-order traversal is applicable for every root node of all subtrees in the tree.
Algorithm Preorder(tree)
1. Visit the root.
2.Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Recursive Preorder Traversal
? Post - Order Traversal : The root node is visited after the left and right child node..
? In this traversal, the left child node is visited first, then the right child node is visited and later we go for
visiting the root node.
? This post-order traversal is applicable for every root node of all subtrees in the tree.
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
Recursive Postorder Traversal
End if
Non Recursive Postorder Traversal
1.1 Create an empty stack
2.1 Do while root is not NULL
a)if root->right!=Null
Push root's right child.
b) then push root to stack
c) Set root as root's left child.
2.2. Pop an item from stack and set it as root
while root != NULL
a) If the popped item has a right child and
if the right child is at top of stack,
then remove the right child from stack,
push the root back and set root as root's right child.
else push the root to stack and set root to right
b) Else print root's data and set root as NULL.
Pop an item from stack and set it as root
2.3 Repeat steps 2.1 and 2.2 while stack is not empty.
1.1 Non Recursive Postorder Traversal
Create an empty stack
2.1 Repeat steps 2.2 and 2.3 while stack is not empty.
2.2 Do while root is not NULL
a)if root->right!=Null
Push root's right child.
b) then push root to stack
c) Set root as root's left child.
2.3 Pop an item from stack and set it as root
Print root
while root != NULL
a) if (node->left && node->right ==null)
pop
If the popped item has a right child and if the right child is at top of
stack,
then remove the right child from stack,
push the root back and set root as root's right child.
else push the root to stack and set root to right
Break;
Print inorder, Preorder and Postorder traversal for given tree.
ghdiebJfca
Binary Tree Construction
• Inoder: B C A E D G H F I
• Preoder: A B C D E F G H I
B,C D, E, F, G, H, I
B D, E, F, G, H, I
C
Binary Tree Construction
• Inoder: B C A E D G H F I
• Preoder: A B C D E F G H I
B D,
C E F, G, H, I
Binary Tree Construction
• Inoder: B C A E D G H F I
• Preoder: A B C D E F G H I
B D,
C E F
I
G,H
Binary Tree Construction
• Infix: B C A E D G H F I
• Prefix: A B C D E F G H I
B D,
C E F
I
G
H
Binary Search Tree
? Definition: A binary tree where every node's left sub-tree has values less than the node's value, and every right
sub-tree has values greater than the node's value
Key
? Insertion
? Deletion
? Traversals
? Search
BST Operations - Insertion
? Algorithm:
1. Read the value for which node to be created, and store it in node called New.
end SearchBST
BST Operations : Deletion