0% found this document useful (0 votes)
1 views

Tree and Huffman Coding

The document provides an overview of non-linear data structures, specifically focusing on trees and their properties. It explains basic terminologies, types of binary trees, their representations in memory, and traversal methods. Additionally, it covers various properties and characteristics of binary trees, including strict, extended, full, complete, balanced, and degenerate binary trees.

Uploaded by

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

Tree and Huffman Coding

The document provides an overview of non-linear data structures, specifically focusing on trees and their properties. It explains basic terminologies, types of binary trees, their representations in memory, and traversal methods. Additionally, it covers various properties and characteristics of binary trees, including strict, extended, full, complete, balanced, and degenerate binary trees.

Uploaded by

Rudraksh Mall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

Data Structures

Tree

Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Non-linear Data Structures
A data structure is said to be non-linear if its elements form a hierarchical
relationship where data items appear at various levels.
Trees and Graphs are widely used non-linear data structures. Tree and
graph structures represent hierarchical relationships between individual
data elements.
Trees can be defined recursively as:
A tree is a finite set of nodes such that:
1. There is a distinguished node called root node
2. The remaining nodes are partitioned into n>=0 disjoint sets T1, T2 ,…,
Tn where each of these sets is a tree. The sets T1, T2,…,Tn are the
subtrees of the root.
Basic Terminologies in Tree Data Structure:
Parent Node: The node which is a predecessor of a node is called the parent node of that
node. {B} is the parent node of {D, E}.
Child Node: The node that is the immediate successor of a node is called the child node of
that node. Examples: {D, E} are the child nodes of {B}.
Root Node: The topmost node of a tree or the node that does not have any parent node is
called the root node. {A} is the root node of the tree. A non-empty tree must contain
exactly one root node and exactly one path from the root to all other nodes of the tree.
Leaf Node or External Node: The nodes that do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P, G} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
Descendant: A node x is a descendant of another node y if and only if y is an ancestor of y.
Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
Level of a node: The count of edges on the path from the root node to that node. The root
node has level 0.
Internal node: A node with at least one child is called an Internal Node.
Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
Subtree: Any node of the tree along with its descendant.
Binary Tree
A binary tree is a finite set of nodes that is:
1. Either empty or
2. Consists of a distinguished node called root and the remaining nodes
are partitioned into two disjoint sets T1 (called left subtree) and T2 (called
right subtree) and both of them are binary trees.
Property 1: The maximum number of nodes on any level i is 2i where i ≥0.

Property 2: The maximum number of nodes possible in a binary tree of


height h is 2h-1.

Proof:
If we sum up the maximum number of nodes possible on each level then we
can get the maximum number of nodes possible in the binary tree. First level
is 0 and last level is h-1. By using property 1, the total number of nodes
possible in a binary tree of height h is given by:
h 1
n   2i
i 0

n  1  21  22  ...  2h 1
2( h 1) 1  1
n
2 1
n  2h  1
Property 3: The minimum number of nodes possible in a binary tree of
height h is equal to h.

Property 4: If a binary tree contains n nodes, then its maximum height


possible is n and the minimum height possible is log 2 (n  1)  .
Proof: There should be at least one element on each level, so the height
cannot be more than n. A binary tree of height h can have a maximum 2h-1
nodes (from property 2). So, the number of nodes will be less than or equal
to this maximum value.
n  2h  1
2h  n  1
h  log 2 (n  1)
h  log 2 (n  1)  (h is an integer)
So the minimum height possible is log 2 (n  1)  .
Property 5: In a non-empty binary tree, if n is the total number of nodes and
e is the total number of edges then e=n-1.

Property 6: For any non-empty binary tree, if n0 is the number of nodes


with no child and n2 is the number of nodes with 2 children, then n0 = n2+1.

Total Number of binary tree is generated by using n nodes (without


label nodes) : = 2nCn/(n+1)

Total Number of binary tree is generated by using n nodes (labelled


nodes) : = 2nCn/(n+1)*n!

Total Number of binary tree of maximum height generated by using n


