0% found this document useful (0 votes)
41 views160 pages

Unit3 Trees

The document provides an overview of trees as a non-linear data structure, detailing their terminology, properties, and various types such as binary trees, binary search trees, and expression trees. It explains tree traversal methods (preorder, inorder, postorder) and includes algorithms for constructing trees from given traversals. Additionally, it presents functions in C for counting nodes, leaf nodes, and non-leaf nodes in a binary tree.

Uploaded by

vidit6163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views160 pages

Unit3 Trees

The document provides an overview of trees as a non-linear data structure, detailing their terminology, properties, and various types such as binary trees, binary search trees, and expression trees. It explains tree traversal methods (preorder, inorder, postorder) and includes algorithms for constructing trees from given traversals. Additionally, it presents functions in C for counting nodes, leaf nodes, and non-leaf nodes in a binary tree.

Uploaded by

vidit6163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

DATA STRUCTURES AND ALGORITHMS

Code: CSPC-213
By
Dr Kunwar Pal

Department of Computer Science and Engineering


Dr. B.R. Ambedkar National Institute of Technology
1
Jalandhar Punjab-144011
Trees
What is a Tree
• Tree is non-linear Data
Structure. Computers”R”U
s
• A tree is a finite nonempty set
of elements.
• It is an abstract model of a Sale Manufacturin R&
hierarchical structure. s g D
• Consists of nodes with a
parent-child relation. U Internationa Laptop Desktop
S l s s
• Applications:
– Organization charts
Europ Asi Canad
– File systems e a a
– Programming
environments

05/23/2025 2
Trees
Tree Terminology
• Root: node without parent (A)
• Siblings: nodes share the same parent
• Subtree: tree consisting of a
• Degree of a Node: Number of subtrees of a node node and its descendants
in a given tree.
• Degree of Tree: It is the maximum degree of a
node in a tree
• Internal: node with at least one child (A, B, C, A
F)
• External or terminal or leaf node: node without
children (E, I, J, K, G, H, D) or degree 0 B C D
• Ancestors of a node: parent, grandparent, grand-
grandparent, etc.
• Descendant of a node: child, grandchild, grand- E F G H
grandchild, etc.
• Edge: It is a connecting line between two nodes:
like (A,B) or (B,F) etc. I J K
• Path: It is a sequence of consecutive edges from subtree
the source node to destination node. In the
example tree the path from A to K is:
05/23/2025
(A,B), (B,F) & (F,K) or A,B,F,K 3
Trees
Tree Terminology
Depth of the tree- no. of edges from root to leaf Level 0
node in max path.
Height of a tree: no. of edges from leaf node to
root node in max path.
Depth of the node – no. of edges from root node to
that node. Level 1
Height of the node – no. of edges from leaf node
to that node.
Level: The entire tree structure is levelled in such a
way that the root node is always at level 0. Then its Level 2
immediate children are at level 1 and their
immediate children are at level 2 and so on. In
general if a node is at level n then its children will
be at n+1 level.
Non-Terminal Nodes: Any node (except the Level 3
root node) whose degree is not zero is called non-
terminal node.

05/23/2025 4
Trees
Tree Properties
Property Value
A
Number of nodes
Height
B C Root Node
Leaves
Interior nodes
D E F Ancestors of H
Descendants of B
Siblings of E
G Right subtree of A
Degree of this tree

H I

05/23/2025 5
Trees
Types of Tree

The tree data structure can be classified into six different categories.

05/23/2025 6
Trees
General Tree
General Tree stores the elements in a hierarchical order in which the top
level element is always present at level 0 as the root element. All the
nodes except the root node are present at number of levels. The nodes
which are present on the same level are called siblings while the nodes
which are present on the different levels exhibit the parent-child
relationship among them. A node may contain any number of sub-trees.

Forests
Forest can be defined as the set of disjoint trees which can be obtained
by deleting the root node and the edges which connects root node to the
first level node.

05/23/2025 7
Trees
Binary Tree
Binary tree is a data structure in which each node can have at most 2
children. The node present at the top most level is called the root
node. A node with the 0 children is called leaf node. Binary Trees are
used in the applications like expression evaluation and many more.

Binary Search Tree


Binary search tree is an ordered binary tree. All the elements in the left
sub-tree are less than the root while elements present in the right
sub-tree are greater than or equal to the root node element.
Binary search trees are used in most of the applications of computer
science domain like searching, sorting, etc.

Expression Tree
Expression trees are used to evaluate the simple arithmetic expressions.
Expression tree is basically a binary tree where internal nodes are
represented by operators while the leaf nodes are represented
by operands.

05/23/2025 8
Trees

05/23/2025 9
Trees
Tournament Tree
Tournament tree are used to record the winner of the match in each
round being played between two players. Tournament tree can also be
called as selection tree or winner tree. External nodes represent the
players among which a match is being played while the internal nodes
represent the winner of the match played. At the top most level, the
winner of the tournament is present as the root node of the tree.

05/23/2025 10
Trees
Binary Tree
A tree whose elements have at most 2 children is called a
binary tree. Since each element in a binary tree can have
only 2 children, we typically name them the left and right
child.

A Binary Tree node contains following parts.


1.Data
2.Pointer to left child
3.Pointer to right child

