U4
U4
TREES STRUCTURES
Tree ADT - Binary Tree ADT – tree traversals - binary search tree - AVL Trees –Heap-
Multi-way search trees.
Tree
A Tree is a nonlinear data structure consists of one or more data nodes where
one node is designated as the root of the tree while the remaining nodes are
called as the children of the root.
In a general tree, A node can have any number of children nodes, but it can
have only a single parent.
The following image shows a tree, where the node A is the root node of the tree
while the other nodes can be seen as the children of A.
Basic terminology
1. Root Node:- The root node is the topmost node in the tree hierarchy which doesn't
have any parent.
2. Leaf Node or external node :- The node of tree, which doesn't have any child node,
is called leaf node.
3. Level : . Root node of the tree is always present at level 0. The level of the any other
child node is one more than that of the parent. Ex : level of Node F is 2.
4. Depth or Height : The maximum level of any node in the tree is called Depth or
Height. For the above tree Depth or height is 2.
5. Degree: The number of nodes connected to a particular node is called Degree. For Ex:
Degree of node B is 3. Degree of node E is 1.
6. Subtree or forest: The nodes other than the root node are partitioned into the non empty
sets called subtree.
If we delete the root and the edges connecting the root to the nodes at level 1, then we get the
Subtree with root as the node at level 1.
The tree T1, T2 and T3 is called sub-trees of the root node.
Full binary tree: It is a binary tree in which all interior nodes have two children and all leaves
have the same depth or same level.
Complete binary tree: It is a binary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible.
The number of internal nodes in a complete binary tree of n nodes is floor(n/2).
Degenarate tree: It is a tree is where each parent node has only one child node. It behaves like a
linked list. There are 2 types, Left Skewed and Right Skewed binary tree.
Uses of Inorder
In case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order.
Pre-order Traversal: (RoLR): In this technique, we do the following :
Process data of root node.
First, traverse left subtree completely.
Then, traverse right subtree.
Procedure for Preorder Traversal:
void printPreorder(struct node* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
printPreorder(node->left);
printPreorder(node->right);
}
Example: Preorder traversal for the above given figure is 1 2 4 5 3.
Uses of Preorder
Preorder traversal is used to create a copy of the tree.
Preorder traversal is also used to get prefix expression on of an expression tree.
Postorder traversal is also useful to get the postfix expression of an expression tree.
Main Program :
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{ struct node* node = (struct node*) malloc(sizeof(struct node)); node-
>data = data; node->left = NULL; node->right = NULL;
return(node);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Inorder traversal");
printInorder(root);
printGivenLevel(tree, level)
if level is 1, then
print(tree->data);
printGivenLevel(tree->left, level-1);
printGivenLevel(tree->right, level-1);
Applications of Trees
*For convenience, we will have the stack grow from left to right in the diagrams.
Next, a '+' is read, so two pointers to trees are popped, a new tree is formed, and a
pointer to it is pushed onto the stack.*
Next, c, d, and e are read, and for each a one-node tree is created and a pointer to
the corresponding tree is pushed onto the stack.
Continuing, a '*' is read, so we pop two tree pointers and form a new tree with a '*'
as root.
Finally, the last symbol is read, two trees are merged, and a pointer to the final tree
is left on the stack.
The Values in the left subtree must be smaller than the keyvalue to be inserted. The
Values in the right subtree must be larger than the keyvalue to be inserted.
OPERATIONS
Find
If the key stored at T is x, we can return T. Otherwise, we make a recursive call on
a subtree of T, either left or right, depending on the relationship of x to the key
stored in T.
Find operation for binary search trees
Position find(structtreenode T, intnum)
{
While(T!=NULL) {
if(num>T-->data) {
T=T-->right;
if(num<T-->data)
T=T-->left; }
else if(num< T-->data)
{
T=T-->left;
if(num>T-->data)
T=T-->right;
}
if(T-->data==num)
break; }
return T; }
To perform a Findmin, start at the root and go left as long as there is a left child. The
stopping point is the smallest element in the BST.
To perform a Findmax, start at the root note and go right as long as there is right
child. The stopping point is the largest element.
findmin(searchtree T)
{
if(T==NULL)
return NULL;
else if(T-->left==NULL)
return T;
else
return findmin(T-->left);
}
// Finding Maximum Position
findmax(searchtree T)
{
if(T==NULL)
return NULL;
else if(T-->right==NULL) return T;
else
return findmax(T-->right);
}
Insert
To insert x into tree T, proceed down with the following steps.
Delete
The hardest operation is deletion in BST. It can be done in 3 ways.
B-TREES
A B-tree is a balanced m- ordered multiway search tree, where m > 2. 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.
Properties of B Tree:
Searching :
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.
Insertions
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.
Insert the new element in the increasing order of elements.
Split the node into the two nodes at the median.
Push the median element upto its parent node.
If the parent node also contain m-1 number of keys, then split it too by
following the same steps.
Note:
If M is odd, split the node from the median. If M is even , then split as right-bias or
left bias.
right-bias: The node is split such that its right subtree has more keys than the left
subtree.
left-bias: The node is split such that its left subtree has more keys than the right
subtree.
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:
Insert the node 8 into the B Tree of order 5 shown in the following image
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.
Example 2:
Construct B Tree of order m=5 for the following keys 1, 12, 8, 2, 25, 5, 14, 28,
17, 7, 52, 16, 48, 68, 3, 26, 29, 53, 55, 45 . State the rules for deletion and Delete
the keys 8 and 55.
Ans:
Insertion:
Deletion:
B+ Tree
In B Tree, Keys and records both can be stored in the internal as well as leaf nodes.
Whereas, in B+ tree, records (data) can only be stored on the leaf nodes while
internal nodes can only store the key values.
The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to
make the search queries more efficient
Threaded Binary Trees- AVL Trees - Heap Applications of heap.
Threaded Binary Trees
The binary tree can have at most two children. But if the tree have only one
children, or no children, the link part in the linked list representation remains null.
In threaded binary tree representation, empty links are reused as threads.
Types are
1. Single threaded tree - (Left threaded and right threaded)
2. Fully threaded binary tree
Single Left threaded tree:
In this, if some node has no left child, then the left pointer will point to its inorder
predecessor. If no predecessor is present, then it will point to header node.
Left Thread Flag Left Link Data Right Link Right Thread Flag
Example: The first tree is balanced, and the next two trees are not balanced
In the second tree, the left subtree of C has height 2 and the right subtree has height
0, so the difference is 2. In the third tree, the right subtree of A has height 2 and the
left is missing, so it is 0, and the difference is 2 again. AVL tree permits difference
(balance factor) to be only 1.
If the difference in the height of left and right sub-trees is more than 1, the tree is
balanced using some rotation techniques.
1. Search
2. Insertion
3. Deletion
Step 1 - Insert the new element into the tree using Binary Search Tree
insertion logic.
Step 2 - After insertion, check the Balance Factor of every node.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for
next operation.
Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that
tree is said to be imbalanced. In this case, perform suitable
Rotation to make it balanced and go for next operation.
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
Deletion Operation in AVL Tree
The deletion operation in AVL Tree is similar to deletion operation in BST. But
after every deletion operation, we need to check with the Balance Factor condition.
If the tree is balanced after deletion go for next operation otherwise perform
suitable rotation to make the tree Balanced.
Heap
Heap data structure is a specialized binary tree-based data structure. In a heap data
structure, nodes are arranged based on their values. A heap data structure also called
as Binary Heap. There are two types of heap data structures.
1. Max Heap
2. Min Heap
Min Heap
In a Min-Heap the key present at the root node must be minimum among the for all
Max Heap
In a Max-Heap the key present at the root node must be greatest among the
children. The same property must be recursively true for
all sub-trees in that Binary Tree.
Operations on Max Heap
The following operations are performed on a Max heap data structure.
1. Finding Maximum
2. Insertion
3. Deletion
Insert into a heap the following values in order: 10,6,20,5, 16, 17, 13,2
We will use smaller values has higher priority as our priority ordering.
insert 10:
insert 6:
insert 20:
insert 5:
insert 16:
insert 17:
1) Heap Sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
4) Many problems can be efficiently solved using Heaps. See following for
example.
a) .
b) Sort an almost sorted array
c ) Merge K Sorted Arrays.
MULTI-WAY SEARCH TREES
We have discussed that every node in a binary search tree contains one value and two pointers, left and
right, which point to the node’s left and right sub-trees, respectively. The same concept is used in an M-
way search tree which has M – 1 values per node and M sub trees. In such a tree, M is called the
degree of the tree. Note that in a binary search tree M = 2, so it has one value and two sub-trees. In
other words, every internal node of an M-way search tree consists of pointers to M sub-trees and
have a maximum of m– 1 keys and m pointers to its sub-trees. B tree may contain a large
number of key values and pointers to sub- trees. Storing a large number of keys in a single
B tree is designed to store sorted data and allows search, insertion, and deletion operations to
children that each node can have) is a tree with all the properties of an M-way search tree.
2. Every node in the B tree except the root node and leaf nodes has at least (minimum) m/2 children.
3. The root node has at least two children if it is not a terminal (leaf) node.
4. All leaf nodes are at the same level.
Searching for an element in a B tree is similar to that in binary search trees. Consider the B tree
Step 1. we begin at the root node. The root node has a value 45 which is less than 59.
Step 2. So, we traverse in the right sub-tree. The right sub-tree of the root node has two key values,
49 and 63. Since 49 < 59 < 63,
Step 3. We traverse the right sub-tree of 49, that is, the left sub-tree of 63. This sub-tree has three
values, 54, 59, and 61. On finding the value 59, the search is successful.
The running time of the search operation depends upon the height of the tree, the algorithm to search
In a B tree, all insertions are done at the leaf node level. A new value is inserted in the B tree using
Procedure:
Step 1. Search the B tree to find the leaf node where the new key value should be inserted.
Step 2. If the leaf node is not full, that is, it contains less than m–1 key values, then insert
the new element in the node keeping the node’s elements ordered.
Step 3. If the leaf node is full, that is, the leaf node already contains m–1 key values, then
(a) insert the new value in order into the existing set of keys,
(b) split the node at its median into two nodes (note that the split nodes are half full), and
(c) push the median element up to its parent’s node. If the parent’s node is already full,
Example 1 Look at the B tree of order 5 given below and insert 8, 9, 39, and 4 into it.
Till now, we have easily inserted 8and 9 in the tree because the leaf nodes were not full. But now, the
node in which 39 should be inserted is already full as it contains four values. Here we split the nodes
to form two separate nodes. But before splitting, arrange the key values in order (including the new
value). The ordered set of values isgivenas21, 27, 36,39, and42. The median value is 36, so push 36
the nodes to form two separate nodes. But before splitting, we arrange the key values in order (including
the new value). The ordered set of values is given as 4, 7, 8, 9, and 11. The median value is 8, so we
push 8 into its parent’s node and split the leaf nodes. But again, we see that the parent’s node is already
Like insertion, deletion is also done from the leaf nodes. There are two cases of deletion. In the first
case, a leaf node has to be deleted. In the second case, an internal node has to be deleted. Let us first see
Step 2. If the leaf node contains more than the minimum number of key values (more than m/2
Step 3. Else if the leaf node does not contain m/2 elements, then fill the node by taking an
key into its parent’s node and pull down the intervening element from the parent
(b) Else, if the right sibling has more than the minimum number of key values, push its
smallest key into its parent node and pull down the intervening element from the
Step 4. Else, if both left and right siblings contain only the minimum number of elements, then
create a new leaf node by combining the two leaf nodes and the intervening element of the
parent
Example 1 Consider the following B-tree of order 5 and delete values 93, 201, 180, and 72 from it
Example 2 Consider the B tree of order 3 given below and perform the following operations:
Example 11.4 Create a B tree of order 5 by inserting the following elements: 3, 14, 7, 1, 8, 5, 11, 17, 13, 6,
23, 12, 20,
A B+ tree is a variant of a B tree which stores sorted data in a way that allows for efficient
insertion, retrieval, and removal of records, each of which is identified by a key. While a B tree
can store both keys and records in its interior nodes, a B+ tree, in contrast, stores all the records at
the leaf level of the tree; only keys are stored in the interior nodes.
The leaf nodes of a B+ tree are often linked to one another in a linked list. This has an added
Typically, B+ trees are used to store large amounts of data that cannot be stored in the main
memory. With B+ trees, the secondary storage (magnetic disk) is used to store the leaf nodes of
trees
and the
internal
nodes of
trees are
stored in
B+ trees store data only in the leaf nodes. All other nodes (internal nodes) are called index nodes
or i-nodes and store index values. This allows us to traverse the tree from
the root down to the leaf node that stores the desired data item. Figure shows a B+ tree of order
3. Many database systems are implemented using B+ tree structure because of its simplicity.
Since all the data appear in the leaf nodes and are ordered, the tree is always balanced and makes
A B+ tree can be thought of as a multi-level index in which the leaves make up a dense index and the
2. It can be used to perform a wide range of queries easily as leaves are linked to nodes at the upper
level
A new element is simply added in the leaf node if there is space for it. But if the data node in the
tree where insertion has to be done is full, then that node is split into two nodes. This calls for
adding a new index value in the parent index node so that future queries can arbitrate between
However, adding the new index value in the parent node may cause it, in turn, to split.
In fact, all the nodes on the path from a leaf to the root may split when a new value is added to a
leaf node. If the root node splits, a new leaf node is created and the tree grows by one level.
Step 2: If the leaf node overflows, split the node and copy the middle element to next
index node. Step 3: If the index node overflows, split that node and move the middle
element to next index page
Example 1. Consider the B+ tree of order 4 given and insert 33 in it.
As in B trees, deletion is always done from a leaf node. If deleting a data element leaves that node
empty, then the neighboring nodes are examined and merged with the under full node.
This process calls for the deletion of an index value from the parent index node which, in turn,
may cause it to become empty. Similar to the insertion process, deletion may cause a merge-
delete wave to run from a leaf node all the way up to the root. This leads to shrinking of the tree
by one level.
Step 2: If the leaf node underflows, merge that node with the sibling and delete the key in between them.
Step 3: If the index node underflows, merge that node with the sibling and move down the key in between
them.
Example 1: Consider the B+ tree of order 4 given below and delete node 15 from it.