nodes: = 2n-1
Types of Binary Tree
1. Strictly Binary Tree
A Binary Tree is a strictly binary tree if each node in the tree is either a leaf
node or has exactly two children i.e. there is no node with one child.

Property 7: A strictly binary tree with n non-leaf nodes has n+1 leaf nodes.
Property 8: A strictly binary tree with n leaf nodes always has 2n-1 nodes.
Remark: Number of Leaf nodes = Number of Internal nodes + 1
2. Extended Binary Tree
If in a binary tree, each empty subtree (NULL link) is replaced by a special
node then the resulting tree is an extended binary tree or 2-tree.
So we can convert a binary tree to an extended binary tree by adding special
nodes to leaf nodes and nodes that have only one child.
The special nodes added to the tree are called external nodes and the
original nodes of the tree are internal nodes.

Internal nodes

External nodes
The path length for any node is the number of edges traversed from that
node to the root node.
The path length of a tree is the sum of the path lengths of all the nodes of
the tree.
The internal path length of a binary tree is the
sum of the path lengths of all internal nodes
which is equal to the sum of levels of all internal
nodes.
Internal path length (I): 0+1+1+2+2+3 = 9
The external path length of a binary tree is the
sum of the path lengths of all external nodes
which is equal to the sum of levels of all
external nodes.
External path length (E): 2+2+3+3+3+4+4 = 21

Property 9: In an extended binary tree, if E is the external path length, I is the


internal path length and n is the number of internal nodes then E = I+2n.
3. Full Binary Tree
A binary tree is a full binary tree if all the levels have a maximum number
of nodes, i.e. if the height of the tree is h, then it will have 2h-1 nodes.

Full Binary Tree


4. Complete Binary Tree
A complete binary tree is a binary
tree in which every level, except
possibly the last, has to be filled and
all nodes are as far left as possible.

Complete Binary Tree

Property 10: If the height of a complete binary tree is h, h ≥1, then the
minimum number of nodes possible is 2h-1 and the maximum number of nodes
possible is 2h -1.
Proof: The number of nodes will be maximum when the last level also
contains maximum nodes i.e. all levels are full, and so total nodes will be 2h -1
from property 2.
The number of nodes will be minimal when the last level has only one node. In
this case the total nodes will be
Total nodes in a full binary tree of height (h-1) + one node
i.e. (2h-1 -1)+1 = 2h-1.
5. Balanced Binary Tree
A balanced binary tree is a binary tree
in which the height of the left and the
right sub-trees of every node may
differ by at most 1.
Remark: AVL Tree and Red-Black Tree are
well-known data structures.

6. Degenerate Binary Tree


Degenerate Binary Tree is a Binary
Tree where every parent node has
only one child node.
Remark: The height of a Degenerate Binary
Tree is equal to the total number of nodes in
that tree.
Representation of Binary Trees in Memory
There are two common methods used for representing a binary tree
1. Sequential representation using Array
2. Linked representation using Linked List
1. Sequential representation using Array
A one-dimensional array can be used
to represent a binary tree in the
memory. Here, we consider that the
root is present at index 0. Assume
tree[N] is an array representing a
binary tree. If ‘i’ be index, then

1. The root node is stored at index 0, i.e. root is at tree[0].


2. Left child is stored at tree[2*i+1].
3. Right child is stored at tree[2*i+2].  ( i  1) 
4. Its parent will be at index floor((i-1)/2) i.e.  2  .
Drawbacks of Array Representation: While representing a binary tree in the form of an
array, most of the memory blocks in the middle will be left blank or unused if the tree is
not a complete binary tree.
2. Linked representation using Linked List
A binary tree can be represented by using a linked list, where each node is
divided into 3 parts:
• Info: it is used to store the information
• Left: It is a pointer that holds the address of the left child
• Right: It is a pointer that holds the address of the right child

The structure for tree node can be declared as


struct node{
char data;
struct node *lchild;
struct node *rchild;
};
Construction of General Tree to Binary Tree
It is convenient to represent a general tree to binary tree in a program,
because in the case of a general tree, the number of edges connected from a
node at any given time is unpredictable.
Therefore the node space has to be managed in such a way that each node is
allowed a variable number of sub-tree pointers.