05/23/2025 11
Trees
Binary Tree(Properties)
1) The maximum number of nodes at level ‘L’ of a binary tree is 2 L.
Here level is the number of nodes on the path from the root to the node
(including root and node). Level of the root is 0.
This can be proved by induction.
For root, L = 0, number of nodes = 20 = 1
Assume that the maximum number of nodes on level ‘L’ is 2 L
Since in Binary tree every node has at most 2 children, next level would have
twice nodes, i.e. 2 * 2L = 2L+1 for L+1 level
2. The Maximum number of nodes in a binary tree of height ‘h’ is 2 h+1 – 1.
3. In a Binary Tree with N nodes, minimum possible height or the minimum
number of levels is? log2(N+1) -1

05/23/2025 12
Trees

Types of Binary Tree

1. Complete Binary Tree


2. Full Binary Tree or Strictly Binary Tree
3. Balanced Binary Tree
4. A degenerate (or pathological) tree

05/23/2025 13
Trees
Full Binary Tree or Strictly Binary Tree: A Binary
Tree is a full binary tree if every node has 0 or 2
children. The following are the examples of a full
binary tree. We can also say a full binary tree is a
binary tree in which all nodes except leaf nodes have
two children.

In a Full Binary Tree, The number of leaf nodes is the


number of internal nodes plus 1
L=I+1
Where L = Number of leaf nodes, I = Number of
internal nodes

05/23/2025 14
Trees
FULL Binary Tree properties

Theorem: Let T be a nonempty, full binary tree Then:

(a) If T has I internal nodes, the number of leaves is L = I + 1.

(b) If T has I internal nodes, the total number of nodes is N = 2I + 1.

(c) If T has a total of N nodes, the number of internal nodes is I = (N – 1)/2.

(d) If T has a total of N nodes, the number of leaves is L = (N + 1)/2.

(e) If T has L leaves, the total number of nodes is N = 2L – 1.

(f) If T has L leaves, the number of internal nodes is I = L – 1.

05/23/2025 15
Trees
Complete Binary Tree: A Binary Tree is a complete Binary Tree if all the levels
are completely filled except possibly the last level and the last level has all keys as
left as possible

The following are examples of Complete Binary Trees

05/23/2025 16
Trees
Perfect Binary Tree: A Binary tree is a Perfect
Binary Tree in which all the internal nodes have two
children and all leaf nodes are at the same level.
The following are the examples of Perfect Binary
Trees.

A Perfect Binary Tree of height h (where the height of


the binary tree is the longest path from the root node
to any leaf node in the tree) has 2h+1 – 1 node.

05/23/2025 17
Trees
Balanced Binary Tree

A binary tree is balanced if the height of the tree is O(Log n) where n is the
number of nodes. For Example, the AVL tree maintains O(Log n) height by
making sure that the difference between the heights of the left and right subtrees is
almost 1. Red-Black trees maintain O(Log n) height by making sure that the
number of Black nodes on every root to leaf paths is the same and there are no
adjacent red nodes. Balanced Binary Search trees are performance-wise good as
they provide O(log n) time for search, insert and delete

05/23/2025 18
Trees
A degenerate (or pathological) tree: A Tree where every internal node has one
child. Such trees are performance-wise same as linked list.

05/23/2025 19
Trees

Binary Tree Representations

A binary tree data structure is represented using two methods. Those methods
are as follows...

Array Representation
Linked List Representation

05/23/2025 20
Trees

1. Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array
(1-D Array) to represent a binary tree.

05/23/2025 21
Trees
2. Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a
double linked list, every node consists of three fields. First field
for storing left child address, second for storing actual data
and third for storing right child address.

05/23/2025 22
Trees

05/23/2025 23
Trees
Binary Tree Traversal
– Preorder (Node,Left,Right)
– Postorder (Left, Right, Node)
– Inorder(Left, Node, Right)

05/23/2025 24
Trees

(a) Inorder (Left, Root, Right) : 4 2 5 1 3


(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1

05/23/2025 25
Trees

PreOrder : (NLR)

Ans – 1 7 2 6 3 8 5 9 4

InOrder: (LNR)

Ans – 2 7 3 6 8 1 5 4 9

PostOrder: (LRN)

Ans - 2 3 8 6 7 4 9 5 1

05/23/2025 26
05/23/2025 27
Traversal Algorithms
• In-order Traversal:
void inorder(node *p){
if(p!=NULL){
inorder(p->left);
printf(“%d\n”, p->num);
inorder(p->right);
}
} Post-order Traversal:
void postorder(node *p){
if(p!=NULL){

Pre-order Traversal: postorder(p->left);


void preorder(node *p){ postorder(p->right);
if(p!=NULL){ printf(“%d\n”, p->num);
}
printf(“%d\n”, p->num);
}
preorder(p->left);
preorder(p->right);
}
}
05/23/2025 28
Trees
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)

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)

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.

05/23/2025 29
Trees

Construct Tree from given Inorder and Postorder traversals

1. Pickup last key of post order sequence and create node of each.

2. Create left subtree of root node recursively by selecting keys which are
preceding the root node in Inorder.

3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder

05/23/2025 30
Trees

Construct Tree from given Inorder and Postorder traversals

Inorder: 1 2 3 4 5 7 8
Postorder: 1 3 4 2 7 8 5

05/23/2025 31
Trees

Construct Tree from given Inorder and Preorder traversals

1. Pickup first key of pre order sequence and create node of each.

