CD3291 -DATA STRUCTURES -UNIT 4 -NOTES
CD3291 -DATA STRUCTURES -UNIT 4 -NOTES
Tree ADT – Binary Tree ADT – tree traversals – binary search trees – AVL trees – heaps –
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.
A binary tree is a tree in which no node can have more than two children. In a binary tree, the
topmost element is called root node, and each node has 0, 1 or at the most 2 children.
Types of Binary Tree
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.
Strictly binary tree: It is a tree in which every node in the tree has either 0 or 2 children.
Traversing a binary tree is the process of visiting a node in the binary tree exactly once in a
systematic way. There are three types of tree traversal
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("Preorder traversal");
printPreorder(root);
printf("Inorder traversal");
printInorder(root);
printf("Postorder traversal");
printPostorder(root);
return 0;
}
Breadth First or Level Order Traversal: In a breadth-first traversal, the processing proceeds
horizontally form the root to all its children, then to its children’s children, and so forth until all
nodes have been processed. In other words, in breadth traversal, each level is completely
processed before the next level is started.
Example: Breadth First Traversal for the above given figure is 1 2 3 4 5.
printGivenLevel(tree, level)
if level is 1, then
print(tree->data);
printGivenLevel(tree->left, level-1);
printGivenLevel(tree->right, level-1);
Applications of Trees
Expression tree is a binary tree in which Leaf nodes are operands and internal nodes are
operators.
*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 on a binary search tree require comparisons between nodes. The following are the
operations that are being done in Binary Search Trees
➢ Searching - Find Min or Find Max .
➢ Insertion.
➢ Deletion.
➢ Sorting.
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.
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);
}
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.
If(T== NULL) {
/* create and return a one node tree*/ T=malloc(sizeof(structtreenode)); If(T==NULL)
Fatalerror( Out of Space );
Else {
T-->element=X;
T-->left=T-->right=NULL;
}
}
Else if(x<T-->element) T-->left=insert(X,T-->left);
Else if(X>=T-->left) T-->right=insert(X,T-->right); Return T;
}
Delete
The hardest operation is deletion in BST. It can be done in 3 ways.
Case 1: Deleting a leaf node: If the node is a leaf, it can be deleted immediately.
Steps are
1. Search the parent of the leaf node and make the link to the leaf node as NULL.
2. Release the memory of the deleted node.
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:
A B tree of order m contains all the properties of an M way tree. In addition, it contains the
following properties.
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.
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
B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search
operations.
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.
AVL Trees
AVL trees, Named after their inventor Adelson, Velski & Landis are height balancing binary
search tree. AVL tree checks the height of the left and the right sub-trees and assures that the
difference is not more than 1. This difference is called the Balance Factor.
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 keys present at all
of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
Max Heap
In a Max-Heap the key present at the root node must be greatest among the keys present at all of
it’s 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:
This is the process of removing the highest priority value from the binary heap. The deleting an
element must ensure
Deletion Procedure
The process of moving the empty spot down the heap is called percolate down.
Applications of Heap
1) Heap Sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
2) Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it
supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time.
Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform
union also efficiently.
3) Graph Algorithms: The priority queues are especially used in Graph Algorithms like
Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree.
4) Many problems can be efficiently solved using Heaps. See following for example.