Trees Data Structure

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

lOMoARcPSD|37334717

Trees (Data Structure)

B.tech (Dr. A.P.J. Abdul Kalam Technical University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)
lOMoARcPSD|37334717

Unit 5: TREES

DEFINITION
A tree is a finite set of one or more nodes such that There is a
specially designated node called root.
The remaining nodes are partitioned into n >= 0 disjoint set T1,…,Tn, where each of these sets is
a tree. T1,…,Tn are called the subtrees of the root.

Every node in the tree is the root of some subtree

TERMINOLOGY
• Node: The item of information plus the branches to other nodes
• Degree: The number of subtrees of a node
• Degree of a tree: The maximum of the degree of the nodes in the tree.
• Terminal nodes (or leaf): nodes that have degree zero or node with no successor
• Nonterminal nodes: nodes that don’t belong to terminal nodes.
• Parent and Children: Suppose N is a node in T with left successor S1 and right successor S2,
then N is called the Parent (or father) of S1 and S2. Here, S1 is called left child (or Son) and S2
is called right child (or Son) of N.
• Siblings: Children of the same parent are said to be siblings.
• Edge: A line drawn from node N of a T to a successor is called an edge
• Path: A sequence of consecutive edges from node N to a node M is called a path.
• Ancestors of a node: All the nodes along the path from the root to that node.
• The level of a node: defined by letting the root be at level zero. If a node is at level l, then it
children are at level l+1.
• Height (or depth): The maximum level+1 of any node in the tree

Example

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

A is the root node


B is the parent of E and F
C and D are the sibling of B
E and F are the children of B
K, L, F, G, M, I, J are external nodes, or leaves
A, B, C, D, E, H are internal nodes
The level of E is 2
The height (depth) of the tree is 4
The degree of node B is 2
The degree of the tree is 3
The ancestors of node M is A, D, H
The descendants of node D is H, I, J, M

BINARY TREES
Definition: A binary tree T is defined as a finite set of nodes such that,
• T is empty or
• T consists of a root and two disjoint binary trees called the left subtree and the right subtree.

Figure: Binary Tree

Different kinds of Binary Tree

1. Skewed Tree
A skewed tree is a tree, skewed to the left or skews to the right. or
It is a tree consisting of only left subtree or only right subtree.
• A tree with only left subtrees is called Left Skewed Binary Tree.
• A tree with only right subtrees is called Right Skewed Binary Tree.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

2. Complete Binary Tree


A binary tree T is said to complete if all its levels, except possibly the last level, have the maximum
number node 2i, i ≥ 0 and if all the nodes at the last level appears as far left as possible.

Figure (a): Skewed binary tree Figure (b): Complete binary tree
3. Full Binary Tree
A full binary tree of depth ‘k’ is a binary tree of depth k having 2 k – 1 nodes, k ≥ 1.

Figure: Full binary tree of level 4 with sequential node number

4. Extended Binary Trees or 2-trees


An extended binary tree is a transformation of any binary tree into a complete binary tree. This
transformation consists of replacing every null subtree of the original tree with “special nodes.” The
nodes from the original tree are then internal nodes, while the special nodes are external nodes.
For instance, consider the following binary tree.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

The following tree is its extended binary tree. The circles represent internal nodes, and square
represent external nodes.
Every internal node in the extended tree has exactly two children, and every external node is a leaf.
The result is a complete binary tree.
PROPERTIES OF BINARY TREES

Lemma 1: [Maximum number of nodes]:


(1) The maximum number of nodes on level i of a binary tree is 2 i, i ≥ 0.
(2) The maximum number of nodes in a binary tree of depth k is 2k -1, k ≥ 1.

Proof:
(1) The proof is by induction on i.
Induction Base: The root is the only node on level i = 0. Hence, the maximum number of nodes on
level i =0 is 2i = 20 = 1.
Induction Hypothesis: Let i be an arbitrary positive integer greater than 1. Assume that the maximum
number of nodes on level i -1 is 2i-1
Induction Step: The maximum number of nodes on level i -1 is 2i-1 by the induction hypothesis. Since
each node in a binary tree has a maximum degree of 2, the maximum number of nodes on level i is
two times the maximum number of nodes on level i-1, or 2i

