Tree
Tree
1. The tree has one node called root. The tree originates from this, and
hence it does not have any parent.
2. Each node has one parent only but can have multiple children.
3. Each node is connected to its children via edge.
Terminology Description Example From Diagram
Node which does not have any child H, I, J, F and G are leaf
Leaf
is called as leaf nodes
Types of Trees
Types of trees depend on the number of children a node has. There are two
major tree types:
General Tree: A tree in which there is no restriction on the number of
children a node has, is called a General tree. Examples are Family tree,
Folder Structure.
Binary Tree: In a Binary tree, every node can have at most 2 children, left
and right. In diagram below, B & D are left children and C, E & F are
right children.
Full/ proper/ strict Binary tree
The full binary tree is also known as a strict binary tree. The tree can only
be considered as the full binary tree if each node must contain either 0 or 2
children. The full binary tree can also be defined as the tree in which each
node must contain 2 children except the leaf nodes.
The complete binary tree is a tree in which all the nodes are completely filled
except the last level. In the last level, all the nodes must be as left as
possible. In a complete binary tree, the nodes should be added from the left.
The degenerate binary tree is a tree in which all the internal nodes have only
one children. It is also known as a ”„skewed tree “.
2 3
4 6 7
5
8 9 13
15 A B C D F G H I J K
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
C
B
G H
D F
K
I J
Binary Search Tree Representation
Binary Search tree exhibits a special behavior. A node's left child must have
a value less than its parent's value and the node's right child must have a
value greater than its parent value and it is true for all nodes.
The basic operations that can be performed on a binary search tree data
structure, are the following −
Insert − Inserts an element in a tree/create a tree.
Search − Searches an element in a tree.
Preorder Traversal − Traverses a tree in a pre-order manner.
Inorder Traversal − Traverses a tree in an in-order manner.
Postorder Traversal − Traverses a tree in a post-order manner.
Deletion – Deletes elements from tree
Insert Operation
This is a very straight forward operation. First, the root node is inserted, and
then the next value is compared with the root node. If the value is greater
than root, it is added to the right subtree, and if it is lesser than the root, it
is added to the left subtree.
Algorithm
IF ROOT==NULL THEN
ROOT=P;
ELSE
IF P->DATA < R->DATA && R->LEFT==NULL THEN
R->LEFT=P
PRINTF("%D INSERTED",P->DATA)
ELSE IF P->DATA < R->DATA && R->LEFT!=NULL THEN
INSERT(R->LEFT,P)
ELSE IF P->DATA >= R->DATA && R->RIGHT==NULL THEN
R->RIGHT=P
PRINTF("%D INSERTED",P->DATA)
ELSE IF P->DATA >= R->DATA && R->RIGHT!=NULL THEN
INSERT(R->RIGHT,P)
END IF
END IF
Create the binary search tree using the following data elements.
The process of creating BST by using the given elements, is shown in the
image below.
Example 1: Create Binary Search Tree from following Nodes
25 root of tree
20
17
35
22
47
34
19
27
Example 2: 8,10,3,6,1,14,4,7,13
Example 3: 60, 41 , 74 ,65, 16, 53, 25 ,46 , 55 ,63 , 70, 64, 62, 42
Search Operation
INT FLAG=0;
WHILE R!= NULL
IF R->DATA==KEY THEN
FLAG=1
BREAK
ELSE
IF KEY < R->DATA THEN
R=R->LEFT
ELSE
R=R->RIGHT
END IF
END IF
END WHILE
RETURN FLAG;
Tree Traversal
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
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 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
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.
Delete Operations
Case I
In the first case, the node to be deleted is the leaf node. In such a case,
simply delete the node from the tree.
Case II
In the second case, the node to be deleted lies has a single child node. In
such a case follow the steps below:
1. Replace that node with its child node.
2. Remove the child node from its original position.
Case III
In the third case, the node to be deleted has two children. In such a case
follow the steps below:
1. Get the inorder successor of that node.
2. Replace the node with the inorder successor.
3. Remove the inorder successor from its original position.
SIMPLE IMPLEMENTATION OF BINARY TREE
/* Binary Tree*/
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
struct Tree
{
int data;
struct Tree* left;
struct Tree* right;
}*root;
/* Inorder traversal */
void InorderTraversal(struct Tree* r)
{
if(r != NULL)
{ //return;
InorderTraversal(r->left);
printf("%2d ->", r->data);
InorderTraversal(r->right);
}
}
/* Preorder traversal */
void PreorderTraversal(struct Tree* r)
{
if(r!=NULL)
{
printf("%2d ->", r->data);
PreorderTraversal(r->left);
PreorderTraversal(r->right);
}
}
/* Postorder traversal */
void PostorderTraversal(struct Tree* r)
{
if (r != NULL)
{
PostorderTraversal(r->left);
PostorderTraversal(r->right);
printf("%2d ->", r->data);
}
}
void main()
{
struct Tree *p;
int choice,n,key;
root=NULL;
do
{
clrscr();
printf(" Tree Operations \n");
printf("**************************\n");
printf(" 1.INSERT-TREE\n");
printf(" 2.PREORDER TRAVERSE\n");
printf(" 3.INORDER TRAVERSE\n");
printf(" 4.POSTORDER TRAVERSE\n");
printf(" 5.ALL TRAVERSAL\n");
printf(" 6.SEARCH KEY\n");
printf(" 7.EXIT\n");
printf("****************************\n");
printf("ENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter Data : ");
scanf("%d",&n);
p=(struct Tree*)malloc(sizeof(struct Tree));
p->data=n;
p->left=p->right=NULL;
Insert(root,p);
getch(); break;
case 2: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nPreorder :");
PreorderTraversal(root);
}
getch(); break;
case 3: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nInorder :");
InorderTraversal(root);
}
getch(); break;
case 4: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nPostorder :");
PostorderTraversal(root);
}
getch(); break;
case 5: printf("\nPreorder :");
PreorderTraversal(root);
printf("\nInorder :");
InorderTraversal(root);
printf("\nPostorder :");
PostorderTraversal(root);
getch();break;
case 6: printf("Enter key to search : ");
scanf("%d",&key);
if(root==NULL)
printf("Tree is Empty");
else
{
if(SearchTree(root,key))
printf("%d found",key);
else
printf("%d not found",key);
}
getch(); break;
case 7: break;
default:printf("Invalid Choice : ");
}
}while(choice!=7);
getch();
}
Expression Tree
Expression Tree is used to represent expressions. The expression tree is a
binary tree in which each internal node corresponds to the operator and
each leaf node corresponds to the operand so for example expression tree
for 3 + ((5+9)*2) would be:
Example1 : 3+((5+9)*2)
Example2: a + (b * c) + d * (e + f)
Example 3: 2*3/(2-1)+5*(4-1)
COMPLETE IMPLEMENTATION OF BINARY TREE
/* Binary Tree*/
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
struct Tree
{
int data;
struct Tree* left;
struct Tree* right;
}*root;
/* Inorder traversal */
void InorderTraversal(struct Tree* r)
{
if(r != NULL)
{ //return;
InorderTraversal(r->left);
printf("%2d ->", r->data);
InorderTraversal(r->right);
}
}
/* Preorder traversal */
void PreorderTraversal(struct Tree* r)
{
if(r!=NULL)
{
printf("%2d ->", r->data);
PreorderTraversal(r->left);
PreorderTraversal(r->right);
}
}
/* Postorder traversal */
void PostorderTraversal(struct Tree* r)
{
if (r != NULL)
{
PostorderTraversal(r->left);
PostorderTraversal(r->right);
printf("%2d ->", r->data);
}
}
return temp->data;
}
}
//return the actual root's address
return root;
}
void main()
{
struct Tree *p;
int choice,n,key;
root=NULL;
do
{
clrscr();
printf(" Tree Operations \n");
printf("**************************\n");
printf(" 1.INSERT-TREE\n");
printf(" 2.PREORDER TRAVERSE\n");
printf(" 3.INORDER TRAVERSE\n");
printf(" 4.POSTORDER TRAVERSE\n");
printf(" 5.ALL TRAVERSAL\n");
printf(" 6.SEARCH KEY\n");
printf(" 7.DELETE NODE\n");
printf(" 8.COUNT NODES\n");
printf(" 9.COUNT LEAF NODES\n");
printf(" 10.EXIT\n");
printf("****************************\n");
printf("ENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter Data : ");
scanf("%d",&n);
p=(struct Tree*)malloc(sizeof(struct Tree));
p->data=n;
p->left=p->right=NULL;
Insert(root,p);
getch(); break;
case 2: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nPreorder :");
PreorderTraversal(root);
}
getch(); break;
case 3: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nInorder :");
InorderTraversal(root);
}
getch(); break;
case 4: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nPostorder :");
PostorderTraversal(root);
}
getch(); break;
case 5: if(root==NULL)
printf("Tree is Empty");
else
{
printf("\nPreorder :");
PreorderTraversal(root);
printf("\nInorder :");
InorderTraversal(root);
printf("\nPostorder :");
PostorderTraversal(root);
}
getch();break;
case 6: if(root==NULL)
printf("Tree is Empty");
else
{
printf("Enter key to search : ");
scanf("%d",&key);
if (SearchTree(root,key))
printf("%d found",key);
else
printf("%d not found",key);
}
getch(); break;
case 7:if(root==NULL)
printf("Tree is Empty");
else
{
printf("Enter Node to Delete : ");
scanf("%d",&key);
root=DeleteNode(root,key);
}
getch(); break;
case 8: if(root==NULL)
printf("Tree is Empty");
else
{
count=0;
printf("\nNo of node in a binary tree = %d ",CountNodes(root));
}
getch(); break;
case 9: if(root==NULL)
printf("Tree is Empty");
else
printf("\nNo of Leaf node in a binary tree = %d
",CountLeafNodes(root));
getch(); break;
case 10: break;
default:printf("Invalid Choice : ");
}
}while(choice!=10);
getch();
}