Unit Iii: Tree Structures
Unit Iii: Tree Structures
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.
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.
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
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.
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);
then printGivenLevel(tree-
>left, level-1);
printGivenLevel(tree->right,
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.
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
Heap
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:
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.