(2) The maximum number of nodes in a binary tree of depth k is k


k
∑ (maximum number of nodes on level i) = ∑ 2i-1 = 2k-1
i=0 i=0

Lemma 2: [Relation between number of leaf nodes and degree-2 nodes]:


For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree
2, then n0 = n2 + 1.

Proof: Let n1 be the number of nodes of degree one and n the total number of nodes.
Since all nodes in T are at most of degree two, we have
n = n0 + n1+ n2 (1)
Count the number of branches in a binary tree. If B is the number of branches, then n =B +
1.
All branches stem from a node of degree one or two. Thus,
B =n 1+ 2n2.
Hence, we obtain
n = B + 1= n 1+ 2n2 + 1 (2)

Subtracting Eq. (2) from Eq. (1) and rearranging terms, we get

n0 = n2 +1

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Consider the figure:

Here, For Figure (b) n2=4, n0= n2+1= 4+1=5 Therefore, the total
number of leaf node=5

BINARY TREE REPRESENTATION


The storage representation of binary trees can be classified as
1. Array representation
2. Linked representation.

Array representation:
• A tree can be represented using an array, which is called sequential representation.
• The nodes are numbered from 1 to n, and one dimensional array can be used to store the nodes.
• Position 0 of this array is left empty and the node numbered i is mapped to position i of the array.
Below figure shows the array representation for both the trees of figure (a).

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

• For complete binary tree the array representation is ideal, as no space is wasted.
• For the skewed tree less than half the array is utilized.

Linked representation:
The problems in array representation are:
• It is good for complete binary trees, but more memory is wasted for skewed and many other
binary trees.
• The insertion and deletion of nodes from the middle of a tree require the movement of many
nodes to reflect the change in level number of these nodes.

These problems can be easily overcome by linked representation

Each node has three fields,


• LeftChild - which contains the address of left subtree
• RightChild - which contains the address of right subtree.
• Data - which contains the actual information

Figure: Node representation

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Linked representation of the binary tree

BINARY TREE TRAVERSALS:


Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes
are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly
access a node in a tree. There are three ways which we use to traverse a tree −

 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values
it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We
should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

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
Algorithm
ALGORITHM Inorder_traversal(Root)
BEGIN:
IF root == NULL THEN
RETURN 0;
ELSE
Inorder_traversal(root→Left)
WRITE (root→Data)
Inorder_traversal(root→Right)
END;
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the 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
Algorithm
Preorder_traversal(Root)
BEGIN:

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

IF root == NULL THEN


RETURN 0
ELSE
WRITE (root → Data)
Preorder_Traversal(root→ Left)
Preorder_Traversal(root → Right)
END;

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree,
then the right subtree and finally the 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
Algorithm

Postorder_traversal( Root)
BEGIN:
IF root == NULL THEN
RETURN 0;
ELSE
Postorder_traversal(root→Left)
Postorder_traversal(root→Right)
WRITE (root→Data)
END;

Tree Conversion
Building of tree using Inorder Tree Traversal and Preorder Tree Traversal
We can build a tree using Inorder and Preorder Traversal; the first node in preorder traversal will be
root node. Using Inorder Traversal, we can determine the left subpart of the tree and the right subpart
of the tree.
Preorder: 3, 9, 20, 15, 7
In order: 9, 3, 15, 20, 7
Step 1

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Preorder: 3, 9, 20, 15, 7 (3 will be the root of tree)


In order: 9, 3, 15, 20, 7 ( Left Root Right).
Here 3 is root of tree, so 9 will lie on LHS, and 15, 20, 7 will lie
on RHS

Step 2
Preorder: 3, 9, 20, 15, 7 (3 will be the root of tree)
In order: 9, 3, 15, 20, 7(Left Root Right).
Here 9 is the root of subtree, so nothing on LHS and nothing will lie on RHS