2. Create left subtree of root node recursively by selecting keys which are
preceding the root node in Inorder.

3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder

05/23/2025 32
Trees

Construct Tree from given Inorder and Preorder traversals

Inorder: 1 2 3 4 5 7 8
Preorder: 5 2 1 4 3 8 7

Inorder: 1 2 3 4 5 6 7 8
Preorder: 5 3 1 2 4 6 8 7

05/23/2025 33
Create Tree
• Preorder: A B D G C E H I F
• Inorder: D G B A H E I C F

05/23/2025 34
Trees

1. Write the C function to count number of nodes in a binary tree.

int countnodes(struct node *r)


{
if(r==NULL)
return 0;
return countnodes(rleft)+ countnodes(rright)+1;
}

05/23/2025 35
Trees

2. Write the C function to count number of leaf nodes in a binary tree.

int countleafnode(struct node *r)


{
if(r==NULL)
return 0;
if(rleft==NULL&&rright==NULL)
return 1;

return countleafnode(rleft)+ countleafnode(rright);


}

05/23/2025 36
Trees

3. Write the C function to count number of non-leaf nodes in a binary tree.

int countleafnode(struct node *r)


{
if(r==NULL)
return 0;
if(rleft==NULL&&rright==NULL)
return 0;

return countleafnode(rleft)+ countleafnode(rright)+1;


}

05/23/2025 37
Trees

4. Write the C function to count height of the binary tree.

int height(struct node *r)


{
if(r==NULL)
return 0;
lh = height(rleft);
rh = height(rright);
if(lh>=rh)
return lh+1;
else
return rh+1;
}

05/23/2025 38
Trees

Binary Search Tree Representation


Binary Search tree exhibits a special behavior.

A node's left child must have a value less than its parent's value and the
node's right child must have a value greater than its parent value.

05/23/2025 39
Trees
Binary Search Tree Representation
Post Order Traversal of BST is 10,9,23,22,27,25,15,95,60,40,29.
Find PreOrder traversal.

05/23/2025 40
Trees

BST
Tree Node
The code to write a tree node would be similar to what is
given below. It has a data part and references to its left and
right child nodes.

struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

05/23/2025 41
Trees
BST Basic Operations
The basic operations that can be performed on a binary search tree data
structure, are the following −

•Insert − Inserts an element in a tree/create a tree.

•Search − Searches an element in a tree.

•Preorder Traversal − Traverses a tree in a pre-order manner.

•Inorder Traversal − Traverses a tree in an in-order manner.

•Postorder Traversal − Traverses a tree in a post-order manner.


We shall learn creating (inserting into) a tree structure and searching a
data item in a tree and learn about tree traversing methods in the coming
chapter.
05/23/2025 42
Insertion in BST
Insert function is used to add a new element in a binary search tree at
appropriate location. Insert function is to be designed in such a way
that, it must not violate the property of binary search tree at each value.
Following are the steps to insert a node.
• Allocate the memory for tree.
• Set the data part to the value and set the left and right pointer of tree,
point to NULL.
• If the item to be inserted, will be the first element of the tree, then
the left and right of this node will point to NULL.
• Else, check if the item is less than the root element of the tree, if this
is true, then recursively perform this operation with the left of the
root.
• If this is false, then perform this operation recursively with the right
sub-tree of the root.

05/23/2025 43
Trees
Insertion in BST

struct node* insert(struct node* node, int key)


{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}

05/23/2025 44
05/23/2025 45
Creation of Binary Search Tree
• Create a Binary Search Tree for following keys
14,10,17,12,10,11,20,12,18,25,20,8,22,11,23

05/23/2025 46
Creation of Binary Search Tree
• Create a Binary Search Tree for following keys
S,T,P,Q,M,N,O,R,K,V,A,B

05/23/2025 47
Trees

BST Insertion Operation


1. To insert element in degenerate tree, it requires order of n times.

2. To insert an element in Complete Binary search tree in the worst case, the no.
comparison required are bounded by O(log2n).

05/23/2025 48
Trees

BST Search Operation

struct node* search(struct node* root, int key)


{
// Base Cases: root is null or key is present at root
if (root == NULL || root->key == key)
return root;

// Key is greater than root's key


if (root->key < key)
return search(root->right, key);

// Key is smaller than root's key


return search(root->left, key);
}

05/23/2025 49
Trees

There are three ways which we use to traverse a tree

•In-order Traversal
•Pre-order Traversal
•Post-order Traversal

05/23/2025 50
Trees

In-order Traversal
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

We start from A, and following in-order traversal, we move to its left


subtree B. B is also traversed in-order. The process goes on until all the nodes
are visited. The output of inorder traversal of this tree will be −
D→B→E→A→F→C→G
05/23/2025 51
Trees
Pre-order Traversal
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

We start from A, and following pre-order traversal, we first visit A itself


and then move to its left subtree B. B is also traversed pre-order. The
process goes on until all the nodes are visited. The output of pre-order
traversal of this tree will be −
A→B→D→E→C→F→G
05/23/2025 52
Trees

Post-order Traversal

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the
nodes are visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A
05/23/2025 53
Trees
Binary Search Tree Deletion
Case 1 : Deleting the leaf node from BST

Task 1 : Finding key takes either log2(n) or O(n) times.


Task 2 : Identify leaf node parent pointer which is pointing to it and
make it NULL.

