Unit 4 Notes
Unit 4 Notes
4
TREE
Topic Name
Introduction
By,
Prof. Dipika Birari
Tree
Tree : Introduction
In linear data structure like Singly Linked List we can traverse in
forward direction node by node.
But in all these data structures, we cannot jump from a node to any
desired node directly.
Tree : Introduction
DS enhances the flexibility in traversing by providing the data
structure Tree where one node is connected with several other nodes.
Hence we can jump from one node to many other nodes.
Non-Linear
Linear DS DS
Tree
Array
Queue
Graph
Stack
Tree may be defined as a finite set ‘T’ of one or more nodes such
that there is a node designated as the root of the tree and the
other nodes are divided into n> = 0 disjoint sets
T1, T2, T3, T4 …. Tn.
T = { V, E}
RcV
V Vertex
E Edge
R Root
Tree : Example
Advantages of Tree
Trees reflect structural relationships in between the records of data.
Node
Every element in the tree is called as node which contains data and
links to other nodes. A
Here A, B, C, D, E, F, G, H, I, J, and
L are nodes. B C
Root
The node which is placed at the top of the tree is called as root node.
A
Leaf Nodes
The lower nodes which does not have any child nodes are called as
leaf nodes. A
Degree of Node
The total number child nodes of any particular node is called as
degree of that node. A 2
In any tree, ‘Degree’ of a node is
total numbers of children it as. B 3 C
Degree of Tree
Degree of tree is the degree of node/nodes in the tree having
maximum degree. A 2
Level of a node
The generation of node is represented by the term level.
Level 0 A
Depth/Height of a Tree
The maximum level of any leaf node in the tree is called depth or
height of the tree. A
Level 0
Depth of Tree= 3
OR Level 1 B C
Height of Tree=3
Level 2 D E F G H
Level 3 I J K L
Depth is 2 Depth is 3
Basic Terminology
Edge
The connecting link between any two nodes is called as edge.
A
Path
Sequence of edges from one node to another is called as path
between those two nodes. A
D E F G H
I J K L
Basic Terminology
Parent
The node which has child node is called as parent.
A
E.g. A, B, C, E, G and H D E F G H
I J K L
Basic Terminology
Child
The node which has a link from its parent node is known as child
node. A
I J K L
Basic Terminology
Child
The node which has a link from its parent node is known as child
node. A
I J K L
Basic Terminology
Child
The node which has a link from its parent node is known as child
node. A
Ancestor
The ancestors of a node are all the nodes along the path from the root
to that node. A
Here,
B and A Ancestor of E B C
D E F G H
I J K L
Basic Terminology
Ancestor
The ancestors of a node are all the nodes along the path from the root
to that node. A
Here,
B and A Ancestor of E B C
C and A Ancestor of G
D E F G H
I J K L
Basic Terminology
Descendents
Descendents of a node are all such nodes in downward direction
which are reachable from that node.. A
Here,
G, H, K and L descendents of C B C
D E F G H
I J K L
Basic Terminology
Siblings
The nodes which belong to same Parent are known as Siblings.
A
Here,
B and C are siblings. B C
D E F G H
I J K L
Sub Tree
Basic Terminology
General Tree
Binary Tree
r1 r1 r1
Forest
Forest is an arbitrary set of trees.
In other words, we can consider that a general tree as the root of a forest,
and a forest is an ordered combination of zero or more general trees.
P
A X
B C Y Z Q
D E F R
T1 T2 T3
Binary Tree
In a normal or general tree, there may be any number of children to
each node which make it complicated to implement such tree.
Left Child
Right Child
Binary Tree
In a binary tree, each and every node can have either 0, 1 or 2 children
but not more than 2.
Applications of Binary Tree
Manipulate hierarchical data.
Router algorithms
Types of Binary Tree
Binary Tree
Strictly binary tree is also called as Full Binary Tree or Proper Binary
Tree or 2-Tree.
Types of Binary Tree
Strictly Binary Tree
Strictly binary tree is also called as Full Binary Tree or Proper Binary
Tree or 2-Tree.
Types of Binary Tree
Complete Binary Tree
In Binary Tree Every Node have Maximum two child nodes.
Example:
At level 2 2^2=4 nodes
At level 3 2^3=8 nodes
Types of Binary Tree
Complete Binary Tree
A binary tree in which every internal node has exactly
two child nodes and all the leaf nodes are at same level
is called Complete Binary Tree.
Types of Binary Tree
Almost Complete Binary Tree
Here the last level is partially filled.
A binary tree with L levels, having all levels 1 to L-1 are completely
filled without any gaps and last level L is partially filled from left to
right, is as Almost Complete Binary Tree.
Types of Binary Tree
Skewed Binary Tree
The binary tree in which either only left branches are present or
only right branches are present is called as Skewed Binary Tree.
The binary tree in which either only left branches are present or
only right branches are present is called as Skewed Binary Tree.
Types of Binary Tree
Extended Binary Tree
Converted to
Binary Tree Full Binary Tree.
By inserting some dummy nodes
Sequential Representation
(Memory is allocated at compile time)
Linked Representation
(Memory is allocated dynamicaly)
Array Representation
In this,
Binary Tree Sequence in memory with the help
Represented in
of single dimensional array.
D E F G 6
3 4 5
For node B Position 1
Left child node D Position = 2*P+1=(2*1+1)=3
Right child node E Position = 2*P+2=(2*1+2)=4
Array Representation
If any of the nodes in the tree has empty sub trees, the nodes forming
the part of these empty sub trees is also numbered and their values in
the corresponding position in the array is NULL.
0 A
B C
1 2
D E F 6
3 4 5
G H
7 8 9 10 11 12 13 14
Element A B C D E F G H
Position 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Array Representation
An array with maximum size is usually declared for data storage which may
lead to wastage of lot of memory space when there is implementation of
unbalanced trees such as skewed tree.
No. of nodes < max possible no. of nodes for a given height.
A Level of tree = L =3
Size of array= (2^3)-1=8-1=7
B
Element A B - C - - -
Position 0 1 2 3 4 5 6
C
Here, the wastage of lot of memory space de to more NULL values.
This advantage is removed in Linked representation.
Array Representation
Advantages
Very easy to understand.
Best representation for compete and full Binary Tree.
Programming is very easy.
Easy to move from child to it’s parent or vice-versa.
Disadvantages
Lot of memory area is wasted.
Insertion and deletion needs lot of data movement.
Execution time is high.
No suited for trees other than full and complete Binary Tree.
Linked Representation
Linked representation is considered as one of the most common and
important method to represent a binary tree in memory.
The linked representation of a binary tree is implemented with the
help of linked list.
Linked List
pointing to A pointing to
left sub-tree right sub-tree
Info Part
B C
Both left and
Right are Null
pointers and D E F
used to store
address. Null Null
Null Null
G H
Disadvantages
The mechanism is difficult to understand.
Linear Data Structure: array, stacks, queues & linked list provide a
single way to traverse the list.
Inorder Traversal
Preorder Traversal
Postorder Traversal
Binary Tree Traversal: Inorder
Recursive In-order Traversal
1. Traverse the left subtree, i.e. call Inorder (left-subtree)
2. Visit Root
3. Traverse the right subtree, i.e. call Inorder (right-subtree)
Left Right
Root
Binary Tree Traversal: Inorder
Recursive In-order Traversal
Example:
A
1. Start from Root
2. Goto left subtree as deep as possible.
B C
3. Reach to leaf node print it.
4. Backtrack, print node which are visited
D E F G
2nd time.
5. Goto right subtree as deep as possible.
6. Reach to leaf node, print it. H
7. Backtrack.
Binary Tree Traversal: Inorder
Recursive In-order Traversal
Example:
A
1. Start from Root.
4
2. Goto left subtree as deep as possible.
B C 6
3. Reach to leaf node print it.
2
4. Backtrack, print node which are visited
D E F G
2nd time. 8
5. Goto right subtree as deep as possible. 1 3 5
D B E A F C H G
Binary Tree Traversal: Inorder
Recursive In-order Traversal
while(T!=NULL)
{
s.push(T);
T=Tleft;
}
Binary Tree Traversal: Inorder
Non-Recursive In-order Traversal
Step2: If the stack S is empty then traversal is finished.
Else
{ // visit right subtree of the node popped from stack
T=s.pop;
visit(T);
T=Tright;
// start traversing from T, traverse left & continue traversing
left. All nodes traversed are pushed on S.
while(T!=NULL)
{
s.push(T);
T=Tleft;
}
}
Binary Tree Traversal: Inorder
Non-Recursive In-order Traversal
void inorder Non-rec(node * T)
{
stack s;
while(T!=NULL)
{
s.push(T);
T=Tleft;
}
while (!s.Iempty())
{ T=s.pop;
print T->data;
T=Tright;
while(T!=NULL)
{ s.push(T);
T=Tleft;
}
}
}
Binary Tree Traversal: Preorder
Root Right
Left
Binary Tree Traversal: Preorder
A B D E C F G H
Binary Tree Traversal: Preorder
A A + preorder on + preorder on
B C
D G H
= A +(B+ preorder on )+(c + preorder on +preorder on)
G H
E F
= A +(B + D + E + F) + (C + G + H)
A B D E F C G H
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
DGB HEICF
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B HEICF
DG
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B HEICF
DG
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B HEICF
DG
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B HEICF
DG
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B HEICF
DG
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
DG HEI F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
DG HEI F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
DG HEI F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
DG HEI F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
DG HEI F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
G
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
G
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
G
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
G
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
G
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D HEI F
G
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Array Representation
Construct BT for the preorder and inorder traversal sequences.
Preorder A B D G C E H I F
Inorder D G B A H E I C F
B C
D E F
G
H I
Binary Tree Traversal: Postorder
Left Root
Right
Binary Tree Traversal: Postorder
D E B F H G C A
Binary Tree Traversal: Postorder
A binary tree in which the data of all the nodes in the left sub-tree of
the root node is less than the data of the root and the data of all the
nodes in the right sub-tree of the root node is more than the data of
the root, is called as Binary Search Tree.
Binary Search Tree (BST)
Node Structure of BST
Node
Value K
20 8
15 30 6 10
8 17 25 1 7
9
Binary Search Tree (BST)
Example:
20 8
15 30 6 10
8 17 25 1 7
BST
Binary Search Tree (BST)
Example:
20 8
15 30 6 10
8 17 25 1 7
BST
Binary Search Tree (BST)
Example:
20 8
15 30 6 10
8 17 25 1 7
Insert BST
Delete BST
Operations on BST
Create BST
struct node
{
int data;
node * left;
node *right;
}
Operations on BST
Initialize
Initially as there is node i.e. tree is empty, the reference pointer will be
kept as NULL.
node * Initialize ()
{
return NULL;
}
Operations on BST
Search node in BST
Step 1 : Read the data (search element) from user to search.
Step 2 : Compare, the search element with the value of root node in the tree.
Step 3 : If both are equal, then display "element found" and exit from the
function
Step 4 : If both are not equal, then check whether search element is smaller or
larger than that node value.
Step 5 : If search element is smaller, then continue the search process in left
sub-tree.
Step 6 : If search element is larger, then continue the search process in right
sub-tree.
Step 7 : Repeat the same until we found exact element or we completed with a
leaf node.
Step 8 : If we reach to the node with search value, then display "Element found"
and exit from the function.
Step 9 : If we reach to a leaf node and it is also not matching, then display
"Element not found“ and exit from the function.
Operations on BST
Search node in BST:
Recursive
This means most threaded binary trees are also binary search trees.
The idea is to visit all the left children of a node first, then visit the
node itself, and then visit the right children last.
In Order Traversal of In-Order
Threaded Binary Tree
An in-order traversal of any binary tree generally goes as follows :
this algorithm uses stack space proportional to the height of the tree due
to its recursive nature.
If the tree has n nodes, this usage can range anywhere from O(log n) for a
fairly balanced tree, to O(n) to a very unbalanced tree.
Thus each node has both a predecessor and a successor (except for the
first and last nodes, which only have a successor or a predecessor
respectively).
Because 10 is being inserted on the left, and 10 has no children of its own,
we can safely set 10's rightThread to its parent 12.