The procedure is:


• Insert edges connecting siblings from left to right at the same level.
• Delete all edges of a parent to its children except to its left most
offspring.
• Rotate the resultant tree 45o to mark clearly the left and right sub-trees.
Traversal of Binary Tree

The process of visiting each node of the tree exactly once is called tree
traversal. The traversal of the binary tree involves three basic activities such
as:
1. Visiting the root
2. Traverse the left sub-tree
3. Traverse the right sub-tree
These three basic activities are done in a different order which follows:
1. Pre-order Traversal (NLR)
a. Visit the root (N)
b. Traverse the left subtree of root in
preorder(L)
c. Traverse the right subtree of root in
preorder(R)

The Pre-order traversal is: ABDECFG


2. In-order Traversal (LNR)
a. Traverse the left subtree of root in
preorder(L)
b. Visit the root (N)
c. Traverse the right subtree of root in
preorder(R)

The In-order traversal is: DBEAFCG

3. Post-order Traversal (LRN)


a. Traverse the left subtree of root in
preorder(L)
b. Traverse the right subtree of root in
preorder(R)
c. Visit the root (N)

The In-order traversal is: DEBFGCA


The above tree traversal methods can be implemented in two ways:
1. Recursive tree traversal
2. Non-Recursive tree traversal
1. Recursive tree traversal
void preorder(struct node * ptr) void inorder(struct node * ptr)
{ {
if(ptr = = NULL) if(ptr = = NULL)
return; return;
printf(“%d”, ptr->info); inorder(ptr->lchild);
preorder(ptr->lchild); printf(“%d”, ptr->info);
preorder(ptr->rchild); inorder(ptr->rchild);
} }
void postorder(struct node * ptr)
{
if(ptr = = NULL)
return;
postorder(ptr->lchild);
postorder(ptr->rchild);
printf(“%d”, ptr->info);
}
2. Non-Recursive tree traversal
void nrec_preorder(struct node *root)
Pre-order Traversal {
1. Push the root node on the stack struct node *ptr = root;
2. Pop a node from the stack if(ptr = = NULL)
3. Visit the popped node {
4. Push the right child of the visited printf(“Tree is empty”);
node on the stack return;
5. Push the left child of the visited }
node on the stack push_stack(ptr);
6. Repeat steps 2,3,4,5 till the stack while(!stack_empty())
is not empty. {
ptr = pop_stack();
printf(“%d”, ptr->info);
if(ptr->rchild!=NULL)
push_stack(ptr->rchild);
if(ptr->lchild!=NULL)
push_stack(ptr->lchild);
}
printf(“\n”);
}
If we apply the algorithm to the tree, the steps would be:
• Push 50
• Pop 50: Visit 50
Push right and left child of 50: Push 60. Push 40
• Pop 40: Visit 40
40 has no right child so push its left child: Push 30
• Pop 30: Visit 30
Push right and left child of 30: Push 35. Push 25
• Pop 25: Visit 25
25 has no right child so push its left child: Push 20
• Pop 20: Visit 20
20 has no child so nothing is pushed
• Pop 35: Visit 35
Push right and left child of 35: Push 36. Push 33
• Pop 33: Visit 33
33 has no child so nothing is pushed
• Pop 36: Visit 36
36 has no child so nothing is pushed
• Pop 60: Visit 60
60 has no left child so push its right child: Push 70
• Pop 70: Visit 70
Push right and left child of 70: Push 80. Push 65 Hence pre-order traversal is:
• Pop 65: Visit 65
65 has no child so nothing is pushed 50 40 30 25 20 35 33 36 60 70 65 80
• Pop 80: Visit 80
80 has no child so nothing is pushed Stack is empty
In-order Traversal void nrec_inorder(struct node *root)
1. Initially ptr is assigned the address {
of the root node. struct node *ptr = root;
2. Move along the leftmost path if(ptr = = NULL)
{
rooted at the node pointed by ptr,
printf(“Tree is empty”);
pushing all the nodes in the path return;
on the stack. Stop when we reach }
the leftmost node i.e. a node that while(1)
has no left child, it is not pushed {
on the stack. Now ptr points to this while(ptr->lchild!=NULL)
leftmost node. {
3. If the node pointed by ptr has no push_stack(ptr);
ptr = ptr->lchild;
right subtree, then visit it and pop
}
another one from the stack. Now while(ptr->rchild==NULL)
keep on popping and visiting the {
nodes till anode is popped that has printf(“%d”, ptr->info);
a right subtree. Now ptr points to if(stack_empty())
this node that has a right subtree. return;
If the stack becomes empty then ptr = pop_stack();
the traversal is finished. }
printf(“%d”, ptr->info);
4. Visit the node pointed by ptr, and
ptr = ptr->rchild;
now ptr is assigned the address of }
its right child. printf(“\n”);
5. Go to step 2 }
Post-order Traversal void nrec_postorder(struct node *root)
1. Initially ptr is assigned the address of {
struct node *q, *ptr = root;
the root node.
if(ptr = = NULL)
2. Move along the leftmost path rooted at {
the node ptr pushing all the nodes in printf(“Tree is empty”);
the path on the stack. Stop when we return;
reach the leftmost node i.e. a node that }
has no left child, it is not pushed on the q = root;
stack. Now ptr points to this leftmost while(1)
node. {
while(ptr->lchild!=NULL)
3. If ptr has no roght subtree or ots right
{
subtree has been traversed then visit push_stack(ptr);
the node and pop another one from the ptr = ptr->lchild;
stack. Now keep on popping and }
visiting the nodes till a node is popped while(ptr->rchild==NULL || ptr->rchild ==q)
that has a right subtree which has not {
been traversed. Now ptr points to this printf(“%d”, ptr->info);
node that has an untraversed right q = ptr;
if(stack_empty())
subtree. If the stack becomes empty
return;
then the traversal is finished. ptr = pop_stack();
4. Push the node pointed by ptr, and now }
ptr is assigned the address of its right push_stack(ptr);
child. ptr = ptr->rchild;
5. Go to step 2 }
}
Level-order Traversal
1. Insert the root node in the queue
2. Delete a node from the front of the queue and visit it
3. Insert the left child of the visited node in the queue at the end
4. Insert the right child of the visited node in the queue at the end
5. Repeat steps 2,3,4 till the queue is not empty.