05/23/2025 54
Trees
Binary Search Tree Deletion
Case 2 : Deleting the node which has only one child

Task 1 : Finding key takes either log2(n) or O(n) times.


Task 2 : Copy the child to the node and delete the child

05/23/2025 55
Trees
Binary Search Tree Deletion
Case 3 : Deleting the node which has left subtree or right subtree.
Task 1 : Finding key takes either log2(n) or O(n) times.
Task 2 : Replace the deleted node with the max of LST or min of RST
i.e the node which is to be deleted, is replaced with its in-order
successor

05/23/2025 56
Trees

Binary Search Tree Representation


Construct all possible Binary Search tree by inserting following keys in any order.

10, 20,30

05/23/2025 57
Trees

AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in
1962. The tree is named AVL in honour of its inventors.

AVL Tree can be defined as height balanced binary search tree


in which each node is associated with a balance factor which
is calculated by subtracting the height of its right sub-tree
from that of its left sub-tree.

Tree is said to be balanced if balance factor of each node is in


between -1 to 1, otherwise, the tree will be unbalanced and
need to be balanced.

BalanceFactor = height(left-subtree) − height(right-subtree)

05/23/2025 58
Trees

AVL Tree

Balance Factor = height(left-subtree) − height(right-subtree)

05/23/2025 59
Trees

05/23/2025 60
Trees

AVL Rotations

We perform rotation in AVL tree only in case if Balance Factor is other


than -1, 0, and 1. There are basically four types of rotations which are as
follows:

1.L L rotation: Inserted node is in the left subtree of left subtree of A


2.R R rotation : Inserted node is in the right subtree of right subtree of A
3.L R rotation : Inserted node is in the right subtree of left subtree of A
4.R L rotation : Inserted node is in the left subtree of right subtree of A
Where node A is the node whose balance Factor is other than -1, 0, 1.

The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations.

05/23/2025 61
Trees
RR Rotation
If a tree becomes unbalanced, when a node is inserted into the right
subtree of the right subtree, then we perform a single left rotation −

In our example, node A has become unbalanced as a node is


inserted in the right subtree of A's right subtree. We perform the left
rotation by making A the left-subtree of B.

05/23/2025 62
Trees

LL Rotation
AVL tree may become unbalanced, if a node is inserted in the left
subtree of the left subtree. The tree then needs a right rotation.

As depicted, the unbalanced node becomes the right child of its left
child by performing a right rotation.

05/23/2025 63
Trees
LR Rotation ()
State Action

A node has been inserted into the right subtree of the


left subtree. This makes C an unbalanced node.
These scenarios cause AVL tree to perform left-right
rotation.

We first perform the left rotation on the left subtree


of C. This makes A, the left subtree of B.

05/23/2025 64
Trees

Node C is still unbalanced,


however now, it is because of the
left-subtree of the left-subtree.

We shall now right-rotate the tree,


making B the new root node of this
subtree. C now becomes the right
subtree of its own left subtree.

The tree is now balanced.

05/23/2025 65
Trees

RL Rotation
The second type of double rotation is Right-Left Rotation. It is a
combination of right rotation followed by left rotation.
State Action

A node has been inserted into the left subtree of the


right subtree. This makes A, an unbalanced node
with balance factor 2.

First, we perform the right rotation along C node,


making C the right subtree of its own left subtree B.
Now, B becomes the right subtree of A.

05/23/2025 66
Trees

Node A is still unbalanced because of the


right subtree of its right subtree and
requires a left rotation.

A left rotation is performed by


making B the new root node of the
subtree. A becomes the left subtree of its
right subtree B.

The tree is now balanced.

05/23/2025 67
Trees
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L

1. Insert H, I, J

05/23/2025 68
Trees
On inserting the above 2. Insert B, A
elements, especially in the case
of H, the BST becomes
unbalanced as the Balance
Factor of H is -2. Since the BST is
right-skewed, we will perform RR
Rotation on node H.
The resultant balance tree
is:

05/23/2025 69
Trees

On inserting the above elements, especially in case of A, the BST


becomes unbalanced as the Balance Factor of H and I is 2, we consider
the first node from the last inserted node i.e. H. Since the BST from H is
left-skewed, we will perform LL Rotation on node H.
The resultant balance tree is:

05/23/2025 70
Trees

3. Insert E On inserting E,

BST becomes unbalanced as the


Balance Factor of I is 2, since if we
travel from E to I we find that it is
inserted in the left subtree of right
subtree of I,

we will perform LR Rotation on node I.


LR = RR + LL rotation

05/23/2025 71
Trees
3 a) We first perform RR rotation on node B
The resultant tree after RR rotation is:

05/23/2025 72
Trees

3b) We first perform LL rotation on the node I


The resultant balanced tree after LL rotation is:

05/23/2025 73
Trees

4. Insert C, F, D

On inserting C, F, D, BST becomes


unbalanced as the Balance Factor
of B and H is -2, since if we travel
from D to B we find that it is
inserted in the right subtree of
left subtree of B,

we will perform RL Rotation on


node I. RL = LL + RR rotation.

05/23/2025 Megha Gupta KCS 301: DS Unit 3 74


Trees

4a) We first perform LL rotation on node E


The resultant tree after LL rotation is:

05/23/2025 75
Trees
4b) We then perform RR rotation on node B
The resultant balanced tree after RR rotation is:

05/23/2025 76
Trees
5. Insert G

On inserting G, BST become unbalanced as the Balance Factor of H is 2,


since if we travel from G to H, we find that it is inserted in the left subtree
of right subtree of H, we will perform LR Rotation on node I. LR = RR + LL
rotation.

05/23/2025 77
Trees

5 a) We first perform RR rotation on node C


The resultant tree after RR rotation is:

05/23/2025 78
Trees

5 b) We then perform LL rotation on node H


The resultant balanced tree after LL rotation is:

05/23/2025 79
Trees

6. Insert K

On inserting K, BST becomes unbalanced as the Balance Factor of I is -2.


Since the BST is right-skewed from I to K, hence we will perform RR
Rotation on the node I.

05/23/2025 80
Trees

The resultant balanced tree after RR rotation is:

05/23/2025 81
Trees

7. Insert L
On inserting the L tree is still balanced as the Balance Factor of each
node is now either, -1, 0, +1. Hence the tree is a Balanced AVL tree

05/23/2025 82
Trees

Deletion in AVL Tree


Deleting a node from an AVL tree is similar to that in a binary search
tree.
Case1: Deleting node is Leaf Node
1. Delete that node
2. Check balance factor and perform rotation if found any critical node
Case 2: Deleting node has one child
3. Replace the deleted node with its child
4. Check balance factor and perform rotation if found any critical node
Case 3: Deleting node has two children
5. Find the largest node (x) in left subtree or find the smallest node (y) in right subtree
and replace deleted node either by x or y.
6. Check balance factor and perform rotation if found any critical node

Rotations
There are two types of rotations L rotation (RR) and R rotation (LL). Here, we
will discuss R rotations. L rotations are the mirror images of them.

If the node which is to be deleted is present in the left sub-tree of the critical node,
then L rotation needs to be applied else if, the node which is to be deleted is present
in the right sub-tree of the critical node, the R rotation will be applied.

05/23/2025 83
Trees
Let us consider that, A is the critical node and B is the root node of its
left sub-tree. If node X, present in the right sub-tree of A, is to be
deleted, then there can be three different situations:

R0 rotation (Node B has balance factor 0 )

If the node B has 0 balance factor, and the balance factor of node A
disturbed upon deleting the node X, then the tree will be rebalanced
by rotating tree using R0 rotation.

The critical node A is moved to its right and the node B becomes the
root of the tree with T1 as its left sub-tree. The sub-trees T2 and T3
becomes the left and right sub-tree of the node A. the process involved
in R0 rotation is shown in the following image.

05/23/2025 84
Trees
Example:
Delete the node 30 from the AVL tree shown in the following image.

Solution
In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image. The node
B(10) becomes the root, while the node A is moved to its right. The right
child of node B will now become the left child of node A.

05/23/2025 85
Trees
R1 Rotation (Node B has balance factor 1)

R1 Rotation is to be performed if the balance factor of Node B is 1. In


R1 rotation, the critical node A is moved to its right having sub-trees T2
and T3 as its left and right child respectively. T1 is to be placed as the
left sub-tree of the node B.
The process involved in R1 rotation is shown in the following image.

05/23/2025 86
Trees

Example
Delete Node 55 from the AVL tree shown in the following image.

Deleting 55 from the AVL Tree disturbs the balance factor of the node 50
i.e. node A which becomes the critical node. This is the condition of R1
rotation in which, the node A will be moved to its right (shown in the
image below). The right of B is now become the left of A (i.e. 45).
The process involved in the solution is shown in the following image.

05/23/2025 87
Trees
R-1 Rotation (Node B has balance factor -1)
R-1 rotation is to be performed if the node B has balance factor -1. This case is
treated in the same way as LR rotation. In this case, the node C, which is the
right child of node B, becomes the root node of the tree with B and A as its left
and right children respectively.
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3, T4
become the left and right sub-trees of A.
The process involved in R-1 rotation is shown in the following image.

05/23/2025 88
Trees
Example
Delete the node 60 from the AVL tree shown in the following image.

Solution:
in this case, node B has balance factor -1. Deleting the node
60, disturbs the balance factor of the node 50 therefore, it
needs to be R-1 rotated. The node C i.e. 45 becomes the root
of the tree with the node B(40) and A(50) as its left and right
child.

05/23/2025 89
AVL Tree Deletion

BF(B)=0 BF(B)=+1 BF(B)=-1

BF(A)=+2 R0 (LL) Rotation R1 (LL) Rotation) R-1 (LR) Rotation

BF(A)=-2 L0 (RR) Rotation L1 (RR) Rotation) L-1 (RL) Rotation

05/23/2025 90
Trees
Motivation for B-Trees
• So far we have assumed that we can store an entire data
structure in main memory
• What if we have so much data that it won’t fit?
• We will have to use disk storage but when this happens our time
complexity fails

05/23/2025 91
Trees

Motivation (cont.)
• Assume that a disk spins at 3600 RPM
• In 1 minute it makes 3600 revolutions, hence one revolution
occurs in 1/60 of a second, or 16.7ms
• On average what we want is half way round this disk – it will take
8ms
• This sounds good until you realize that we get 120 disk accesses
a second – the same time as 25 million instructions
• In other words, one disk access takes about the same time as
200,000 instructions
• It is worth executing lots of instructions to avoid a disk
access

