Tree
Tree
Tree
A tree is a simple, acyclic and connected graph.
A tree is a finite set of one or more nodes such that there is a specially designated node
called the root. The remaining nodes are partitioned into 𝑛 ≥ 0 disjoint sets 𝑇1 , 𝑇2 . . . , 𝑇𝑛 ,
where each of these sets is a tree. 𝑇1 , 𝑇2 . . . , 𝑇𝑛 are the sub-trees of the root.
A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the
top of the hierarchy. Each node can have at most one link coming into it. The node
where the link originates is called the parent node. The root node has no parent. The
links leaving a node (any number of links are allowed) point to child nodes. Trees are
recursive structures. Each child node is itself the root of a sub-tree. At the bottom of the
tree are leaf nodes, which have no children.
Advantages of trees:
Tree terminology:
1. Root: In a tree, the first node is called as Root Node. Every tree must have root
node. We can say that root node is the origin of tree data structure. In any tree,
there must be only one root node. In above tree, a is a Root node.
2. Edge: In a tree, the connecting link between any two nodes is called as edge. A
tree with 'N' number of nodes has N-1 number of edges.
Chinmayee Pati
2
3. Parent: A node that directly precedes a node is called a parent node. In a tree
each node has exactly one parent except the root.
4. Child: One or more nodes that directly follow a node are called child nodes. In a
tree, any parent node can have any number of child nodes. In a tree, all the nodes
except root are child nodes.
5. Siblings: In a tree, the nodes that have the same parent are said to be siblings.
6. Leaf: In a tree, nodes of degree zero are called leaf nodes or external nodes. In
simple words, a leaf is a node with no child.
7. Internal nodes: A node in a tree that has one or more child nodes is called an
internal node. A non-leaf node is called an internal node.
8. Degree: In a tree, the total number of children of a node is called as degree of
that Node. In simple words, the Degree of a node is total number of children it
has.
9. Degree of Tree: The highest degree of a node among all the nodes in a tree is
called as Degree of Tree.
10. Path: In a tree, a path is a sequence of nodes, (𝑎0 , 𝑎1 , 𝑎2 , . . . , 𝑎𝑛 ) where each node
in this sequence is a child of the previous node in the sequence, i.e., 𝑎𝑘+1 is a
child of 𝑎𝑘 . Length of a Path is total number of edges in that path. In the above
tree, the path 𝑎 → 𝑐 → 𝑖 → 𝑝 has length 3.
11. Level: In a tree, the root node is said to be at Level 0 and the children of root
node are at Level 1 and the children of the nodes which are at Level 1 will be at
Level 2 and so on.
12. Depth: In a tree, the length of the path from root node to a particular node is
called as depth of that node. In a tree, depth of the root node is '0'. The depth of a
tree is the maximum level of any leaf in the tree.
13. Height: In a tree, the height of a node is the length of the longest path from that
node to a leaf. Thus all leaves are at height 0. The height of a tree is the height of
the root. The height of an empty tree (a tree containing no nodes) is –1.
14. Ancestor and descendant:
In a tree if a path exists from node A to node B then,
i) A is said to be an ancestor of B and
ii) B is said to be a descendant of A.
The strict ancestors of a node are all ancestors of a node other than the node
itself. Similarly, the strict descendants of a node are all descendants of a node
other than the node itself.
15. Sub Tree: A sub-tree of a tree is a tree consisting a node of the tree and all of its
descendants in the tree
2. A node of degree n is a tree if it has n children, all of which are trees and none of
which have overlapping descendants.
Binary Tree:
A tree in which every node can have a maximum of two children is called as Binary Tree.
In a binary tree, every node can have either 0 children or 1 child or 2 children but not
more than 2 children.
Proof:
Proof:
Proof:
1. Strictly Binary Tree: A binary tree in which every node has either two or zero
number of children is called Strictly Binary Tree. Strictly binary tree is also called
as Full Binary Tree or Proper Binary Tree.
2. Complete Binary Tree: A binary tree in which every internal node has exactly
two children and all leaf nodes are at same level is called Complete Binary Tree.
Complete binary tree is also called as Perfect Binary Tree.
3. Almost Complete Binary tree: An almost complete binary tree is a binary tree
that satisfies the following 2 properties-
i) All the levels are completely filled except possibly the last level.
Chinmayee Pati
5
ii) The last level must be strictly filled from left to right.
5. Balanced Binary Tree: A balanced binary tree is also known as height balanced
tree. A height balanced binary tree is a binary tree in which the height of the left
and right sub-tree of any node differs by not more than 1.
1. Array representation
2. Linked representation.
Chinmayee Pati
6
Array representation:
In array representation of binary tree, we use a one dimensional array (1-D Array) to
represent a binary tree. To represent a binary tree T of depth ’d’ using array
representation, we need one dimensional array with a maximum size of 2𝑑 − 1. If the
tree T is a complete binary tree, its nodes are represented sequentially and this method
wastes no space. This method uses a single linear array T as follows:
Example:
𝑇[ ] A B C D E F G H I J K L
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Example:
𝑇[ ] a b c d e f g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Chinmayee Pati
7
Linked Representation:
1. Left Child (LChild): It holds address of the left child node of the parent node.
2. Information of the Node (Info): It holds information of every node.
3. Right Child (RChild): It holds address of the right node of the parent node.
If any sub-tree is empty then the corresponding pointer’s LChild and RChild will store a
NULL value.
Node
LChild Info RChild
Example:
A tree traversal is a method of visiting every node in the tree. It is a way in which each
node in the tree is visited exactly once in a systematic manner. There are three standard
ways of traversing a binary tree. They are:
Preorder Traversal:
In a preorder traversal, each root node is visited before its left and right subtrees are
traversed. Preorder search is also called backtracking.
Chinmayee Pati
8
if (root != NULL)
preorder(root → lchild);
preorder(root →rchild);
Inorder Traversal:
In the case of inorder traversal, the root of each subtree is visited after its left subtree
has been traversed but before the traversal of its right subtree begins.
if (root != NULL)
inorder(root → lchild);
inorder(root → rchild);
Chinmayee Pati
9
Postorder Traversal:
In a postorder traversal, each root is visited after its left and right subtrees have been
traversed.
if (root != NULL)
postorder(root → rchild);
In a level order traversal, the nodes are visited level by level starting from the root, and
going from left to right. The level order traversal requires a queue data structure. So, it
is not possible to develop a recursive procedure to traverse the binary tree in level
order. This is nothing but a breadth first search technique.
Algorithm:
void levelorder()
int j;
if(tree[j] != NULL)
Chinmayee Pati
10
Example:
Example:
Preorder Traversal: 𝑎, 𝑏, 𝑒, 𝑗, 𝑐, 𝑓, 𝑔, ℎ
Inorder Traversal: 𝑏, 𝑗, 𝑒, 𝑎, 𝑓, 𝑐, 𝑔, ℎ
Postorder Traversal: 𝑗, 𝑒, 𝑏, 𝑓, ℎ, 𝑔, 𝑐, 𝑎
Chinmayee Pati
11
A binary tree can be constructed if any of the following two traversals are given:
Preorder and inorder
Postorder and inorder
Construct a binary tree from the following Preorder and inorder sequence.
Preorder: 𝐴𝐵𝐷𝐸𝐻𝐶𝐹𝐺𝐼𝐾𝐽
Inorder: 𝐷𝐵𝐻𝐸𝐴𝐹𝐶𝐾𝐼𝐺𝐽
DBHE FCKIGJ
D HE
Chinmayee Pati
12
DBHE
B C FCKIGJ
HE
KIGJ
D E F G
KI
I J
H
Postorder: 𝐺𝐷𝐵𝐻𝐼𝐸𝐹𝐶𝐴
Inorder: 𝐷𝐺𝐵𝐴𝐻𝐸𝐼𝐶𝐹
DGB HEICF
B HEICF
Chinmayee Pati
DG
13
B C
D E F
G H I
The operations performed on binary tree can also be applied to Binary Search Tree with
following three operations:
Inserting a node
Searching a node
Deleting a node
Inserting a node:
Chinmayee Pati
14
A BST is constructed by the repeated insertion of new nodes to the tree structure.
Inserting a node in to a tree is achieved by performing two separate operations.
Example:
Algorithm:
NEWNODE is a pointer variable to hold the address of the newly created node. DATA is
the information to be pushed.
Chinmayee Pati
15
3. If (ROOT == NULL)
ROOT=NEW NODE
4. Else if (DATA < ROOT → Info)
a) ROOT = ROOT → Lchild
b) GoTo Step 4
5. Else If (DATA > ROOT → Info)
a) ROOT = ROOT → Rchild
b) GoTo Step 4
6. If (DATA < ROOT → Info)
ROOT → LChild = NEWNODE
7. Else If (DATA > ROOT → Info)
ROOT → RChild = NEWNODE
8. Else
a) Display (“DUPLICATE NODE”)
b) EXIT
9. NEW NODE → Info = DATA
10. NEW NODE → LChild = NULL
11. NEW NODE → RChild = NULL
12. EXIT
Searching a node:
Algorithm:
1. Input the DATA to be searched and assign the address of the root node to ROOT.
2. If (DATA == ROOT → Info)
a) Display “The DATA exist in the tree”
b) GoTo Step 6
3. If (ROOT == NULL)
a) Display “The DATA does not exist”
b) GoTo Step 6
4. If(DATA > ROOT→Info)
a) ROOT = ROOT→RChild
b) GoTo Step 2
5. If(DATA < ROOT→Info)
a) ROOT = ROOT→Lchild
b) GoTo Step 2
6. Exit
Deleting a node:
To delete a DATA of information from a binary search tree, first search and locate the
node to be deleted. Then any one of the following conditions arises:
Chinmayee Pati
16
2. The node has exactly one child (or one sub tree, left or right sub tree): If N has one
child, check whether it is a right or left child. If it is a right child, then find the
smallest element from the corresponding right sub tree. Then replace the
smallest node information with the deleted node. If N has a left child, find the
largest element from the corresponding left sub tree. Then replace the largest
node information with the deleted node.
3. The node has two children (or two sub tress, left and right sub tree): The same
process is repeated if N has two children, i.e., left and right child. The node which
is to be deleted is replaced with its in-order successor or predecessor recursively
until the node value (to be deleted) is placed on the leaf of the tree. After the
procedure, replace the node with NULL and free the allocated space.
Example:
Chinmayee Pati
17
Algorithm:
NODE is the current position of the tree, which is in under consideration. LOC is the
place where node is to be replaced. DATA is the information of node to be deleted.
A balanced binary tree is also known as height balanced tree. A height balanced binary
tree is a binary tree in which the heights of the left and right sub-tree of any node differ
by not more than 1. (Example: AVL Tree)
AVL Tree:
An AVL tree is a binary search tree in which the left and right sub tree of any node may
differ in height by at most 1, and in which both the sub trees are themselves AVL Trees.
Each node in the AVL Tree possesses any one of the following properties:
Chinmayee Pati
18
1. A node is called left heavy, if the largest path in its left sub tree is one level larger
than the largest path of its right sub tree.
2. A node is called right heavy, if the largest path in its right sub tree is one level
larger than the largest path of its left sub tree.
3. The node is called balanced, if the largest paths in both the right and left sub
trees are equal.
Note: Every AVL Tree is a binary search tree but every Binary Search Tree need not be
AVL tree.
Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
Chinmayee Pati
19
Classification of Rotation:
Rotations
Left Right Rotation (LR Rotation)
Double Rotation
Right Left Rotation (RL Rotation)
In LL Rotation, every node moves one position to left from the current position.
In RR Rotation, every node moves one position to right from the current position.
Chinmayee Pati
20
The LR Rotation is a sequence of single left rotation followed by a single right rotation.
In LR Rotation, at first, every node moves one position to the left and one position to
right from the current position.
The RL Rotation is sequence of single right rotation followed by single left rotation. In
RL Rotation, at first every node moves one position to right and one position to left from
the current position.
In AVL Tree, a new node is always inserted as a leaf node. It is performed in the same
way as it is performed in a binary search tree. However, it may lead to violation in the
AVL tree property and therefore the tree may need balancing.
Algorithm:
1. Insert the new element into the tree using Binary Search Tree insertion logic.
2. After insertion, check the Balance Factor of every node.
Chinmayee Pati
21
Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may
disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced
in order to maintain the AVLness.
Algorithm:
1. Delete a node from the tree using Binary Search Tree deletion logic.
2. After deletion, check the Balance Factor of every node.
3. If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
4. If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to
be imbalanced. In this case, perform suitable Rotation to make it balanced and go
for next operation.
An m-way search tree is a tree in which, for some integer m called the order of the tree,
each node has at most m children. If 𝑘 ≤ 𝑚 is the number of children, then the node
contains exactly 𝑘 − 1 keys, which partition all the keys into k subsets consisting of all
the keys less than the first key in the node, all the keys between a pair of keys in the
node, and all keys greater than the largest key in the node.
B-Tree:
Chinmayee Pati
22
Algorithm:
1. Traverse the B Tree in order to find the appropriate leaf node at which the node
can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then
a) Insert the new element in the increasing order of elements.
b) Split the node into the two nodes at the median.
Chinmayee Pati
23
Deletion is also performed at the leaf nodes. The node which is to be deleted can either
be a leaf node or an internal node.
Algorithm:
If the the node which is to be deleted is an internal node, then replace the node with its
in-order successor or predecessor. Since, successor or predecessor will always be on the
leaf node hence, the process will be similar as the node is being deleted from the leaf
node.
Chinmayee Pati