The level-order traversal is ABCDEFG.


Creation of Binary Tree from In-order and Pre-order traversals

The Pre-order traversal is: A B D H E C F I G J K


The In-order traversal is: D H B E A I F C J G K
In pre-order traversal, the first node is the root node.
Hence, A is the root of the binary tree.
From In-order traversal, we see that nodes to the left of
root node A are nodes D, H, B, E so these nodes form the
left subtree of A, similarly nodes I, F, C, J, G, K form the
right subtree of A.

D H B E I F C J G K
Pre-order : B D H E Pre-order : C F I G J K
In-order : D H B E In-order : I F C J G K

left subtree of A right subtree of A


A

B C

D H E I F J G K
Pre-order : D H Pre-order : F I Pre-order : G J K
In-order : D H In-order : I F In-order : J G K
left subtree of B left subtree of C right subtree of C

B C

D E F G

H I J K
Creation of Binary Tree from In-order and Post-order traversals

The Post-order traversal is: H D E B I F J K G C A


The In-order traversal is: D H B E A I F C J G K
In post-order traversal, the last node is the root node.
Hence, A is the root of the binary tree.
From In-order traversal, we see that nodes to the left of
root node A are nodes D, H, B, E so these nodes form the
left subtree of A, similarly nodes I, F, C, J, G, K form the
right subtree of A.

D H B E I F C J G K
Post-order : H D E B Post-order : I F J K G C
In-order : D H B E In-order : I F C J G K

left subtree of A right subtree of A


A

B C

D H E I F J G K
Post-order : H D Post-order : I F Post-order : J K G
In-order : D H In-order : I F In-order : J G K
left subtree of B left subtree of C right subtree of C

B C

D E F G

H I J K
Creation of Binary Tree from Pre-order and Post-order traversals

The Pre-order traversal is: A B D E H I C F G


