Trees Data Structure
Trees Data Structure
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.
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
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.
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.
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.
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
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
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
Here, For Figure (b) n2=4, n0= n2+1= 4+1=5 Therefore, the total
number of leaf node=5
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).
• 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.
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.
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:
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
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)
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.)
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.
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.
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.
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)
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.
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 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 not found, we eventually reach the left or right child of a leaf node which is NULL and it
gets propagated and returned.
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.
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.
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
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:
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:
3 is to be deleted
Copy the value of the inorder successor (4) to the node
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.
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.
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.
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.
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.
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.
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
inorder():
Begin
temp := root
repeat infinitely, do
p := temp
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.
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.
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
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
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.
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
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
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.
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:
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
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.
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
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.
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
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()
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)
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.
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.
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 :
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.
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.
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.
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.
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.
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.