05/23/2025 92
Trees

Motivation (cont.)
• Assume that we use an Binary tree to store all the details of
people in Canada (about 32 million records)
• We still end up with a very deep tree with lots of different disk
accesses; log2 20,000,000 is about 25, so this takes about
1.21 seconds (if there is only one user of the program)
• We know we can’t improve on the log n for a binary tree
• But, the solution is to use more branches and thus less height!
• As branching increases, depth decreases

05/23/2025 93
Trees

M-Way Search Tree


1. Generalized version of BST

2. The goal is to minimize the access time( Time required to load the data from the
memory) while retrieving a key value from the file.

A m-way search tree is an empty tree if tree is not empty then it satisfies the
following properties

(i) Each node has at most m-child nodes called order of the tree.

Each node can be represented as (A0,k1A1,k2A2…..Km-1,Am-1)

(ii) If m is the order of the tree, then max no. of child pointers are m and max
number of key stores m-1.

05/23/2025 94
Trees
2. If a node has k keys than, k<=m-1
than the keys are k1,k2,k3,….km
such that ki<ki+1 also each of the
keys in the node partition the sub
trees into k+1 subsets.

3. For any node all the keys in the


subtree pointed by Ai-1 are less than
the key ki and all the keys in the sub
tree pointed by Ai are greter than the
key ki.

4. Each subtree pointed by ai are


also m-way search tree.

05/23/2025 95
M-way Trees
An M-way(multi-way) tree is a tree that has the following properties:
• Each node in the tree can have at most m children.
• Nodes in the tree have at most (m-1) key fields and pointers(references) to
the children.

05/23/2025 96
M-way Search Trees
• An M-way search tree is a more constrained m-way tree, and these
constrain mainly apply to the key fields and the values in them. The
constraints on an M-way tree that makes it an M-way search tree
are:
• Each node in the tree can associate with m children and m-1 key
fields.
• The keys in any node of the tree are arranged in a sorted
order(ascending).
• The keys in the first K children are less than the Kth key of this
node.
• The keys in the last (m-K) children are higher than the Kth key.

05/23/2025 97
M-way Search Tree

05/23/2025 98
M-way Search Trees
• If we want to search for a value say X in an M-way search tree and
currently we are at a node that contains key values from Y1, Y2,
Y3,.....,Yk. Then in total 4 cases are possible to deal with this scenario,
these are:

• If X < Y1, then we need to recursively traverse the left subtree of Y1.
• If X > Yk, then we need to recursively traverse the right subtree of Yk.
• If X = Yi, for some i, then we are done, and can return.
• Last and only remaining case is that when for some i we have Yi < X <
Y(i+1), then in this case we need to recursively traverse the subtree that is
present in between Yi and Y(i+1).
• An m-Way search tree of n elements ranges from a minimum height
of log_m(n+1) to a maximum of n (It is not height balanced)

05/23/2025 99
Searching in M-way search tree

05/23/2025 100
B Trees Data Structure
A B tree is an extension of an M-way search tree. Besides having all the
properties of an M-way search tree, it has some properties of its own,
these mainly are:
• All the leaf nodes in a B tree are at the same level.
• The root node has at least two child nodes and most M child nodes
• All internal nodes except root node must have at least
⌈ M/2⌉ child nodes and at most M child nodes
• The number of keys in each internal node is one less than the
number of child nodes and these keys and these keys partition the
keys in the subtrees of the node in a manner similar to that of M-
way search tree.

05/23/2025 101
B-tree of degree 5

05/23/2025 102
Trees
Insertion in the B-Tree
Search the element in the B-Tree is the same manner as in the m-way search
tree.
If the element is not present then search tends to finish at some leaf node.

Two cases arise-

Case 1. When leaf element is not full then insert element into it.

Case 2. When leaf node is full


a) Insert the element into the node
b) Split the node at its median into two nodes at the same level and pushing
the median element up by one level into the parent node.
c) If parent node accommodating the median element is not full then exit
otherwise repeat the above steps. It may even call the rearrangement of
keys in the root nodes or the formation of new root itself.

05/23/2025 103
Trees

Constructing a B Tree

Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R.
Order = 5

1. Insert P

2. Insert P

05/23/2025 104
Trees
Constructing a B Tree

Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R,O.
1. Insert Q

2. Insert S

3. Insert B

05/23/2025 105
Trees

4. Insert X

5. Insert C

6. Insert L

05/23/2025 106
Trees
7. Insert Z

8. Insert Y

9. Insert T

05/23/2025 107
Trees
10. Insert G

11 Insert J

12 Insert I

05/23/2025 108
Trees

13. Insert D

14. Insert H

05/23/2025 109
Trees
15. Insert R

16. Insert V

05/23/2025 110
Trees

17. Insert E

18. Insert U

05/23/2025 111
Trees

19. Insert F

20. Insert O

05/23/2025 112
Trees

Constructing a B Tree
Example
key :- 1,12,8,2,25,6,14,28,17,7,52,16,48,68,3,26,29,53,55,45,67.
Order = 5
Procedure for adding key in b-tree

Step1. Add first key as root node.

05/23/2025 113
Trees

Step2. Add next key at the appropriate place in sorted order.

Step3. Same process applied until root node full. if root node full then
splitting process applied.