The Post-order traversal is: D H I E B F G C A
In first node of pre-order and last node of post-order
traversal i.e. A is considered as root of the binary tree.
Find the node succussing the root node in pre-order, say x1
and the node preceding the root node in post-order say x2.
• If x1 = x2, then it can be taken as left or right child of root and which may not result
unique tree.
• If x1 != x2, then x1 is taken as left child i.e. B and x2 is taken as right child i.e. C of
the root node.
Find the position of x2, i.e. ‘C’ in pre-order and position of x1, i.e. ‘B’ in the post order.
• Consider two sets of pre-order and post-order traversal of left and right sub-tree of root.
• The first set consists of nodes that are present after x1 and before x2 in pre-order
traversal i.e. DEHI and the nodes present before x1 in post-order i.e. DHIE.
• The second set consists of nodes that are present after x2 in pre-order traversal i.e. FG
and the nodes present after x1 and before x2 in post-order traversal i.e. FG.
A

B = x1 B C C = x2

D H E I G F
Pre-order : D E H I Pre-order : F G
Post-order : D H I E Post-order : F G

Find the node succussing the root node in pre-order, say x1 (i.e. D) and the node
preceding the root node in post-order say x2 (i.e. E).
• If x1 != x2, then x1 is taken as left child i.e. D and x2 is taken as right child i.e. E of
the root node.
Find the position of x2, i.e. ‘E’ in pre-order and position of x1, i.e. ‘D’ in the post order.
• Consider two sets of pre-order and post-order traversal of left and right sub-tree of root.
• The first set consists of nodes that are present after x1 and before x2 in pre-order
traversal i.e. NULL and the nodes present before x1 in post-order i.e. NULL.
• The second set consists of nodes that are present after x2 in pre-order traversal i.e. HI
and the nodes present after x1 and before x2 in post-order traversal i.e. HI.
A

B C
G F
D = x1 D E E = x2 Pre-order : F G
Post-order : F G
H I
Pre-order : HI
Post-order : HI

Follow the same procedure to get the final binary tree.


Height of Binary Tree
int height(struct node *ptr)
{
int h_left, h_right;
if(ptr = = NULL)
return 0;
h_left = height(ptr->lchild);
h_right = height(ptr->rchild);
if(h_left > h_right)
return 1+h_left ;
else
return 1+h_right;
}
Expression Tree
It is a binary tree, which stores an arithmetic expression. The leaf nodes of the
expression tree are always operands and all other internal nodes are the operators,
including the root.

arithmetic expression: 4 / ( 2 - ( - 8 * 3 ) )
Construction of Expression Tree:

First of all, we will do scanning of the given expression into left to the right
manner, then one by one check the identified character:
1. If a scanned character is an operand, we will apply the push operation and push it
into the stack.
2. If a scanned character is an operator, we will apply the pop operation into it to
remove the two values from the stack to make them its child, and after then we will
push back the current parent node into the stack.

postfix notation is: a b + c d e + * *


Binary Search Tree

A binary search tree is a binary tree that may be


empty and if it is not empty then it satisfies the
following properties:
1. All the keys in the left subtree of root are less
than the key in the root
2. All the keys in the right subtree of root are greater
than the key in the root
3. Left and right subtrees of root are also binary
search trees.
Traversal in Binary Search Tree

The different traversals for the binary search


tree are:
Pre-order: 30, 20, 10, 15, 25, 23, 39, 35, 42

In-order: 10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42

Post-order: 15 , 10 , 23 , 25, 20, 35, 42, 39, 30

Level order: 30, 20, 39, 10, 25, 35, 42, 15, 23

Remark: Note that the in-order traversal of a binary search tree gives us all keys of
that tree in ascending order.
Searching in a Binary Search Tree

1. Compare the element with the root of the tree.


2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on root, if so then
move to the left sub-tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.
Non-recursive code for searching a key in Binary Search Tree

struct node *search_nrec(struct node *ptr, int skey)


