Unit IV
Unit IV
Prepared by:
Mr.M.Ganesan, Associate Professor/CSE
Dr.P.Iyappan, Associate Professor/CSE
UNIT – IV
TREES
Trees: Basic Tree Terminologies, Different types of Trees: Binary Tree, Threaded Binary Tree, Binary Search Tree,
Binary Tree Traversals, AVL Tree. Introduction to B-Tree and B+ Tree.
1. Define tree?
Tree is a non linear data structure. There is no linear relation between the data
items. It can be defined as finite set of more than one node.
There is a special node designated as root node.
The remaining nodes are partitioned into sub tree of a tree.
The below diagram shows the tree
7. Define degree?
The degree of a node is referred as the number of sub-trees of a particular node.
8. Define a binary tree?
A binary tree is tree, which has nodes either empty or not more than two child nodes each of which
may be a leaf node.
16. What are the tasks performed during inorder tree traversal?
Traverse the left subtree
Process the root node.
Traverse the right subtree
17. What are the tasks performed during postorder tree traversal?
Traverse the left subtree
Traverse the right subtree
Process the root node.
18. State the merits and demerits of linear representation of binary tree?
Merits:
Storage method is easy and can be easily implemented in arrays.
When the location of a parent /child node is known other one can be determined easily. It requires static memory
allocation so it is easily implemented in all programming language
Demerits:
Insertions and deletions in a node, taker an excessive amount of processing time due to data movement up
and down the array.
19. State the merits and demerits of linked representation of binary tree?
Merits:
Insertions and deletions in a node, involves no data movement except the re arrangement of pointers,
hence less processing time.
Demerits:
Given a node structure, it is difficult to determine its parent node.
Memory spaces are wasted for storing null pointers for the nodes, which have one or no subtrees. It
requires dynamic memory allocation, which is not possible in some programming languages.
20. What do you mean by general tree?
General tree is a tree with nodes having any number of children
21. What is the length of the path in a tree?
The path length of a tree is the sum of the levels of all the tree's nodes. The path length of a tree with N nodes is
the sum of the path lengths of the subtrees of its root plus N-1.
22. Define B+ tree indexing?
A B+ tree can be viewed as a B-tree in which each node contains only keys (not key-value pairs), and to
which an additional level is added at the bottom with linked leaves. The primary values of a B+ tree is in storing
data for efficient retrieval in a block-oriented storage context — in particular, file systems.
23. What are the applications of binary trees?
1. Binary search tree
2. Binary tries
3. Hash trees
4. Heaps
5. T-tree
6. Syntax tree
7. Huffman coding tree
24. What is the use of indexing technique?
Indexing is an auxiliary data structure which speeds up the record retrieval.
25. Compare B- tree and B+ tree?
a. A B+ tree is the form of a balanced tree in which every path from the root of the tree to a leaf of the
tree is the same length. B+ trees are good for searches but cause some overhead issues in wasted
space.
b. B-trees are similar to B+ trees but it allows search-key values to appear only once, eliminates
redundant storage of search keys. It is possible sometimes to find search-key value before reaching the
leaf node. Implementation is harder than B+ trees.
26. Differentiate binary tree and binary search tree?
a. Binary is a specialized form of tree with two child (left child and right child). It is simply representation
of data in tree structure.
b. Binary search tree is a special type of binary tree that follows the following condition:
i. Left child is smaller than its parent node
ii. Right child is greater than its parent node
27. Give some of the applications of B tree?
B tree is optimized for systems that read and write large blocks of data. It is commonly used in
databases and file systems.
B tree also optimizes costly disk accesses that are of concern when dealing with large data
sets.
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and
right subtrees cannot be more than one for all nodes.
The above tree is AVL because differences between heights of left and right subtrees for every node is
less than or equal to 1.
Example
Terminology
In a tree data structure, we use the following terminology...
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have a root node. We can say that
the root node is the origin of the tree data structure. In any tree, there must be only one root node. We never have
multiple root nodes in a tree.
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree with 'N' number of
nodes there will be a maximum of 'N-1' number of edges.
3. Parent
In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE. In simple words,
the node which has a branch from it to any other node is called a parent node. Parent node can also be defined as
"The node which has child / children".
1. Child
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In simple words, the
node which has a link from its parent node is called as child node. In a tree, any parent node can have any number
of child nodes. In a tree, all the nodes except root are child nodes.
2. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple words, the nodes
with the same parent are called Sibling nodes.
3. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple words, a leaf is a
node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a node with no child.
In a tree, leaf node is also called as 'Terminal' node.
3. EXPLAIN THE TREE TERMINOLOGIE INTERNAL NODES, DEGREE, LEVEL.
1. Internal Nodes
In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In simple words, an
internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be
Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-Terminal' nodes.
2. Degree
In a tree data structure, the total number of children of a node is called as DEGREE of that Node. In simple words,
the Degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree
is called as 'Degree of Tree'
3. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are at Level 1 and the
children of the nodes which are at Level 1 will be at Level 2 and so on... In simple words, in a tree each step from
top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step).
1. Height
In a tree data structure, the total number of edges from leaf node to a particular node in the longest path is called
as HEIGHT of that Node. In a tree, height of the root node is said to be height of the tree. In a tree, height of all
leaf nodes is '0'.
2. Depth
In a tree data structure, the total number of egdes from root node to a particular node is called as DEPTH of that
Node. In a tree, the total number of edges from root node to a leaf node in the longest path is said to be Depth of
the tree. In simple words, the highest depth of any leaf node in a tree is said to be depth of that tree. In a tree, depth
of the root node is '0'.
3. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as PATH between
that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J
has length 4.
4. Sub Tree
In a tree data structure, each child from a node forms a subtree recursively. Every child node will form a subtree
on its parent node.
5. WHAT IS BINARY TREE AND EXPLAIN THE STRICTLY BINARY TREE.
A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree
can have only 2 children, we typically name them the left and right child.
In Strictly Binary Tree, every non-leaf node contain non-empty left and right sub-trees. In other words, the degree
of every non-leaf node will always be 2. A strictly binary tree with n leaves, will have (2n - 1) nodes.
A Binary Tree is said to be a complete binary tree if all of the leaves are located at the same level d. A complete
binary tree is a binary tree that contains exactly 2^l nodes at each level between level 0 and d. The total number
of nodes in a complete binary tree with depth d is 2d+1-1 where leaf nodes are 2d while non-leaf nodes are 2d-1.
7. WHAT IS BINARY TREE AND EXPLAIN THE EXTENDED BINARY TREE
A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree
can have only 2 children, we typically name them the left and right child.
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
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
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
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
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
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
9. EXPLAIN B TREE.
B-Tree is a self-balancing search tree. In most of the other self-balancing search trees , it is assumed that
everything is in main memory. To understand the use of B-Trees, we must think of the huge amount of
data that cannot fit in main memory.
When the number of keys is high, the data is read from disk in the form of blocks. Disk access time is
very high compared to the main memory access time. The main idea of using B-Trees is to reduce the
number of disk accesses.
Most of the tree operations (search, insert, delete, max, min, ..etc ) require O(h) disk accesses where h is
the height of the tree. B-tree is a fat tree. The height of B-Trees is kept low by putting maximum possible
keys in a B-Tree node. Generally, the B-Tree node size is kept equal to the disk block size.
Since the height of the B-tree is low so total disk accesses for most of the operations are reduced
significantly compared to balanced Binary Search Trees like AVL Tree, Red-Black Tree, ..etc.
Time Complexity of B-Tree:
1. Search O(log n)
2. Insert O(log n)
SR. NO. ALGORITHM TIME COMPLEXITY
3. Delete O(log n)
Properties of B-Tree:
We can see in the above diagram that all the leaf nodes are at the same level and all non-leaf have no empty sub-
tree and have keys one less than the number of their children.
Diagram-I
The structure of the leaf nodes of a B+ tree of order ‘b’ is as follows:
1. Each leaf node is of the form :
<<K1, D1>, <K2, D2>, ….., <Kc-1, Dc-1>, Pnext>
where c <= b and each Di is a data pointer (i.e points to actual record in the disk whose key value is
Ki or to a disk file block containing that record) and, each Ki is a key value and, Pnext points to next leaf
node in the B+ tree (see diagram II for reference).
2. Every leaf node has : K1 < K2 < …. < Kc-1, c <= b
3. Each leaf node has at least \ceil(b/2) values.
4. All leaf nodes are at same level.
Diagram-II
Using the Pnext pointer it is viable to traverse all the leaf nodes, just like a linked list, thereby achieving ordered
access to the records stored in the disk.
A Diagram of B+ Tree –
10 Marks
Example LEVEL
The degree of a tree is the maximum degree of the nodes in the tree.
Nodes with the same parent are called siblings. Here D & E are all siblings. H & I are also siblings.
The level of a node is defined by initially letting the root be at level zero. If a node is al level l,
there its children are at level l+1.
The height or depth of a tree is defined to be the maximum level of any node in the tree.
A set of trees is called forest; if we remove the root of a tree we get a forest. In the above fig, if we
remove A, we get a forest with three trees.
FIG: Tree FIG: Forest (Sub-
trees) Properties of a Tree
1. Any node can be the root of the tree and each node in a tree has the property that there is exactly one path
connecting that node with every other node in the tree.
2. The tree in which the root is identified is called a rooted tree; a tree in which the root is not identified is
called a free tree.
Once the size of the array has been determined the following method is used to represent the tree.
1. Store the root in 1st location of the array.
2. If a node is in location n of the array store its left child at location 2n and its right child at (2n+1).
In c, arrays start at position 0; therefore instead of numbering the trees nodes from 1 to n, we number
them from 0 to n-1. The two child of a node at position P are in positions 2P+1 and 2P+2.
The following figure illustrates arrays that represent the almost complete binary trees.
0 1 2 3 4 5 6 7 8
A B C D E F G H
We can extend this array representation of almost complete binary trees to an array representation of
binary trees generally.
The following fig (A) illustrates binary tree and fig (B) illustrates the almost complete binary tree of
fig (a). Finally fig(C) illustrates the array implementation of the almost complete binary tree.
0 1 2 3 4 5 6 7 8 9 10 11 12
A B C F G H I
Fig (C) Array Representation
Advantages
1. Given a child node, its parent node can be determined immediately. If a child node is at location N in the
array, then its parent is at location N/2.
2. It can be implemented easily in languages in which only static memory allocation is directly available.
Disadvantages
1. Insertion or deletion of a node causes considerable data movement up and down the array, using an
excessive amount of processing time.
2. Wastage of memory due to partially filled trees.
0 D 0
0 H 0
Array Representation:
Each node contains info, left, right and father fields. The left, right and father fields of a node point
to the node’s left son, right son and father respectively.
Using the array implementation, we may declare,
#define NUMNODES 100 struct nodetype
{
int info; int left; int right; int father;
};
struct nodetype node[NUMNODES];
This representation is called linked array representation.
Under this representation,
info(p) would be implemented by reference node[p].info,
left(p) would be implemented by reference node[p].left,
right(p) would be implemented by reference node[p].right,
The operations,
isleft(p) can be implemented in terms of the operation left(p)
isright(p) can be implemented in terms of the operation right(p)
The above representation appears to be good for complete binary trees and wasteful for many other
binary trees. In addition, the insertion or deletion of nodes from the middle of a tree requires the
insertion of many nodes to reflect the change in level number of these nodes.
3. What is Binary Tree? Explain its operation in detail with example. (Nov
2012, April 2011, April 2012,April 2013)
Binary Tree:
A binary tree is a tree in which each node has atmost 2 childrens. (i.e) a node can have 0 child or 1 child
or 2 child. A node must not have more than 2 childrens.
Eg:
BINARY SEARCH TREE OPERATIONS:
The basic operation on a binary search tree(BST) include, creating a BST, inserting an element into
BST, deleting an element from BST, searching an element and printing element of BST in ascending
order.
The ADT specification of a BST:
ADT BST
{
Create BST() : Create an empty BST;
Insert(elt) : Insert elt into the BST;
Search(elt,x) : Search for the presence of element elt and
Set x=elt, return true if elt is found, else
return false.
FindMin() : Find minimum element;
FindMax() : Find maximum element;
Ordered Output() : Output elements of BST in ascending
order; Delete(elt,x) : Delete elt and set x = elt;
}
Inserting an element into Binary Search Tree
Algorithm InsertBST(int elt, NODE *T)
[ elt is the element to be inserted and T is the pointer to the root of the
tree] If (T= = NULL) then
Create a one-node tree and
return Else if (elt<key) then
InsertBST(elt,
Tlchild) Else
if(elt>key) then
InsertBST(elt, Trchild)
Else
“ element is
already exist return T
End
C coding to Insert element into a BST
struct node
{
int info;
struct node *lchild; struct node *rchild;
};
typedef stuct node NODE;
NODE *InsertBST(int elt, NODE *T)
{
if(T = = NULL)
{
T = (NODE *)malloc(sizeof(NODE)); if ( T = = NULL)
printf ( “ No memory error”); else
{
tinfo = elt; tlchild = NULL;
trchild = NULL;
}
}
else if ( elt < Tinfo)
tlchild = InsertBST(elt, tlchild); else if ( elt > Tinfo)
trchild = InsertBST( elt, trchild); return T;
}
In case null pointer(null left/righ chile) is reached, it is an indication of the absence of the node.
NODE * SearchBST(int elt, NODE *T)
{
if(T = = NULL)
return NULL;
if(T = = NULL)
printf(“ element not found \n”); else if ( elt < Tinfo)
Tlchild = DeleteBST(elt,
Tlchild); else if ( elt > Tinfo)
Trchild = DeleteBST(elt, Trchild); else
if(Tlchild && T rchild)
{
minElt = FindMin(Trchild); Tinfo = minEltinfo;
Trchild = DeleteBST(Tinfo, Trchild);
}
else
{
minElt = Tree;
if (Tlchild = = NULL) T = Trchild;
else if ( Trchild = = NULL) T = Tlchild;
Free (minElt);
}
return T;
}
Deletion:
Deletion is the process whereby a node is deleted from the tree. Only certain nodes in a binary tree
can be deleted easily.
(i.e) the node with 0 or 1 children can be deleted, but, the node with 2 childrens cannot be deleted easily
I. Inorder Traversal:
It follows the general strategy of Left-Root-Right. In this traversal, if T is not empty, we first traverse (in
order) the left sub-tree;
Then visit the root node of T and then traverse the right sub-
tree. Consider the binary tree given in the following figure.
Steps :
1. Traverse left subtree in inorder
4. Process root node
5. Traverse right subtree in inorder
Algorithm C Coding
void inorder_traversal ( NODE * T)
{
Algorithm inoder traversal (Bin-Tree T) if( T ! = NULL)
{
Begin inorder_traversal(T->lchild);
If ( not empty (T) ) then printf(“%d \t “, T-
>info); Begin inorder_traversal(T-
>rchild);
Inorder_traversal ( left subtree ( T ) ) }
Print ( info ( T ) ) / * process node */ }
Inorder_traversal ( right subtree ( T ) )
E
Algorithm C function
void preorder_traversal ( NODE * T)
{
Algorithm preorder traversal (Bin-Tree T) if( T ! = NULL)
{
Begin printf(“%d \t”, T->info);
If ( not empty (T) ) then preorder_traversal(T->lchild);
Begin preorder_traversal(T->rchild);
Print ( info ( T ) ) / * process node * / }
Preoder traversal (left subtree ( T ) ) }
Inorder traversal ( right subtree ( T ) )
End
End
Output is : A B C D E F
As we shall see in many application the set name is just the element at the root. The operation of
FIND(i) now becomes; determine the root of the tree containing element i. UNION(I, j) requires two trees
with roots I and j to be joined. We shall assume that the nodes in the trees are numbered 1 through n so that
the node index corresponds to the element index. Thus element 6 is represented by the node with index 6.
Consequently, each node needs only one field: The PARENT field to link to its parent. Root nodes have a
PARENT field of zero. Based on the above discussion our first attempt arriving at UNION, FIND algorithms
would result in the algorithms U and F below.
Algorithm for FIND / UNION operation
Procedure U(I, j)
//replace the disjoint sets with roots I and j, i!=j with their union//
PARENT(i) j
End U
Procedure F(i)
//Find the root j of the tree containing element i//
j i
while PARENT(j) > 0 do
j PARENT(j)
end
return(j)
end F
We can do much better if care is taken to avoid the creation of degenerate trees. In order to accomplish this
we shall use a Weighting Rule for UNION(I, j). If the number of nodes in tree I is less than the number of nodes
in tree j, then make j the parent of i, otherwise make i the parent of j. using this rule on the sequence of set
unions given before we obtain the trees on next page remember that the arqument of UNION must both be
roots. The time required to process all the n finds is only O(m) since in tis case the maximum level of any node
is 2. This, however, is not the worst case.
The maximum level for any node is [log n]+1. First let us see how easy it is to implement the weighting rule.
We need to know how many nodes there are in any tree. To do this easily, we maintain a count field in the
toot of every tree. If I is a root node, then COUNT(i) = number of nodes in that tree. The count can be
maintained in the PARENT field as a negative number. This is equivalent to using a one bit field to distinguish
a count from a pointer. No confusion is created as for all other nodes the PARENT is positive.
Procedure UNION(i, j)
Polynomials:
Definition:
45 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
A polynomial is the sum of terms where each term consists of variable, coefficient and
exponent.
The Polynomial Expression can be represented using Linked list by the following illustration,
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct list
{
int coef;
int pow;
struct list *next;
};
struct list *p1=NULL,*p2=NULL,*p=NULL;
46 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
scanf("%d", &node->pow);
while(p1->next || p2->next)
{
if(p1->next)
{
p->pow=p1->pow;
p->coef=p1->coef;
p1=p1->next;
}
if(p2->next)
{
p->pow=p2->pow;
p->coef=p2->coef;
p2=p2->next;
}
p->next=(struct list *)malloc(sizeof(struct list));
p=p->next;
p->next=NULL;
}
}
void main() // Main method
{
char ch;
clrscr();
do
{
p1=(struct list *)malloc(sizeof(struct list));
p2=(struct list *)malloc(sizeof(struct list));
p=(struct list *)malloc(sizeof(struct list));
48 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
printf("\n First No is:");
display(p1);
printf("\n Second No is:");
display(p2);
add(p1,p2,p);
printf("\n Addition of the polynomial is:");
display(p);
getch();
}
while(ch=='y' || ch=='Y');
OUTPUT:
50 Data Structures