05/23/2025 114
Trees

05/23/2025 115
Trees

05/23/2025 116
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

05/23/2025 117
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

05/23/2025 118
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

05/23/2025 119
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

05/23/2025 120
Trees

Deleting from a B-tree

As usual, this is the hardest of the processes to apply. The deletion


process will basically be a reversal of the insertion process - rather
than splitting nodes, it's possible that nodes will be merged so that
B-tree properties, namely the requirement that a node must be at
least half full, can be maintained.
There are two main cases to be considered:
1.Deletion from a leaf
2.Deletion from a non-leaf

05/23/2025 121
Case 1: Deletion from a leaf
1a) If the leaf is at least half full after deleting the desired value, the remaining larger values are
moved to "fill the gap".

Deleting 6 from the following tree:

05/23/2025 122
1b) If the leaf is less than half full after deleting the desired value (known as underflow), two
things could happen:
Deleting 7 from the tree above results in

1b-1) If there is a left or right sibling with the number of keys exceeding the minimum
requirement, all of the keys from the leaf and sibling will be redistributed between them
by moving the separator key from the parent to the leaf and moving the middle key from
the node and the sibling combined to the parent.

05/23/2025 123
Now delete 8 from the tree:

1b-2) If the number of keys in the sibling does not exceed the minimum requirement, then the
leaf and sibling are merged by putting the keys from the leaf, the sibling, and the separator
from the parent into the leaf. The sibling node is discarded and the keys in the parent are
moved to "fill the gap". It's possible that this will cause the parent to underflow. If that is the
case, treat the parent as a leaf and continue repeating step 1b-2 until the minimum requirement
is met or the root of the tree is reached.

Special Case for 1b-2: When merging nodes, if the parent is the root with only one key, the
keys from the node, the sibling, and the only key of the root are placed into a node and this
will become the new root for the B-tree. Both the sibling and the old root will be discarded.

05/23/2025 124
05/23/2025 125
Case 2: Deletion from a non-leaf
This case can lead to problems with tree reorganization but it will be solved in a manner
similar to deletion from a binary search tree.

The key to be deleted will be replaced by its immediate predecessor (or successor) and then
the predecessor (or successor) will be deleted since it can only be found in a leaf node.
Deleting 16 from the tree above results in:

05/23/2025 126
The "gap" is filled in with the immediate predecessor:

05/23/2025 127
Huffman Coding
 Huffman Coding is a famous Greedy Algorithm.

 It is used for the lossless compression of data.

 It uses variable length encoding.

 It assigns variable length codes to all the characters depending


on how frequently they occur in the given text.
 The character which occurs most frequently gets the smallest
code.

 The character which occurs least frequently gets the largest


code.

 It is also known as Huffman Encoding.


05/23/2025 128
Huffman Coding
Prefix Rule-
 To prevent ambiguities while decoding, Huffman coding
implements a rule known as a prefix rule.
 It ensures that the code assigned to any character is not a
prefix of the code assigned to any other character.

Major Steps in Huffman Coding-

There are two major steps in Huffman coding-


1. Building a Huffman tree from the input characters.
2. Assigning codes to the characters by traversing the Huffman
tree.

05/23/2025 129
Huffman Coding

The steps involved in the construction of Huffman Tree


are as follows-

Step-01:
 Create a leaf node for all the given characters.
 Leaf node contains the occurring frequency of that character.

Step-02:

 Arrange all the nodes in increasing order of their frequency value.

05/23/2025 130
Huffman Coding

Considering the first two nodes having minimum frequency,


 Create a new internal node.
 The frequency of this node is the sum of the frequency of those
two nodes.
 Make the first node as a left child and the other node as a
right child of the newly created node.

Step-04:

 Keep repeating Step-02 and Step-03 until all the nodes form a
single tree.
 The tree finally obtained will be the desired Huffman Tree.

05/23/2025 131
Huffman Coding

Important Formulas-
Formula-01:

05/23/2025 132
Huffman Coding

Formula-02:
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code
length per character
= ∑ ( frequencyi x Code lengthi )

05/23/2025 133
Huffman Coding

PRACTICE PROBLEM BASED ON HUFFMAN CODING-

Problem-

A file contains the following characters with the frequencies as shown.


If Huffman coding is used for data compression, determine-
1. Huffman code for each character
2. Average code length
3. Length of Huffman encoded message (in bits)

05/23/2025 134
Huffman Coding

Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1

05/23/2025 135
Huffman Coding
Solution-
First let us construct the Huffman Tree.
Huffman Tree is constructed in the
following steps-
Step-01:

Step-02:

05/23/2025 136
Huffman Coding

Step-03:

05/23/2025 137
Huffman Coding
Step-04:

05/23/2025 138
Huffman Coding
Step-05:

05/23/2025 139
Huffman Coding
Step-06:

05/23/2025 140
Huffman Coding
Step-07:

05/23/2025 141
Huffman Coding
Now,
 We assign weight to all the edges of the constructed Huffman
tree.
 Let us assign weight ‘0’ to the left edges and weight ‘1’ to the
right edges.
Rule
 If you assign weight ‘0’ to the left edges, then assign
weight ‘1’ to the right edges.
 If you assign weight ‘1’ to the left edges, then assign
weight ‘0’ to the right edges.
 But follow the same convention at the time of decoding
that was adopted at the time of encoding.