{
while(ptr!=NULL)
{
if(skey < ptr->info)
ptr = ptr->lchild; /* move to left child*/
else if(skey > ptr->info)
ptr = ptr->rchild; /* move to right child*/
else /*skey found*/
return ptr;
}
return NULL;
}
Recursive code for searching a key in Binary Search Tree
struct node *search_rec(struct node *ptr, int skey)
{
if(ptr==NULL)
{
printf(“key not found\n”);
return NULL;
}
else if(skey < ptr->info) /* search in left subtree*/
return search_rec(ptr->lchild, skey);
else if(skey > ptr->info) /* search in right subtree*/
return seach_rec(ptr->rchild, skey);
else /*skey found*/
return ptr;
}
Insertion in a Binary Search Tree

1. We start at the root and move down the tree.


2. If the key to be inserted is equal to the key in the node then there is nothing
to be done as duplicate keys are not allowed.
3. If the key to be inserted is less than the key of the node then we move to
left child.
4. If the key to be inserted is greater than the key of the node then we move
to right child.
5.We insert the new key when we reach a NULL left or right child.
6. END
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Example of creating a binary search tree


Now, let's see the creation of binary search tree using an example.

Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

Step 1 - Insert 45.

Step 2 - Insert 15.


As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 - Insert 79.


As 79 is greater than 45, so insert it as the root node of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.

Step 5 - Insert 10.


10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.
Step 6 - Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.

Step 7 - Insert 12.


12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree of 10.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.

Step 9 - Insert 50.


50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of 55.
Struct node *insert_nrec(struct node *root, int ikey)
{
struct node *tmp, *par, *ptr; /*par: parent*/
ptr = root;
par = NULL;
while(ptr!=NULL)
{
par = ptr;
if(ikey < ptr->info)
ptr = ptr->lchild;
else if(ikey > ptr->info)
ptr = ptr->rchild;
else
{
printf(“Duplicate key”);
return root;
}
}
tmp = (struct node *)malloc(sizeof(struct node));
tmp->info = ikey;
tmp->lchild = NULL;
tmp->rchild = NULL;
if(par = = NULL)
root = tmp;
else if(ikey < par->info)
par->lchild = tmp;
else
par->rchild = tmp;
return root;
}
Struct node *insert_rec(struct node *ptr, int ikey)
{
if(ptr = =NULL)
{
ptr = (struct node *)malloc(sizeof(struct node));
ptr->info = ikey;
ptr->lchild = NULL;
ptr->rchild = NULL;
}
else if(ikey < ptr->info)
ptr->lchild = insert_rec(ptr->lchild, ikey);
else if(ikey > ptr->info)
ptr->rchild = insert_rec(ptr->rchild, ikey);
else
printf(“Duplicate key”);
return ptr;
}
Deletion in a Binary Search Tree

Given a BST, the task is to delete a node in this BST, which can be broken
down into 3 cases:
Case 1. Delete a Leaf Node in BST
Case 2. Delete a Node with Single Child in BST
Deleting a single child node is also simple in BST. Copy the child to the node and
delete the node.

Case 3. Delete a Node with Both Children in BST


Here we have to find the inorder successor of the node. The data of the inorder
successor is copied to the node and then the inorder successor is deleted from the tree.
struct node *del_rec(struct node *ptr, int dkey)
{
struct node *tmp, *succ;
if(ptr = =NULL)
{
printf(“dkey not found”);
return(ptr);
}
if(dkey < ptr->info) /* delete from left subtree*/
ptr->lchild = del_rec(ptr->lchild, dkey);
else if(dkey > ptr->info) /* delete from right subtree*/
ptr->rchild = del_rec(ptr->rchild, dkey);
else
{ /* key to be deleted is found*/
if(ptr->child!=NULL && ptr->rchild!=NULL) /* 2 children*/
{
succ = ptr->rchild;
while(succ->lchild)
succ = succ->lchild;
ptr->info = succ->info;
ptr->rchild = del_rec(ptr->rchild, succ->info);
}
else
{
temp = ptr;
if(ptr->lchild!=NULL) /* only left child*/
ptr = ptr->lchild;
else if(ptr->rchild!=NULL) /* only right child*/
ptr = ptr->rchild;
else
ptr = NULL;
free(tmp);
}
}
return ptr;
}
Threaded Binary Tree

