Professional Documents
Culture Documents
MODULE – 5
printf(“ TREES ”);
Introduction:
Data Structures such as arrays, linked list, stack, and queue are linear data structures that
store data sequentially. In order to perform any operation in a linear data structure, the time
complexity increases with the increase in the data size. So it is difficult to use linear data
structures for large amount of data. To overcome the problem we are going to use non
linear data structures such as trees.
Definition :
Trees are non linear data structure that can be represented in a hierarchical manner.
A tree contains a finite non-empty set of elements. Any two nodes in the tree are connected
with a relationship of parent-child. Every individual elements in a tree can have any
number of sub trees.
Tree Terminology:
Parent : a node is said to be parent node to all its child nodes.
Leaf : a node that has no child nodes.
Siblings : Two nodes are siblings if they are children to the same parent node.
Degree of node: Number of sub trees of a given node
Ex: Degree of A=3,E=2
Root : The basic node of all nodes in the tree. All operations on the tree are performed with passing root
node to the functions. The first or top data item in hierarchical arrangement.
Child : a successor node connected to a node is called child. A node in binary tree may have at most two
children.
Degree of a tree: maximum degree of a node in a tree
Ex: Degree of above tree is 3
Depth or Height: Maximum level number of a node+1
Ex: Depth of above tree is 3+1=4
Non-terminal node: Any node except root whose degree is not zero .
Forest: Set of disjoint trees
Internal nodes: All nodes those have children nodes are called as internal nodes.
Path: Sequence of consecutive edges from the source node to the destination node
Applications :
1. Store hierarchical data, like folder structure, organization structure, XML/HTML data.
2. Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also allows
finding closest item
3. Trees can be used to represent real-world constructions like sentences or mathematical
expressions.
4. A tree is used to represent a file system.
Binary Tree:
A binary tree is a tree like structure that is rooted and in which each node has at
most two children and each child of a node is designated as its left or right child.
In this the maximum degree of any node is at most 2.
Number of nodes on each level i of binary tree is at most 2 power i
We can create binary tree using double linked list
Types of Binary Tree:
Complete Binary Tree: If all levels of tree are completely filled except the last level and the last level
has all keys as left as possible, is said to be a Complete Binary Tree.
Complete binary tree is also called as Perfect Binary Tree.
In a complete binary tree, every internal node has exactly two children and all leaf nodes are at same
level.
For example, at Level 2, there must be 22 = 4 nodes and at Level 3 there must be 23 = 8 nodes.
Fully Binary Tree: If every non terminal node in a binary tree consist of non empty
left sub tree and right sub tree. In other words if any node of a binary tree has either 0
or 2 child nodes then such tree is know as Fully binary tree . Fully binary tree also
know as strictly binary tree. Full binary tree is used to represent mathematical
expressions.
Skewed Binary Tree
If a tree which is dominated by left child node or right child node, is said to be a Skewed
Binary Tree.
In a skewed binary tree, all nodes except one have only one child node. The remaining node
has no child.
In a left skewed tree, most of the nodes have the left child without corresponding right child.
In a right skewed tree, most of the nodes have the right child without corresponding left
child.
Binary Search tree: can be defined as a class of binary trees, in which the nodes are arranged
in a specific order. This is also called ordered binary tree.
In a binary search tree, the value of all the nodes in the left sub-tree is less than the value of the
root.
Similarly, value of all the nodes in the right sub-tree is greater than or equal to the value of the
root.
This rule will be recursively applied to all the left and right sub-trees of the root.
Example:
Construct the BST for the given numbers13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
Binary Tree Representation:
To represent a binary tree of depth 'n' using array representation, we need one dimensional array with a
maximum size of 2n + 1.
If the parent at location i then left child at 2i+1 and right child 2i+2
2. Linked Representation:
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
storing right child address.
Operations on Binary Search Tree:
1. Insertion
2. Deletion
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
Inorder 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.
LDR(Left-Data-Right)
D→B→E→A→F→C→G
Preorder Traversal:
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
DLR(Data-left-right)
A→B→D→E→C→F→G
Postorder 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.
LRD(Left-Right-Data)
D→E→B→F→G→C→A
Example :
Construct the Binary Search Tree and traversal
1) 25,36,20,40,22,10,5,12,28,38,48
2) 56,32,59,12,34,23,90,46,29,13
Example:
2. Inorder : 26,32,35,56,58,59,66
Preorder: 56,32,26,35,59,58,66
56
32 59
26 35 58 66
Binary Search Tree Implementation:
#include <stdio.h>
struct node* find_minimum(struct node *root)
#include <stdlib.h>
{
struct node
if(root == NULL)
{
return NULL;
int data;
else if(root->left != NULL)
struct node *right;
return find_minimum(root->left);
struct node *left;
return root;
};
}
struct node* search(struct node *root, int x)
{
if(root==NULL || root->data==x)
return root;
else if(x>root->data)
return search(root->right, x);
else
return search(root->left ,x);
}
struct node* new_node(int x) struct node* delete(struct node *root, int x)
{ {
struct node *p; if(root==NULL)
p = (struct node*)malloc(sizeof(struct node)); return NULL;
p->data = x; if (x>root->data)
p->left = NULL; root->right = delete(root->right, x);
p->right = NULL; else if(x<root->data)
root->left = delete(root->left, x);
return p; else
} {
struct node* insert(struct node *root, int x) //No Children
{ if(root->left==NULL && root->right==NULL)
if(root==NULL) {
return new_node(x); free(root);
else if(x>root->data) return NULL;
root->right = insert(root->right, x); }
else
root->left = insert(root->left ,x);
return root;
}
//One Child
else if(root->left==NULL || root->right==NULL)
{
struct node *temp;
if(root->left==NULL)
temp = root->right;
else
temp = root->left;
free(root);
return temp;
}
//Two Children
else
{
struct node *temp = find_minimum(root->right);
root->data = temp->data;
root->right = delete(root->right, temp->data);
}
}
return root;
}
void inorder(struct node *root) void preorder(struct node *root)
{ {
if(root!=NULL) if(root!=NULL)
{ {
inorder(root->left); printf(" %d ", root->data);
printf(" %d ", root->data); preorder(root->left);
inorder(root->right); preorder(root->right);
} }
} }
A node has been inserted into the left subtree of the right subtree. This
makes C an unbalanced node. These scenarios cause AVL tree to
perform left-right rotation.
We first perform the left rotation on the left subtree of C. This
makes A, the left subtree of B.
Node C is still unbalanced, however now, it is because of the left-
subtree of the left-subtree.
We shall now right-rotate the tree, making B the new root node of this
subtree. C now becomes the right subtree of its own left subtree.
A node has been inserted into the right subtree of the left subtree. This
makes A, an unbalanced node with balance factor 2.
Start with 50
Consider 12
12>1 accommodate next
Consider 8,2,25
So maximum capacity is 4 need to split for that
consider center key i.e 8
Consider 6
Insert in left part of 8 6 is greater than 1 and 2 so after 2
after insert14,28,17,7,52,16,48,68,3,26,29,53,55,45,67.