After assigning weight to all the edges, the modified Huffman Tree is-

05/23/2025 142
Huffman Coding

Now, let us answer each part of the problem one by one-

05/23/2025 143
Huffman Coding

1. Huffman Code For The Characters-


To write Huffman Code for a character, traverse the Huffman
tree from root node to all the leaf node of that character.
Following this rule, the Huffman Code for each character is-
 a = 111
 e = 10
 i = 00
 o = 11001
 u = 1101
 s = 01
 t = 11000

05/23/2025 144
Huffman Coding
From here, We can observe-
 Characters occurring less frequently in the text are
assigned the larger code.
 Characters occurring more frequently in the text are
assigned the smaller code.

2. Average Code Length-

Average code length


= ∑ ( frequencyi x code lengthi ) / ∑ ( frequencyi )
= { (10 x 3) + (15 x 2) + (12 x 2) + (3 x 5) + (4 x 4) + (13 x 2) +
(1 x 5) } / (10
+ 15 + 12 + 3 + 4 + 13 + 1)
= 2.52

05/23/2025 145
Huffman Coding

3. Length of Huffman Encoded Message-

Total number of bits in Huffman encoded message


= Total number of characters in the message x Average code
length per character
= 58 x 2.52
= 146.16
≅ 147 bits

05/23/2025 146
Trees
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the link fields contain
NULL values which results in wastage of storage space.
• If a binary tree consists of n nodes then n+1 link fields contain NULL values. So in order
to effectively manage the space, a method was devised by Perlis and Thornton in which
the NULL links are replaced with special links known as threads.
• Such binary trees with threads are known as threaded binary trees. Each node in a
threaded binary tree either contains a link to its child node or thread to other nodes in
the tree.

05/23/2025 147
Trees

Types of Threaded Binary Tree


There are two types of Threaded Binary Tree
• One-way threaded Binary Tree
• Two-way threaded Binary Tree

05/23/2025 148
Trees

Threaded Binary Tree Two-Way

• In the two-way threading of T.


• A thread will also appear in the left field of a node and will point to the
preceding node in the in-order traversal of tree T.
• Furthermore, the left pointer of the first node and the right pointer of the
last node (in the in-order traversal of T) will contain the null value
when T does not have a header node.

05/23/2025 149
Trees

Threaded Binary Tree Two-Way

• In the two-way threading of T.


• A thread will also appear in the left field of a node and will point to the
preceding node in the in-order traversal of tree T.
• Furthermore, the left pointer of the first node and the right pointer of the
last node (in the in-order traversal of T) will contain the null value
when T does not have a header node.

05/23/2025 150
Trees
One Way Threaded Binary Tree
• In one-way threaded binary trees, a thread will appear either in the right or left link
field of a node.
• If it appears in the right link field of a node then it will point to the next node that will
appear on performing inorder traversal. Such trees are called Right threaded binary
trees.
• If thread appears in the left field of a node then it will point to the nodes inorder
predecessor. Such trees are called Left threaded binary trees.
• In one-way threaded binary trees, the right link field of last node and left link field of
first node contains a NULL.
• In order to distinguish threads from normal links they are represented by dotted lines.

05/23/2025 151
05/23/2025 152
Trees
Two-way threaded Binary Trees:
In two-way threaded Binary trees, the right link field of a node
containing NULL values is replaced by a thread that points to nodes
inorder successor and left field of a node containing NULL values is
replaced by a thread that points to nodes inorder predecessor.

05/23/2025 153
In the above figure of two-way threaded Binary tree, we noticed that no
left thread is possible for the first node and no right thread is possible
for the last node.
This is because they don't have any inorder predecessor and successor
respectively. This is indicated by threads pointing nowhere.
So in order to maintain the uniformity of threads, we maintain a special
node called the header node.

05/23/2025 154
05/23/2025 155
Trees

Threaded Binary Tree


Advantages of threaded binary tree:

• Threaded binary trees have numerous advantages over non-threaded


binary trees listed as below:
• The traversal operation is more faster than that of its unthreaded
version, because with threaded binary tree non-recursive
implementation is possible which can run faster and does not require
the botheration of stack management.

05/23/2025 156
Trees

Threaded Binary Tree


• Advantages of threaded binary tree:
• The second advantage is more understated with a threaded binary
tree, we can efficiently determine the predecessor and successor
nodes starting from any node.
In case of unthreaded binary tree, however, this task is more time
consuming and difficult.
For this case a stack is required to provide upward pointing
information in the tree whereas in a threaded binary tree, without
having to include the overhead of using a stack mechanism the same
can be carried out with the threads.

05/23/2025 157
Trees

Threaded Binary Tree


• Advantages of threaded binary tree:
Any node can be accessible from any other node.
Threads are usually more to upward whereas links are downward.
Thus in a threaded tree, one can move in their direction and nodes
are in fact circularly linked.
This is not possible in unthreaded counter part because there we can
move only in downward direction starting from root.
Insertion into and deletions from a threaded tree are although time
consuming operations but these are very easy to implement.

05/23/2025 158
Trees

Threaded Binary Tree


• Disadvantages of threaded binary tree:

• Insertion and deletion from a threaded tree are very time


consuming operation compare to non-threaded binary tree.

• This tree require additional bit to identify the threaded link.

05/23/2025 159
Thank you

05/23/2025 160

You might also like