A binary tree with n nodes has 2n pointers out of which n+1 are always
NULL, so we can see that about half the space allocated for pointers is
wasted. We can utilize this wasted to contain some useful information.
A left NULL pointer can be used to store the address of inorder predecessor
of the node and a right NULL pointer can be used to store the address of
inorder successor of the node.

These pointers are called threads and a binary tree which implements these
pointers is called a threaded binary tree.
Types of Threaded Binary Tree

Depending on the type of threading, there are two types of threaded binary
tree:
Types of Threaded Binary Tree

One-way Threading Two-way Threading

Left In-Threaded Binary Tree Right In-Threaded Binary Tree

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


If only left NULL pointers are used as
threads then the binary tree is called a
left in-threaded binary tree.

Left in-threaded binary tree

If only right NULL pointers are used


as threads then the binary tree is
called a right in-threaded binary tree.

Right in-threaded binary tree


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
If both left and right NULL pointers are used as threads then the binary
tree is called a fully in-threaded binary tree.

The structure declaration of a


node in a fully in-threaded
binary tree will be as:

struct node
{
struct node *left;
boolean lthread;
int info;
boolean rthread;
struct node *right;
Fully in-threaded binary tree };

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


In the inorder traversal, the first node has no predecessor and last node has
no successor. So the left pointer of first node and right pointer of last node
are NULL. To make the threading consistent, we can take a dummy node
called the header node which can be predecessor of the first node and the
successor of the last node.

Fully in-threaded binary tree


Finding inorder successor of a node in in-threaded tree

If the right pointer of the node is a thread, then there is no need to do


anything because the right thread points to inorder successor. If node’s
right pointer is not a thread i.e. node has a right child then to find the
inorder successor, we move to this right child and keep on moving left till
we find a node with no left child.

struct node *in_succ(struct node *ptr)


{
if(ptr->rthread = = true)
return ptr->right;
else
{
ptr = ptr->right;
while(ptr->lthread = =false)
ptr = ptr->left;
return ptr;
}
}
Finding inorder predecessor of a node in in-threaded tree

If the left pointer of the node is a thread, then thread will point to inorder
predecessor. If the left pointer is not a thread i.e. node has a left child then
to find the inorder predecessor, we move to this left child and keep on
moving right till we find a node with no right child.

struct node *in_pred(struct node *ptr)


{
if(ptr->lthread = = true)
return ptr->left;
else
{
ptr = ptr->left;
while(ptr->rthread = =false)
ptr = ptr->right;
return ptr;
}
}
Inorder Traversal of in threaded binary tree
struct node *inorder(struct node *root)
The first node to be visit in the {
inorder traversal is the last node struct node *ptr;
in the leftmost branch starting if(root = = NULL)
from root. So we start from the {
root and move left till we get a printf(“Tree is empty”);
return;
node with no left child, and this
}
node is visited. ptr = root;
Now with the help of in_succ() /*Find the leftmost node*/
function we find the inorder while(ptr->lthread = = false)
successor of each node and visit ptr = ptr->left;
while(ptr!=NULL)
it. The right pointer of last node
{
is NULL and we have marked it printf(“%d”, ptr->info);
as thread. So we stop our ptr = in_succ(ptr);
process when we get a node }
whose right thread is NULL. }
AVL Tree

The technique for balancing a binary search tree was introduced by Russian
mathematicians G.M. Adelson, Velski and E. M. Lendis in 1962.

AVL tree is a self-balancing binary search tree in which the heights of the
two sub-trees of a node may differ by at most one. Because of this property,
AVL tree is also known as a height-balanced tree.
The key advantage of using an AVL tree is that it takes O(logn) time to
perform search, insertion and deletion operations in average case as well as
worst case (because the height of the tree is limited to O(logn)).
The structure of an AVL tree is same as that of a binary search tree but with
a little difference. In its structure, it stores an additional variable called the
BalanceFactor.
The balance factor of a node is calculated by subtracting the height of its
right sub-tree from the height of its left sub-tree.
Balance factor = Height (left sub-tree) – Height (right sub-tree)
A binary search tree in which every node
has a balance factor of -1, 0 or 1 is said to be
height balanced. A node with any other
balance factor is considered to be
unbalanced and requires rebalancing.

