Professional Documents
Culture Documents
Data Structure UNIT 3 TREE
Data Structure UNIT 3 TREE
TREE
A tree is a collection of nodes. The collection can be empty; otherwise a tree
consists of a specially designed node called root, and one or more non empty
sub trees T1, T2, …, Tk, each of whose roots are connected by a directed edge
from root.
Root
The root is the first and top most nodes in the hierarchical arrangement of
data items. A node which does not have a parent node is called root node. In
the above tree, the root is A.
Node
Each data item present in the tree is called a node. It is the basic data
structures that specifies the data information and have links to other data items.
Example node A, B, …..,,
Leaf
A node which doesn‟t have children is called leaf or Terminal node. In the
above tree B, C, H, I, K, L, M, N, P, Q are leaf node.
Siblings
Children of the same parents are said to be siblings. In the above tree B,
C, D, E, F ,G are siblings, I,J are siblings, K, L, M are siblings, P, Q are
siblings.
Path
A path from node n1 to nk is defined as a sequence of nodes n1, n2, . . .
, nk such that ni is the parent of ni+1 for 1 <= i <= k.
There is exactly only one path from the root to each node. In the above
tree the path from A to P is A, E, J, P. Where A is the parent for E, E is the
parent of J, and J is the parent of P.
Length
The length is defined as the number of edges on the path. The length for
the path A to P is 3.
Degree
i. Degree of a node
The number of sub trees of a node is called its degree. In the above tree,
degree of A is 6, degree of F is 3, and degree of B is 0.
ii. Degree of a tree
The degree of the tree is the maximum degree of any node in the tree. In
the above tree, the degree of the tree is 6.
Level
The level of a node is defined by initially letting the root be at level one.
If a node is at level L then its children are at level L+1.
In the previous tree, the level of A is 1, level of B, C, D, E, F, G is 2,
level of H, I, J, K, L, M, N is 3, and the level of P, Q is 4.
Depth
For any node n, the depth of n is the length of the unique path from root
to n. For example, the depth of root A is 0, depth of c is 1, depth of H is 2, and
the depth of P is 3
Height
For any node n, the heig ht of the node n is the length of the longest path
from n to the leaf. The height of the leaf is zero. For example, the height of
node F is 1, and the height of E is 2.
Note
➢
The height of the tree is equal to the height of the root.
➢
Depth of the tree is equal to the height of the tree.
Terminal node
A node with degree zero is called a terminal node. Leaf is called the
terminal node because it has the degree 0. In the previous tree node B, C, H, I,
K, L, M, N, P, Q are terminal nodes.
Non – Terminal Node
Any node whose degree is non zero is called Non – Terminal node. In
the previous tree node A, D, E, F, G, J are non terminal nodes.
Edge
An edge is a condition line which connects two adjacent node of a tree.
The line drawn from one node to another node is called edge. If a tree has N
nodes, then there are N-1 edges. For example, the previous tree has 13 nodes
and 12 edges.
Interior node
A node is said to be an interior node only if it is between root and leaves.
All non terminal nodes except the root is called interior node. For example, D,
E, F, G, J are interior nodes.
Ancestor node
A node is said to be an ancestor of another node only if it is the parent of
that node or the parent of same ancestor of that node. For example, the ancestor
of node H are A and D, and the ancestor of node F is A.
Descendent node
A node is said to be a descendent of another node if it is the child of that
node or the child of some other descendent of that node. For example, the
descendent of F are K, L, M, and the descendent of E are I, J, P, Q.
BINARY TREE
Binary Tree is a special datastructure used for data storage purposes. A binary tree
has a special condition that each node can have a maximum of two children. A
binary tree has the benefits of both an ordered array and a linked list as search is as
quick as in a sorted array and insertion or deletion operation are as fast as in linked
list.
Important Terms
Following are the important terms with respect to tree.
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree and
one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its child
node.
• Leaf − The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root node is at level
0, then its next child node is at level 1, its grandchild is at level 2, and so on.
• keys − Key represents a value of a node based on which a search operation is to be
carried out for a node.
Implementation
A binary tree has at most two children; we can keep direct pointers to
them. The declaration of tree nodes is similar in structure to that for doubly
linked lists, in that a node is a structure consisting of the key information plus
two pointers (left and right) to other nodes.
Application of trees
1.Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business chess).
Other Application
1. Heap is a tree data structure which is implemented using arrays and used to
implement priority queues.
2. B-Tree and B+ Tree : They are used to implement indexing in databases.
3. Syntax Tree: Used in Compilers.
4. K-D Tree: A space partitioning tree used to organize points in K dimensional
space.
5. Trie : Used to implement dictionaries with prefix lookup.
6. Suffix Tree : For quick pattern searching in a fixed text.
A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
To represent a binary tree of depth 'n' using array representation, we need one dimensional
We use a double linked list to represent a binary tree. In a double linked list, every node consists
of three fields. First field for storing left child address, second for storing actual data and third for
The above example of the binary tree represented using Linked list representation is shown as
follows...
Binary search tree ADT
Binary search tree is a binary tree in which every node X in the tree, the values
of all the keys in its left sub tree are smaller than the key value in X, and the
values of all the keys in its right sub tree are larger than the key vale in X.
Comparison between binary tree and binary search tree
Delete
While deleting a node from a tree, the memory is to be released. Deletion
operation is the complex operation in the binary search tree. To delete a node
from the tree consider the following three possibilities.
Case 1: Node to be deleted is a leaf node
Case 2: Node with one child.
Case 3: Node with two children.
Case 1: Deleting the leaf node
➢
Search the parent of the leaf node.
➢
Make the parent link of the leaf node as NULL.
➢
Release Memory.
Example: Deletion of node 6
Search Operation
• Search operation is performed with O(log n) time complexity in a binary
search tree.
• This operation starts from the root node. It is used whenever an element is
to be searched.
The following algorithm shows the search operation in binary search
tree:
Step 2: Compare this element with the value of root node in a tree.
Step 3: If element and value are matching, display "Node is Found" and
terminate the function.
Step 4: If element and value are not matching, check whether an element is
smaller or larger than a node value.
Step 7: Repeat the same process until we found the exact element.
Step 9: If we reach to a leaf node and the search value is not match to a
leaf node, display "Element is not found" and terminate the function.
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −
• 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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rlink;
struct node *llink;
}*tmp=NULL;
typedef struct node NODE;
NODE *create();
void preorder(NODE *);
void inorder(NODE *);
void postorder(NODE *);
void insert(NODE *);
int main()
{
int n,i,ch;
do
{
printf("\n\n1.Create\n\n2.Insert\n\n3.Preorder\n\n4.Postorder\n\n5.I
norder\n\n6.Exit\n\n");
printf("\n\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
tmp=create();
break;
case 2:
insert(tmp);
break;
case 3:
printf("\n\nDisplay Tree in Preorder Traversal : ");
preorder(tmp);
break;
case 4:
printf("\n\nDisplay Tree in Postorder Traversal : ");
postorder(tmp);
break;
case 5:
printf("\n\nDisplay Tree in Inorder Traversal : ");
inorder(tmp);
break;
case 6:
exit(0);
default:
printf("\n Inavild Choice..");
}
}
while(n!=5);
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n\nEnter the Data ");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
void postorder(NODE *tmp)
{
if(tmp!=NULL)
{
postorder(tmp->llink);
postorder(tmp->rlink);
printf("%d->",tmp->data);
}
}
void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d->",tmp->data);
inorder(tmp->rlink);
}
}
void preorder(NODE *tmp)
{
if(tmp!=NULL)
{
printf("%d->",tmp->data);
preorder(tmp->llink);
preorder(tmp->rlink);
}
}
Expression Tree
Expression tree is a binary tree in which each internal node corresponds to operator
and each leaf node corresponds to operand so for example expression tree for 3 +
((5+9)*2) would be:
Inorder traversal of expression tree produces infix version of given postfix expression
(same with preorder traversal it gives prefix expression)
Construction of Expression Tree:
Now For constructing expression tree we use a stack. We loop through input expression and
do following for every character.
1) If character is operand push that into stack
2) If character is operator pop two values from stack make them its child and push current
node again.
At the end only element of stack will be root of expression tree.
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary
search tree but it is a balanced tree. A binary tree is said to be balanced if, the difference
between the heights of left and right subtrees of every node in the tree is either -1, 0 or +1. In
other words, a binary tree is said to be balanced if the height of left and right children of
every node differ by either -1, 0 or +1. In an AVL tree, every node maintains an extra
information known as balance factor. The AVL tree was introduced in the year 1962 by
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every
The above tree is a binary search tree and every node is satisfying balance factor condition. So
this tree is said to be an AVL tree
Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.
There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)
In LL Rotation, every node moves one position to left from the current position. To understand LL
Rotation, let us consider the following insertion operation in AVL Tree...
1. Search
2. Insertion
3. Deletion
• Step 4 - If both are not matched, then check whether search element is smaller or larger
than that node value.
• Step 5 - If search element is smaller, then continue the search process in left subtree.
• Step 6 - If search element is larger, then continue the search process in right subtree.
• Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node.
• Step 8 - If we reach to the node having the value equal to the search value, then display
"Element is found" and terminate the function.
• Step 9 - If we reach to the leaf node and if it is also not matched with the search element,
then display "Element is not found" and terminate the function.
• 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.
B - Tree Datastructure
In search trees like binary search tree, AVL Tree, Red-Black tree, etc., every
node contains only one value (key) and a maximum of two children. But
there is a special type of search tree called B-Tree in which a node contains
more than one value (key) and more than two children. B-Tree was
developed in the year 1972 by Bayer and McCreight with the
name Height Balanced m-way Search Tree. Later it was named as B-Tree.
For example, B-Tree of Order 4 contains a maximum of 3 key values in a node and maximum of
4 children for a node.
Example
Operations on a B-Tree
The following operations are performed on a B-Tree...
1. Search
2. Insertion
3. Deletion
Example
Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.
Red - Black Tree Datastructure
Red - Black Tree is another variant of Binary Search Tree in which every node is colored either
Red Black Tree is a Binary Search Tree in which every node is colored either RED or
BLACK.
Example
Following is a Red-Black Tree which is created by inserting numbers from 1 to 9.
The above tree is a Red-Black tree where every node is satisfying all the properties of Red-Black
Tree.
Every Red Black Tree is a binary search tree but every Binary Search Tree need not be
Red Black tree.
Insertion into RED BLACK Tree
In a Red-Black Tree, every new node must be inserted with the color RED. The insertion
operation in Red Black Tree is similar to insertion operation in Binary Search Tree. But it is
inserted with a color property. After every insertion operation, we need to check all the properties
of Red-Black Tree. If all the properties are satisfied then we go to next operation otherwise we
perform the following operation to make it Red Black Tree.
• 1. Recolor
• 2. Rotation
• 3. Rotation followed by Recolor
The insertion operation in Red Black tree is performed using the following steps...
Example
Deletion Operation in Red Black Tree
The deletion operation in Red-Black Tree is similar to deletion operation in BST. But after every
deletion operation, we need to check with the Red-Black Tree properties. If any of the properties
are violated then make suitable operations like Recolor, Rotation and Rotation followed by
Recolor to make it Red-Black Tree.
Splay tree is another variant of a binary search tree. In a splay tree, recently accessed element is
Splay Tree is a self - adjusted Binary Search Tree in which every operation on element
rearranges the tree so that the element is placed at the root position of the tree.
In a splay tree, every operation is performed at the root of the tree. All the operations in splay
tree are involved with a common operation called "Splaying".
Splaying an element, is the process of bringing it to the root position by performing
suitable rotation operations.
In a splay tree, splaying an element rearranges all the elements in the tree so that splayed
element is placed at the root of the tree.
By splaying elements we bring more frequently used elements closer to the root of the tree so
that any operation on those elements is performed quickly. That means the splaying operation
automatically brings more frequently used elements closer to the root of the tree.
Every operation on splay tree performs the splaying operation. For example, the insertion
operation first inserts the new element using the binary search tree insertion process, then the
newly inserted element is splayed so that it is placed at the root of the tree. The search operation
in a splay tree is nothing but searching the element using binary search process and then
splaying that searched element so that it is placed at the root of the tree.
In splay tree, to splay any element we use the following rotation operations...
Example
Zig Rotation
The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree rotations. In zig
rotation, every node moves one position to the right from its current position. Consider the
following example...
Zig-Zig Rotation
The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation, every node moves
two positions to the right from its current position. Consider the following example...
Zag-Zag Rotation
The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation, every node
moves two positions to the left from its current position. Consider the following example...
Zag-Zig Rotation
The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig rotation. In zag-
zig rotation, every node moves one position to the left followed by one position to the right from
its current position. Consider the following example...
Every Splay tree must be a binary search tree but it is need not to be balanced tree.
Tries
All the search trees are used to store the collection of numerical values but they are not suitable
for storing the collection of words or strings. Trie is a data structure which is used to store the
collection of strings and makes searching of a pattern in words more easy. The term trie came
from the word retrieval. Trie data structure makes retrieval of a string from the collection of
strings more easily. Trie is also called as Prefix Tree and some times Digital Tree. A trie is
defined as follows...
Trie is a tree like data structure used to store collection of strings.
The trie data structure provides fast pattern matching for string data values. Using trie, we bring
the search complexity of a string to the optimal limit. A trie searches a string in O(m) time
In trie, every node except the root stores a character value. Every node in trie can have one or a
number of children. All the children of a node are alphabetically ordered. If any two strings have a
Example
2-3 Trees | (Search and Insert)
2-3 tree is a tree data structure in which every internal node (non-leaf node) has
either one data element and two children or two data elements and three children. If
a node contains one data element leftVal, it has two subtrees (children)
namely left and middle. Whereas if a node contains two data
elements leftVal and rightVal, it has three subtrees namely left, middle and right.
The main advantage with 2-3 trees is that it is balanced in nature as opposed to a
binary search tree whose height in the worst case can be O(n). Due to this, the worst
case time-complexity of operations such as search, insertion and deletion is as the
height of a 2-3 tree is .
Search: To search a key K in given 2-3 tree T, we follow the following procedure:
Base cases:
1. If T is empty, return False (key cannot be found in the tree).
2. If current node contains data value which is equal to K, return True.
3. If we reach the leaf-node and it doesn’t contain the required key value K, return
False.
Recursive Calls:
1. If K < currentNode.leftVal, we explore the left subtree of the current node.
2. Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle
subtree of the current node.
3. Else if K > currentNode.rightVal, we explore the right subtree of the current
node.
Case 2: Insert in a node with two data elements whose parent contains only one data
element.
Case 3: Insert in a node with two data elements whose parent also contains two
data elements.