Step 3
Preorder: 3, 9, 20, 15, 7 (20 will be the root of tree)
In order: 9, 3, 15, 20, 7(Left Root Right).
Here 20 is the root of subtree, so 15 on LHS and 20 will lie on RHS.

Complexity: - O (n2)
Building of tree using Inorder Tree Traversal and Preorder Tree Traversal
ALGORITHM Build_Tree(Preorder [], Inorder [], n)
BEGIN:
PreStart = 0
PreEnd = n-1 // n is number of elements
InorderStart = 0
InorderEnd = n-1 // n is number of elements
RETURN Construct (Preorder, Inorder, Prestart, PreEnd, InorderStart, InorderEnd)

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

END;
ALGORITHM Construct(Preorder [], Inorder[], Prestart, PreEnd,InorderStart, InorderEnd)
BEGIN:
IF Prestart >PreEnd || InorderStart>InorderEnd THEN
RETURN NULL
Val = Preorder [Prestart]
P=GetNode(Val)
FOR i=0 TO Inorder.length() DO
IF Val == inorder[i] THEN
k=i
BREAK
P→left = Construct (Preorder, Inorder, Prestart + 1, PreStart + (k – InorderStart), InorderStart, k-1)
P→right = Construct (Preorder, Inorder, Prestart + (k – InorderStart)+ 1, PreEnd , k+1, InorderEnd)
RETURN P
END;
Building of tree using Inorder Tree Traversal and Postorder Tree Traversal
We can build a tree using Inorder and Post-order Traversal; the last node in Post-order traversal will
be the root node. Using Inorder Traversal, we can determine the left subpart of the tree and the right
sub part of the tree.
Postorder: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8.
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Step 1
Postorder: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8. (Last element in Postorder traversal will be 8.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have 9,5,1,7,2,12 and Right Subtree will have 4, 3, 11.
Root will be 8

Step 2
Postorder: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8. (Last element in Postorder traversal will be 4.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have nothing and Right Subtree will have 3, 11.
Root will be 4

Step 3
Postorder: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8. (Last element in Postorder traversal will be 11.)

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.


Left Subtree will have 3, and Right Subtree will have nothing.
Root will be 11

Step 4
Postorder: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8. (Last element in Postorder traversal will be 11.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have nothing, and Right Subtree will have nothing.
Root will be 3.

Step 5
Postorder: 9, 1, 2, 12, 7, 5,3, 11, 4, 8. (Last element in Postorder traversal will be 5.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have 9, and Right Subtree will have 1,7,2,12.
Root will be 5.

Step 5
Postorder: 9, 1, 2, 12, 7, 5,3, 11, 4, 8. (Last element in Postorder traversal will be 7.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have 1, and Right Subtree will have 2,12.
Root will be 7.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Step 6
Postorder: 9, 1, 2, 12, 7, 5,3, 11, 4, 8. (Last element in Postorder traversal will be 12.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have 2 and Right Subtree will have nothing.
Root will be 12.

Step 7: -
Postorder: 9, 1, 2, 12, 7, 5,3, 11, 4, 8. (Last element in Postorder traversal will be 2.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have nothing and Right Subtree will have nothing.
Root will be 2.

Step 8
Postorder: 9, 1, 2, 12, 7, 5,3, 11, 4, 8. (Last element in Postorder traversal will be 1.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have nothing, and Right Subtree will have nothing.
Root will be 1.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Step 9
Postorder:9, 1, 2, 12, 7, 5,3, 11, 4, 8. (Last element in Postorder traversal will be 9.)
In order: 9, 5, 1, 7, 2, 12, 8, 4,3, 11.
Left Subtree will have nothing, and Right Subtree will have nothing.
Root will be 9.

Algorithm for building Tree from Postorder and Inorder Traversal


ALGORITHM Build_Tree(Postorder [], Inorder [], n)
BEGIN:
PostStart = 0
PostEnd = n-1 // n is number of elements
InorderStart = 0
InorderEnd = n-1 // n is number of elements
RETURNConstruct (Inorder, Postorder, InorderStart, InorderEnd, PostStart, PostEnd)
END;
ALGORITHM Construct (Inorder[], Postorder[], InorderStart, InorderEnd, PostStart, PostEnd)
BEGIN:
IF Poststart>PostEnd || InorderStart>InorderEnd THEN
RETURN NULL
Val = Postorder [Postend];
P=GetNode()
FOR i=0 TO Inorder.length()DO
IF Val == inorder[i] THEN
k=i
BREAK
P→left = Construct (Inorder,Postorder, InorderStart, k-1, PostStart, PostStart + k - (Inorderstart+1))
P→right = construct (Inorder, Postorder, k+1, InorderEnd, Poststart +k –InorderStart, PostEnd -1)
RETURN P
END;
Complexity
Time Complexity: - O (n2)

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Space Complexity: - O(n)

Binary Search Tree:

A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −
 The value of the key of the left sub-tree is less than the value of its parent (root) node's key.
 The value of the key of the right sub-tree is greater than or equal to the value of its parent (root)
node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can
be defined as −
left_subtree (keys) < node (key) ≤ right_subtree (keys)

Linked list Representation of BST :


In linked representation, BST is a collection of nodes where each node has at least four fields.
Left address Field Address to Parent Data Field Right address Field
node

a) Left address Field contains the address of the left child of the node.
b) Address to the Parent node contains the address of the parent node.
c) Data Field contains the actual value to be stored in the node.
d) The right address Field contains the address of the right child of the node.

Opertaions in BST:

(a) Searching
(b) Insertion
(c) Deletion
Searching:

The algorithm depends on the property of BST that if each left subtree has values below root and
each right subtree has values above the root.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

If the value is below the root, we can say for sure that the value is not in the right subtree; we need to
only search in the left subtree and if the value is above the root, we can say for sure that the value is
not in the left subtree; we need to only search in the right subtree.

Algorithm:
BST_Search(root)
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return BST_Search(root->left)
If number > root->data
return BST_Search(root->right)
Let us try to visualize this with a diagram.

4 is not found so, traverse through the left subtree of 8

4 is not found so, traverse through the right subtree of 3

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

4 is not found so, traverse through the left subtree of 6

4 is found

If the value is found, we return the value so that it gets propagated in each recursion step as shown in
the image below.

If you might have noticed, we have called return search(struct node*) four times. When we return
either the new node or NULL, the value gets returned again and again until search(root) returns the
final result.

If the value is found in any of the subtrees, it is


propagated up so that in the end it is returned, otherwise null is returned

If the value is not found, we eventually reach the left or right child of a leaf node which is NULL and it
gets propagated and returned.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Insert Operation

Inserting a value in the correct position is similar to searching because we try to maintain the rule that
the left subtree is lesser than root and the right subtree is larger than root. We keep going to either
right subtree or left subtree depending on the value and when we reach a point left or right subtree is
null, we put the new node there.

Algorithm:
BST_Insert(root, data)
If node == NULL
return createNode(data)
if (data < node->data)
node->left = BST_Insert(node->left, data);
else if (data > node->data)
node->right = BST_Insert(node->right, data);
return node;
The algorithm isn't as simple as it looks. Let's try to visualize how we add a number to an existing
BST.

4<8 so, transverse through the left child of 8

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

4>3 so, transverse through the right child of 8

4<6 so, transverse through the left child of 6

Insert 4 as a left child of 6

We have attached the node but we still have to exit from the function without doing any damage to
the rest of the tree. This is where the return node; at the end comes in handy. In the case of NULL,
the newly created node is returned and attached to the parent node, otherwise the same node is
returned without any change as we go up until we return to the root.
This makes sure that as we move back up the tree, the other node connections aren't changed.

Image showing the importance of returning the root element at the end so that the elements don't lose
their position during the upward recursion step.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Deletion Operation

There are three cases for deleting a node from a binary search tree.

Case I

In the first case, the node to be deleted is the leaf node. In such a case, simply delete the node from
the tree.

4 is to be deleted

Delete the node

Case II

In the second case, the node to be deleted lies has a single child node. In such a case follow the
steps below:

1. Replace that node with its child node.

2. Remove the child node from its original position.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

6 is to be deleted

copy the value of its child to the node and delete the child

Final tree
Case III

In the third case, the node to be deleted has two children. In such a case follow the steps below:

1. Get the inorder successor of that node.

2. Replace the node with the inorder successor.

3. Remove the inorder successor from its original position.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

3 is to be deleted
Copy the value of the inorder successor (4) to the node

Delete the inorder successor

ALGORITHM BST_Delete(P)
BEGIN:
//Case 1 When Node to be deleted has no child.
IF P→Left== NULL && P→Right == NULL THEN
Q = P→Father
IF Q == NULL THEN //node to be deleted is root node
ROOT = NULL
ELSE
IF IsLeft(P)==True THEN //node to be deleted is left or right child of Q→Left == NULL its parent
ELSE
Q→Right == NULL
FreeNode(P)
ELSE //Case 2 When Node to be deleted has one child.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

IF P→Left== NULL || P→Right == NULL THEN


IF P→Left == NULL THEN
Child = P→Right
ELSE
Child = P→Left
Q = P→Father
IF Q == NULL THEN //node to be deleted is root node ROOT = Child
ELSE
IF IsLeft(P) == True THEN
Q→Left = Child
ELSE
Q→Right = Child
Child→Father = Q
FreeNode(P)
ELSE //Case 3 When Node to be deleted has two children.
SUCCN = Inorder_SuccessorBST(P)
P→Info = SUCCN→Info //Replace the value of the node to be deleted with
value of its successor node
BST_Delete(SUCCN) // Delete the successor node
END;

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.

Types of Threaded Binary Tree

There are two types of threaded Binary Tree:

o One-way threaded Binary Tree


o Two-way threaded Binary Tree

One-way threaded Binary trees:

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

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
in order 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. Left threaded binary trees are used less often as they don't yield the last advantages of right
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.

The above figure shows the inorder traversal of this binary tree yields D, B, E, A, C, F. When this tree
is represented as a right threaded binary tree, the right link field of leaf node D which contains a NULL
value is replaced with a thread that points to node B which is the inorder successor of a node D. In the
same way other nodes containing values in the right link field will contain NULL value.

Two-way threaded Binary Trees:

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

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.

The above figure shows the inorder traversal of this binary tree yields D, B, E, G, A, C, F. If we consider
the two-way threaded Binary tree, the node E whose left field contains NULL is replaced by a thread
pointing to its inorder predecessor i.e. node B. Similarly, for node G whose right and left linked fields
contain NULL values are replaced by threads such that right link field points to its inorder successor
and left link field points to its inorder predecessor. In the same way, other nodes containing NULL
values in their link fields are filled with threads.

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. The header
node does not contain any data part and its left link field points to the root node and its right link field
points to itself. If this header node is included in the two-way threaded Binary tree then this node
becomes the inorder predecessor of the first node and inorder successor of the last node. Now threads
of left link fields of the first node and right link fields of the last node will point to the header node.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Advantages of Threaded Binary Tree:

o In threaded binary tree, linear and fast traversal of nodes in the tree so there is no requirement
of stack. If the stack is used then it consumes a lot of memory and time.
o It is more general as one can efficiently determine the successor and predecessor of any node
by simply following the thread and links. It almost behaves like a circular linked list.

Disadvantages of Threaded Binary Tree:

o When implemented, the threaded binary tree needs to maintain the extra information for each
node to indicate whether the link field of each node points to an ordinary node or the node's
successor and predecessor.
o Insertion into and deletion from a threaded binary tree are more time consuming since both
threads and ordinary links need to be maintained.

Inorder Traversal of a Threaded Binary Tree

For fully threaded binary tree, each node has five fields. Three fields like normal binary tree node,
another two fields to store Boolean value to denote whether link of that side is actual link or thread.

Left Thread Flag Left Link Data Right Link Right Thread Flag

This is the fully threaded binary tree

inorder():
Begin
temp := root
repeat infinitely, do
p := temp

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

temp = right of temp


if right flag of p is false, then
while left flag of temp is not null, do
temp := left of temp
done
end if
if temp and root are same, then
break
end if
print key of temp
done
End

Huffman Coding

Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length
codes to input characters, lengths of the assigned codes are based on the frequencies of
corresponding characters. The most frequent character gets the smallest code and the least
frequent character gets the largest code.
The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit
sequences) are assigned in such a way that the code assigned to one character is not the prefix of
code assigned to any other character. This is how Huffman Coding makes sure that there is no
ambiguity when decoding the generated bitstream.
Let us understand prefix codes with a counter example. Let there be four characters a, b, c and d,
and their corresponding variable length codes be 00, 01, 0 and 1. This coding leads to ambiguity
because code assigned to c is the prefix of codes assigned to a and b. If the compressed bit stream
is 0001, the de-compressed output may be “cccd” or “ccb” or “acd” or “ab”.
See this for applications of Huffman Coding.
There are mainly two major parts in Huffman Coding
1. Build a Huffman Tree from input characters.
2. Traverse the Huffman Tree and assign codes to characters.

Steps to build Huffman Tree


Input is an array of unique characters along with their frequency of occurrences and output is
Huffman Tree.

1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min Heap is
used as a priority queue. The value of frequency field is used to compare two nodes in min
heap. Initially, the least frequent character is at root)
2. Extract two nodes with the minimum frequency from the min heap.

3. Create a new internal node with a frequency equal to the sum of the two nodes frequencies.
Make the first extracted node as its left child and the other extracted node as its right child. Add
this node to the min heap.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root
node and the tree is complete.
Let us understand the algorithm with an example:
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Step 1. Build a min heap that contains 6 nodes where each node represents root of a tree with
single node.
Step 2 Extract two minimum frequency nodes from min heap. Add a new internal node with
frequency 5 + 9 = 14.

Now min heap contains 5 nodes where 4 nodes are roots of trees with single element each, and
one heap node is root of tree with 3 elements
character Frequency
c 12
d 13
Internal Node 14
e 16
f 45
Step 3: Extract two minimum frequency nodes from heap. Add a new internal node with frequency
12 + 13 = 25

Now min heap contains 4 nodes where 2 nodes are roots of trees with single element each, and two
heap nodes are root of tree with more than one nodes

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Step 4: Extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 =
30

Now min heap contains 3 nodes.


character Frequency
Internal Node 25
Internal Node 30
f 45
Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 =
55

Now min heap contains 2 nodes.


character Frequency
f 45
Internal Node 55
Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 =
100

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Now min heap contains only one node.


character Frequency
Internal Node 100
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman Tree:
Traverse the tree formed starting from the root. Maintain an auxiliary array. While moving to the left
child, write 0 to the array. While moving to the right child, write 1 to the array. Print the array when a
leaf node is encountered.

The codes are as follows:


character code-word
f 0
c 100
d 101
a 1100
b 1101
e 111

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.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

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.

Balance Factor (k) = height (left(k)) - height (right(k))

If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-
tree.

If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.

If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-
tree.

An AVL tree is given in the following figure. We can see that, balance factor associated with each node
is in between -1 and +1. therefore, it is an example of AVL tree.

Complexity

Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Operations on AVL tree

Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are performed in
the same way as they are performed in a binary search tree. Searching and traversing do not lead to
the violation in property of AVL tree. However, insertion and deletion are the operations which can
violate this property and therefore, they need to be revisited.

SN Operation Description

1 Insertion Insertion in AVL tree 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. The tree can be balanced by
applying rotations.

2 Deletion Deletion can also be performed in the same way as it is performed in a binary
search tree. Deletion may also disturb the balance of the tree therefore,
various types of rotations are used to rebalance the tree.

Why AVL Tree?

AVL tree controls the height of the binary search tree by not letting it to be skewed. The time taken for
all operations in a binary search tree of height h is O(h). However, it can be extended to O(n) if the BST
becomes skewed (i.e. worst case). By limiting this height to log n, AVL tree imposes an upper bound
on each operation to be O(log n) where n is the number of nodes.

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. For a tree to be unbalanced, minimum height must be at least 2, Let us understand each
rotation

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

1. RR Rotation

When BST becomes unbalanced, due to a node is inserted into the right subtree of the right subtree of
A, then we perform RR rotation, RR rotation is an anticlockwise rotation, which is applied on the edge
below a node having balance factor -2

In above example, node A has balance factor -2 because a node C is inserted in the right subtree of A
right subtree. We perform the RR rotation on the edge below A.

2. LL Rotation

When BST becomes unbalanced, due to a node is inserted into the left subtree of the left subtree of C,
then we perform LL rotation, LL rotation is clockwise rotation, which is applied on the edge below a
node having balance factor 2.

In above example, node C has balance factor 2 because a node A is inserted in the left subtree of C
left subtree. We perform the LL rotation on the edge below A.

3. LR Rotation

Double rotations are bit tougher than single rotation which has already explained above. LR rotation =
RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and then LL rotation is performed
on full tree, by full tree we mean the first node from the path of inserted node whose balance factor is
other than -1, 0, or 1.

Let us understand each and every step very clearly:

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

S Action
t
a
t
e

A node B has been inserted into the right subtree of A the left subtree of
C, because of which C has become an unbalanced node having balance
factor 2. This case is L R rotation where: Inserted node is in the right
subtree of left subtree of C

As LR rotation = RR + LL rotation, hence RR (anticlockwise) on subtree


rooted at A is performed first. By doing RR rotation, node A, has become
the left subtree of B.

After performing RR rotation, node C is still unbalanced, i.e., having balance


factor 2, as inserted node A is in the left of left of C

Now we perform LL clockwise rotation on full tree, i.e. on node C.


node C has now become the right subtree of node B, A is left subtree of B

Balance factor of each node is now either -1, 0, or 1, i.e. BST is balanced
now.

4. RL Rotation

As already discussed, that double rotations are bit tougher than single rotation which has already
explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is performed on subtree
and then RR rotation is performed on full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

S Action
t
a
t
e

A node B has been inserted into the left subtree of C the right subtree of A,
because of which A has become an unbalanced node having balance factor -
2. This case is RL rotation where: Inserted node is in the left subtree of right
subtree of A

As RL rotation = LL rotation + RR rotation, hence, LL (clockwise) on subtree


rooted at C is performed first. By doing RR rotation, node C has become the
right subtree of B.

After performing LL rotation, node A is still unbalanced, i.e. having balance


factor -2, which is because of the right-subtree of the right-subtree node A.

Now we perform RR rotation (anticlockwise rotation) on full tree, i.e. on node


A. node C has now become the right subtree of node B, and node A has
become the left subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e., BST is balanced now.

AVL-Algorithms
Creation of Node
ALGORITHM GetNode()

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

BEGIN:
P→Left = NULL
P→Right = NULL
P→Father = NULL
RETURN P
END;
Rotate Left
ALGORITHM RotateLeft (P)
BEGIN:
Q=P→RIGHT
R=Q→LEFT
Q→LEFT=P
P→RIGHT=R
RETURN Q
END;
Rotate Right
ALGORITHM RotateRIGHT( P)
BEGIN:
Q=P→LEFT
R=Q→RIGHT
Q→RIGHT=P
P→LEFT=R
RETURN Q
END;
Rotation LL
ALGORITHM LL( P)
BEGIN:
Q=RotateRIGHT(P)
RETURN Q
END;
Rotation RR
ALGORITHM RR( P)
BEGIN:
Q=RotateLEFT(P)
RETURN Q
END;
Rotation LR
ALGORITHM LR (P)
BEGIN:
Q=P→LEFT
R=RotateLEFT(Q)
P→LEFT=R
R=RotateRIGHT(P)
RETURN R
END;
Rotation RL
ALGORITHM RL(P)
BEGIN:
Q=P→RIGHT
R=RotateRIGHT(Q)
P->RIGHT=R
R=RotateLEFT(P)

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

RETURN R
END;

B Tree

B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order m can
have at most m-1 keys and m children. One of the main reason of using B tree is its capability to store
large number of keys in a single node and large key values by keeping the height of the tree relatively
small.

A B tree of order m contains all the properties of an M way tree. In addition, it contains the following
properties.

1. For each node x, the keys are stored in increasing order.


2. In each node, there is a boolean value x.leaf which is true if x is a leaf.
3. If n is the order of the tree, each internal node can contain at most n - 1 keys along with a pointer to
each child.
4. Each node except root can have at most n children and at least n/2 children.
5. All leaves have the same depth (i.e. height-h of the tree).

6. The root has at least 2 children and contains a minimum of 1 key.

7. If n ≥ 1, then for any n-key B-tree of height h and minimum degree t ≥ 2, h ≥ logt (n+1)/2.

It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.

A B tree of order 4 is shown in the following image.

Operations
Searching :

Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49
in the following B Tree. The process will something like following :

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.

Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n) time to
search any element in a B tree.

Inserting

Insertions are done at the leaf node level. The following algorithm needs to be followed in order to insert
an item into B Tree.

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 follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it too by following the same
steps.

Example:

Insert the node 8 into the B Tree of order 5 shown in the following image.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

8 will be inserted to the right of 5, therefore insert 8.

The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore split the node from the
median i.e. 8 and push it up to its parent node shown as follows.

Deletion

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. Following algorithm needs to be followed in order to delete a node from a B tree.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from
eight or left sibling.
o If the left sibling contains more than m/2 elements then push its largest element up to its
parent and move the intervening element down to the node where the key is deleted.
o If the right sibling contains more than m/2 elements then push its smallest element up to
the parent and move intervening element down to the node where the key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf node by joining
two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent too.

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.

Example 1

Delete the node 53 from the B Tree of order 5 shown in the following figure.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

53 is present in the right child of element 49. Delete it

Now, 57 is the only element which is left in the node, the minimum number of elements that must be
present in a B tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are also
not sufficient therefore, merge it with the left sibling and intervening element of parent i.e. 49.

The final B tree is shown as follows.

Application of B tree

B tree is used to index the data and provides fast access to the actual data stored on the disks since,
the access to value stored in a large database that is stored on a disk is a very time consuming process.

Searching an un-indexed and unsorted database containing n key values needs O(n) running time in
worst case. However, if we use B Tree to index this database, it will be searched in O(log n) time in
worst case.

Binary Heap:
A binary heap is a data structure, which looks similar to a complete binary tree. Heap data structure
obeys ordering properties discussed below. Generally, a Heap is represented by an array. In this
chapter, we are representing a heap by H.
As the elements of a heap is stored in an array, considering the starting index as 1, the position of the
parent node of ith element can be found at ⌊ i/2 ⌋ . Left child and right child of ith node is at
position 2i and 2i + 1.
A binary heap can be classified further as either a max-heap or a min-heap based on the ordering
property.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)


lOMoARcPSD|37334717

Max-Heap
In this heap, the key value of a node is greater than or equal to the key value of the highest child.
Hence, H[Parent(i)] ≥ H[i]

Min-Heap
In mean-heap, the key value of a node is lesser than or equal to the key value of the lowest child.
Hence, H[Parent(i)] ≤ H[i]
In this context, basic operations are shown below with respect to Max-Heap. Insertion and deletion of
elements in and from heaps need rearrangement of elements. Hence, Heapify function needs to be
called.

Downloaded by vodnala srujana (vodnalasrujana29@gmail.com)

You might also like