bf = hl – hr = {-1, 0, 1}

If the balance factor of a node is 1, then it means that the left sub-tree of the
tree is one level higher than that of the right sub-tree. Such a tree is called
Left-heavy tree.

If the balance factor of a node is 0, then it means that the height of the left
sub-tree is equal to the height of its right sub-tree.

If the balance factor of a node is -1, then it means that the left sub-tree of
the tree is one level lower than that of the right sub-tree. Such a tree is
called Right-heavy tree.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Insert a node in AVL Tree

Types of Rotations
1. LL Rotation: Inserted node is in the left subtree of left subtree of C

2. RR Rotation: Inserted node is in the right subtree of right subtree of A


3. LR Rotation: Inserted node is in the right subtree of left subtree of C

LR Rotation = RR Rotation + LL Rotation

4. RL Rotation: Inserted node is in the left subtree of right subtree of A

RL Rotation = LL Rotation + RR Rotation


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
2. Insert B

3. Insert A
4. 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
5. Insert C, F, D

Perform LL rotation Perform RR rotation


on node E on node B
6. Insert G

Perform RR rotation Perform LL rotation


on node C on node H
7. Insert K
8. Insert L
Delete a node in AVL Tree

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.

For this purpose, we need to perform rotations. The two types of rotations
are L rotation and R rotation. 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.
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:
1. 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.
Example: Delete node 30 from the AVL tree shown in the following image.
2. 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.
Example: Delete node 55 from the AVL tree shown in the following image.
3. 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.
Example: Delete node 60 from the AVL tree shown in the following image.
Data Structures
Graphs

Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Huffman Coding

Huffman coding algorithm compresses the storage if data using variable


length codes.
The basic idea behind Huffman coding algorithm is to use fewer bits for
more frequently occurring characters.

is minimum, where |binarycode(c)| represents the length of binary code of


character c.
Prefix Code: A code is called a prefix (free) code if no codeword is a
prefix of another one.
Example: {a = 0, b = 110, c = 10, d = 111} is a prefix code.
Example: 01101100 = 01101100 = abba

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Example:
Suppose in a message we have these letters and their frequencies:

a: 05 b: 48 c: 07 d: 17 e: 10 f: 13

Sort the nodes based on their frequencies. Further, consider the two nodes
having minimum frequencies:

a: 05 c: 07 e: 10 f: 13 d: 17 b: 48

Connect these two nodes at a newly created common node that will store
only the sum of frequencies of all the nodes connected below it.

12 e: 10 f: 13 d: 17 b: 48

a: 05 c: 07
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Repeat the same process:

12 e: 10 f: 13 d: 17 b: 48

a: 05 c: 07
Further, consider the two nodes having minimum frequencies:

22 f: 13 d: 17 b: 48

12 e: 10

a: 05 c: 07
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Further, consider the two nodes having minimum frequencies:

22 f: 13 d: 17 b: 48

12 e: 10

a: 05 c: 07

22 30 b: 48

12 e: 10 f: 13 d: 17

a: 05 c: 07
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
22 30 b: 48

12 e: 10 f: 13 d: 17

a: 05 c: 07

Further, consider the 52 b: 48


two nodes having
minimum frequencies:
22 30

12 e: 10 f: 13 d: 17

a: 05 c: 07
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Further, consider the
two nodes having 100
minimum frequencies: 0 1

52 b: 48
0 1

22 30 Character Code

0 1 0 1 a 0000

b 1
12 e: 10 f: 13 d: 17
c 0001
0 1
d 011
a: 05 c: 07
e 001
f 010

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Character Frequency Code Size

a 5 0000 5*4 = 20
b 48 1 48*1 = 48

c 7 0001 7*4 = 28
d 17 011 17*3 = 51
e 10 001 10*3 = 30

f 13 010 13*3 = 39
6 * 8 = 48
18 bits 216 bits
bits

Without encoding, the total size of the string was 800 bits. After encoding
the size is reduced to 48 + 18 + 216 = 282.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

You might also like