Вы находитесь на странице: 1из 0

p

a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Definition of tree :-
A tree is a finite set of nodes such that it contains a root node and number of sub trees.
Tree terminologies :-
Consider the following tree to explain the tree terminologies
1. ROOT : A is root .
2. DEGREE OF NODE : Degree of node A is 3. Degree of node E is 0 .
3. LEAVES : E,F,G,H,C are the leaves .
4. INTERNAL NODES : B , D are internal nodes .
5. PARENT NODE : b , d are the parent nodes of E,F,G,H .
6. PREDECESSOR : E is a predecessor of B .
7. SUCESSOR : B is successor of F,G.
8. LEVEL OF THE NODE : A is at level of 0 .
9. HEIGHT OF TREE : maximum level of the tree .
10. DEGREE OF THE TREE : maximum degree of the tree .
BINARY TREE-
A binary tree is a finite set of nodes which is either empty or consists of a root and two
disjoint trees called left subtree and right sub tree .
TYPES :
1.Left skewed binary tree .
2. Right skewed binary tree
3.strictly binary tree .
4.complete binary tree .
F
G
C B
H
D
A
E
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
1.Left skewed binary tree .
2.Right skewed binary tree .
B
D
A
C
B
D
A
C
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
3.strictly binary tree .
Every non-leaf node has two children and the length from the
root to any leaf is the same.
A Strictly Binary Tree of level n has 2n leaves and number
of nodes in a Strictly Binary Tree is 2n+1 - 1
4.complete binary tree .
F G
C B
D
A
E
F G
C B
D
A
E
D E
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
A Complete Binary Tree has all of its leaves are on Level n or n-1 .
A Complete Binary Tree of level n has 2n leaves and number of nodes in a Strictly
Binary Tree is 2n+1 1 .
BINARY TREE REPRENSENTATIONS :
There are two types .
1. sequential representation .
2. linked representation .
BINARY TREE TRAVERSALS :
Pseudo codes :
1.INORDER
Template <class T>
Void inorder(bintree<T>* temp)
{
If(temp!=NULL)
{
Inorder(temp->left);
Cout<<temp->data;
Inorder(temp->right);
}}
2.PREORDER :
Template <class T>
Void preorder(bintree<T>* temp)
{
If(temp!=NULL)
{
Cout<<temp->data;
preorder(temp->left);
preorder(temp->right);
Traversing the tree means visiting each node at least once .
There are 3 traversing techniques .
1.INORDER .
2.PREORDER .
3.POSTORDER .
INORDER takes the form of LDR .
PREORDER takes the form of DLR .
POSTORDER takes the form of LRD .
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}}
3.POSTORDER :
Template <class T>
Void postorder(bintree<T>* temp)
{
If(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
Cout<<temp->data;
}}
EXAMPLE FOR B.T TRAVERSALS :
F G
C B
H
D
A
E
INORDER (LDR) : H DB E A F C G
PREORDER (DLR) : A B DH E C F G
POSTRORDER (LRD):
H DE B F G
C
A
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Binary Search Trees
While creating binary search trees the data is arranged systematically. That means values
at left sub tree is lesser than root value and root is Lesser than right sub tree value .
EXAMPLE :
To reduce the searching time complexity in binary tree we use binary search tree
OPERATIONS :-
1. INSERTION .
2. DELETION .
3. SEARCHING .
1.INSERTION OF A NODE :
If the value of the node which is to be inserted is greater than theValue of
current node we move on to the right sub branch otherwise we move on to the left sub
branch . As soon as the appropriateposition is found we attach this new node as left or
right child exactly .
12 99
90 9
5
70
10
EXAMPLE :-
15 22
20 6
21 9
10
8
24
15 22
20 6
21 9
10
8
24
23
After Inserting 23
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
DELETION OF A NODE :-
a.Deletion of leaf node .
b.Deletion of node having one child .
c.Deletion of node having two child.
a. DELETION OF LEAF NODE :

This is the simplest deletion in which we set the left or right pointer of parent node as
NULL .
Example :
15 22
20 8
6
12
7
15 22
20 8
12
7
After deleting 6
6
15 22
20 8
18
10
6
16
b.DELETION OF A NODE HAVING ONE CHILD :
22
20 8
10
6
After deleting 15
18
16
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
c.DELETION OF NODE HAVING TWO CHILD :
Here the INORDER successor has to be copied at the place where we
Want to delete the node .
3.SEARCHING FOR A NODE IN B.S.T :-
If we want to search value 9 , then we compare 9 with root node 10 . As 9 is less than 10
we will search on left sub branch . Now compare 9 with 5 , but 9 is greater than 5. So we
will move on to right sub branch . Now compare 9 with 8 but as 9 is greater than 8 we
will Move on to right sub branch as the node we will get holds the value 9. Desired node
can be searched .
5
7
10
6
9
4
12 5
7
10
8
6
9
4
12 5
8
10
6
9
4
After deleting 7
Inorder successor
4,5,10,12,6,7,8,9
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Balanced tree
A Balanced tree is a rooted tree where each subtree of the root has equal number of
nodes .
There are 3 types
1. HEIGHT BALANCED TREE .
2.RED-BLACK TREE .
3.B-TREES .
1.HEIGHT BALANCED TREE :-
These can be explained by taking AVL trees . Adelsion Velski and Lendis proposed the
AVL trees .
DEFINITION : An empty tree is height balanced if T is a Non empty B.T with TL and
TR as its left and right sub trees. The T is height balanced if and only if
( i ) TL and TR are height balanced
( ii ) hL-hR where hL and hR are heights of TL and TR
Balancing of tree is obtained by calculating balancing factor .
Is defined as hL-hR where hL and hR are heights of left and right sub tree of T .
for every node x, define its balance factor
balance factor of x =height of left subtree of x
- height of right subtree of x
balance factor of every node x is -1, 0, or 1
Balance Factors
0 0
0
0
1
0
-1 0
1
0
-1
1
-1
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
INSERTION ALGORITHAM:-
1.insert a new node as new leaf just as in ordinary B.S.T .
2.now trace the path from insertion point towards root . For each node n encountered ,
check if height of left and Right differ by atmost 1 .
(a) If yes , move towards parent (n).
(b) Other wise restructure by doing either a single rotation
Or double rotation .Thus once we perform a rotation at node n we do not require to
perform any rotation at any ancestors on n .
PROCEDURE OF INSERTION :
1. The elements in the node should be inserted using BINARY SEARCH TREE
condition i.es Left Node Value <Root NodeValue <Right Node Value .
2. The Balancing Factor (BF) should be one among the following
value i.es 1,0,-1 . We can calculate it by
BF =Length of Left Nodes Lenght of Right Nodes
3. If the balancing Factor conditios is failed then we should apply one among the
four Rotational techniques to balance the tree
-2
-2
1
This is not AVL tree.
Now consider another example
0
0
0
0 0
1
-1
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
4. The four Rotation techniques are as follows
There are four cases when re balancing is required
1.An insertion of new node into left subtree of left child (LL)
2.An insertion of new node into right subtree of right child (RR)
3.An insertion of new node into right subtree of left child (RL)
4.An insertion of new node into left subtree of right child (LR)
There are 2 ways the rotations can be implemented
Single rotation double rotation
LL RL
RR LR
Example :Let us insert the following elements 6,4 ,7,5, 2,1 by using AVL TREE .
SOL : After insertion of 6,4,7,5,2 using B.S.T condition
we get the following tree , and the BF is satisfied
But after insertion of the element 1 , The will become imbalance as shown below , so to
maintain balance we use rotations
4
2 5
0
0
0
6
1
7
0
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Example :Let us insert the following elements 2,4,1,6,3,7 by using AVL TREE .
SOL : After insertion of 2,4,1,6,3 using B.S.T condition we get the following tree , and
the BF is satisfied
4
2
5
1
0
1
0
1
6
2
7
0
LL rotation
4
2
6
1
0
1
0
0
5 7
0 0
2
1 4
0
0
3
0
6
0
-1
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
But after insertion of the element 7 , The Tree will become imbalance as shown below ,
so to maintain balance we use rotations
Example:Let us insert the following elements 6,2,7,1,4,3 by using AVL TREE .
SOL : After insertion of 6,2,7,1,4 using B.S.T conditionwe get the following tree , and
the BF is satisfied
2
1 4
0
-1
3
0
6
7
0
-1
-2
RR rotation
4
2
6
1 3
0 0
0
-1
0
7
0
6
2
7
0 0
1
0
4
0
1
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
But after insertion of the element 3 , The Tree will become imbalance
as shown below , so to maintain balance we use rotations
Here there is un balance of LR . Sofirst rotate un balanced sub tree left and then check
for the balance factor and again if there is unbalancethen again rotate the un balanced
Sub tree to right .So in this tree first we have to rotate
6
2 7
-1
0
1
0
4
3
0
1
2
6
2
7
-1
0
1
0
4
3
0
1
2
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
So the tree will become like this
Againthere is an un balance so we Should rotate to right at subtree where it got
unbalanced .so the treewill look like
6
4 7
2
1
3
4
2 6
1
3
0 0
0
-1
0
7
0
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Example:Let us insert the following elements 2,6,1,4,7,5 by using AVL TREE .
SOL : After insertion of 2,6,1,4,7 using B.S.T condition
we get the following tree , and the BF is satisfied
But after insertion of the element 5 , The Tree will become imbalance as shown below ,
so to maintain balance we use rotations
Here there is un balance of RL . So first rotate un balanced sub tree right And then check
for the balance Factor and again if there is unbalance .Then again rotate the un balanced
Sub tree to left .
So in this tree first we have to rotate right
2
1 6
0
0
7
0
4
0
-1
2
1 6
0
1
7
0
4
5
0
-1
-2
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
So the tree will become like this
Again there is an un balance so we Should rotate to left at subtree where it got
unbalanced .so the treewill look like
2
1 6
0
1
7
0
4
5
0
-1
-2
2
1 4
6
5 7
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Height of an AVL Tree :
Theorem: The Height of AVL Tree with n elements (nodes) is O(log n).
Proof: Let an AVL tree with n nodes in it . N
h
be the minimum number of nodes in an
AVL tree of height h.
In worst cases , one subtree may have height h-1 and other subree may have height h-2 .
And both these subtrees are AVL trees . since for every node in AVL tree the height of
left and right subtree differ by at most 1.
Hence
N
h
=N
h-1
+N
h-2
+1
Where N
h
denotes the minimum number of nodes in an AVL tree of height h.
Therefore N
0
=0 N
1
=2
We can also write it as
N >Nh = N
h-1
+N
h-2
+1
> 2 N
h-2

> 4 N
h-2
:
:
>2
i
N
h-2
i


if value of h is even , Let i=h

Then the equation becomes

N > 2
h
N
2

= N > 2
h
N
2
, since N
2
=4
= O(log n)
if the value of h is odd , Let i=(h-1)
2
4
2 6
1
0
0
0
0
5 7
0 0
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
then the equation will be as

N>2
(h-1)/2
N
1


N>2
(h-1)/2
* 1 since , N
1
=1
h=O(log n)
this proves that the height of an AVL tree ia always O(log n) .
/* Write a C++program to perform the following operations on AVL-trees:
a) Insertion.
b) Deletion. */
#include<iostream.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define NULL 0
class AVL;
class AVLNODE
{
friend class AVL;
private:
int data;
AVLNODE *left,*right;
int bf;
};
class AVL
{
private:
AVLNODE *root;
public:
AVLNODE *loc,*par;
AVL()
{
root=NULL;
}
int insert(int);
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
void displayitem();
void display(AVLNODE *);
void removeitem(int);
void remove1(AVLNODE *,AVLNODE *,int);
void remove2(AVLNODE *,AVLNODE *,int);
void search(int x);
void search1(AVLNODE *,int);
};
int AVL::insert(int x)
{
AVLNODE *a,*b,*c,*f,*p,*q,*y,*clchild,*crchild;
int found,unbalanced;
int d;
if(!root) //special case empty tree
{ y=new AVLNODE;
y->data=x;
root=y;
root->bf=0;
root->left=root->right=NULL;
return TRUE; }
//phase 1:locate insertion point for x.a keeps track of the most
// recent node with balance factor +/-1,and f is the parent of a
// q follows p through the tree.
f=NULL;
a=p=root;
q=NULL;
found=FALSE;
while(p&&!found)
{ //search for insertion point for x
if(p->bf)
{
a=p;
f=q;
}
if(x<p->data) //take left branch
{
q=p;
p=p->left;
}
else if(x>p->data)
{
q=p;
p=p->right;
}
else
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
y=p;
found=TRUE;
}
} //end while
//phase 2:insert and rebalance.x is not in the tree and
// may be inserted as the appropriate child of q.
if(!found)
{
y =new AVLNODE;
y->data=x;
y->left=y->right=NULL;
y->bf=0;
if(x<q->data) //insert as left child
q->left=y;
else
q->right=y; //insert as right child
//adjust balance factors of nodes on path from a to q
//note that by the definition of a,all nodes on this
//path must have balance factors of 0 and so will change
//to +/- d=+1 implies that x is inserted in the left
// subtree of a d=-1 implies
//to that x inserted in the right subtree of a.
if(x>a->data)
{
p=a->right;
b=p;
d=-1;
}
else
{
p=a->left;
b=p;
d=1;
}
while(p!=y)
if(x>p->data) //height of right increases by 1
{
p->bf=-1;
p=p->right;
}
else //height of left increases by 1
{
p->bf=1;
p=p->left;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
//is tree unbalanced
unbalanced=TRUE;
if(!(a->bf)||!(a->bf+d))
{ //tree still balanced
a->bf+=d;
unbalanced=FALSE;
}
if(unbalanced) //tree unbalanced,determine rotation type
{
if(d==1)
{ //left imbalance
if(b->bf==1) //rotation type LL
{
a->left=b->right;
b->right=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->left=c->right;c->left=b;
c->right=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
} //end of left imbalance
else //right imbalance
{
if(b->bf==-1) //rotation type RR
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
a->right=b->left;
b->left=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->right=c->left;
c->right=b;
c->left=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
}
//subtree with root b has been rebalanced and is the new subtree
if(!f)
root=b;
else if(a==f->left)
f->left=b;
else if(a==f->right)
f->right=b;
} //end of if unbalanced
return TRUE;
} //end of if(!found)
return FALSE;
} //end of AVL INSERTION
void AVL::displayitem()
{
display(root);
}
void AVL::display(AVLNODE *temp)
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
{
if(temp==NULL)
return;
cout<<temp->data<<" ";
display(temp->left);
display(temp->right);
}
void AVL::removeitem(int x)
{
search(x);
if(loc==NULL)
{
cout<<"\nitem is not in tree";
return;
}
if(loc->right!=NULL&&loc->left!=NULL)
remove1(loc,par,x);
else
remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
AVLNODE *ptr,*save,*suc,*psuc;
ptr=l->right;
save=l;
while(ptr->left!=NULL)
{
save=ptr;
ptr=ptr->left;
}
suc=ptr;
psuc=save;
remove2(suc,psuc,x);
if(p!=NULL)
if(l==p->left)
p->left=suc;
else
p->right=suc;
else
root=l;
suc->left=l->left;
suc->right=l->right;
return;
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
AVLNODE *child;
if(s->left==NULL && s->right==NULL)
child=NULL;
else if(s->left!=NULL)
child=s->left;
else
child=s->right;
if(p!=NULL)
if(s==p->left)
p->left=child;
else
p->right=child;
else
root=child;
}
void AVL::search(int x)
{
search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
AVLNODE *ptr,*save;
int flag;
if(temp==NULL)
{
cout<<"\nthe tree is empty";
return;
}
if(temp->data==x)
{
cout<<"\nthe item is root and is found";
par=NULL;
loc=temp;
par->left=NULL;
par->right=NULL;
return; }
if( x <temp->data)
{
ptr=temp->left;
save=temp;
}
else
{
ptr=temp->right;
save=temp;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
while(ptr!=NULL)
{
if(x==ptr->data)
{ flag=1;
cout<<"\nitemfound";
loc=ptr;
par=save;
}
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(flag!=1)
{
cout<<"item is not there in tree";
loc=NULL;
par=NULL;
cout<<loc;
cout<<par;
}
}
main()
{
AVL a;
int x,y,c;
char ch;
do
{
cout<<"\n1.insert";
cout<<"\n2.display";
cout<<"\n3.delete";
cout<<"\n4.search";
cout<<"\n5.exit";
cout<<"\nEnter u r choice to perform on AVL tree";
cin>>c;
switch(c)
{
case 1:cout<<"\nEnter an element to insert into tree";
cin>>x;
a.insert(x);
break;
case 2:a.displayitem(); break;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
case 3:cout<<"\nEnter an item to deletion";
cin>>y;
a.removeitem(y);
break;
case 4:cout<<"\nEnter an element to search";
cin>>c;
a.search(c);
break;
case 5:exit(0); break;
default :cout<<"\nInvalid option try again";
}
cout<<"\ndo u want to continue";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}



/*Binary search tree with all the Recursive and non Recursive traversals*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class binarynode
{
public:
int data;
binarynode *left;
binarynode *right;
};
class binsrctree
{
private:
binarynode *root;
void inorder_rec(binarynode *);
void inorder_non_rec(binarynode *);
void preorder_rec(binarynode *);
void preorder_non_rec(binarynode *);
void postorder_rec(binarynode *);
void postorder_non_rec(binarynode *);
public:
binsrctree()
{
root=NULL;
}
void insert(int );
void print_inorder_rec();
void print_inorder_non_rec();
void print_preorder_rec();
void print_preorder_non_rec();
void print_postorder_rec();
void print_postorder_non_rec();
};
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
class stack
{
int top;
binarynode *stackel[20];
public:
stack()
{
top=-1;
}
void push(binarynode *);
binarynode* pop();
int empty()
{
if(top==-1)
return(1);
return(0);
}
};
void stack::push(binarynode *node)
{
stackel[++top]=node;
}
binarynode *stack::pop()
{
return(stackel[top--]);
}
class stack_int
{
int top;
int stack_int[20];
public:
stack_int()
{
top=-1;
}
void push(int flag);
int pop();
int empty_int()
{
if(top==-1)
return(1);
return(0);
}
};
void stack_int::push(int flag)
{
stack_int[++top]=flag;
}
int stack_int::pop()
{
return(stack_int[top--]);
}
/*---------------------------------------------------------------------*/
/* fUNCTION TO INSERT A NODE IN THE TREE */
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
/*---------------------------------------------------------------------*/
void binsrctree::insert(int val)
{
binarynode *temp,*prev,*curr;
temp=new binarynode;
temp->data=val;
temp->left=temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
curr=root;
while(curr!=NULL)
{
prev=curr;
if(temp->data<curr->data)
curr=curr->left;
else
curr=curr->right;
}
if(temp->data<prev->data)
prev->left=temp;
else
prev->right=temp;
}
}
/* ------------------------------------------------*/
/*INORDER RECURSIVE TRAVERSAL*/
/*-------------------------------------------------*/
void binsrctree::inorder_rec(binarynode *root)
{
if(root!=NULL)
{
inorder_rec(root->left);
cout<<root->data<<" ";
inorder_rec(root->right);
}
}
/*--------------------------------------------------*/
/*INORDER NON RECURSIVE TRAVERSAL*/
/*--------------------------------------------------*/
void binsrctree::inorder_non_rec(binarynode *root)
{
stack stk;
binarynode *temp;
if(root!=NULL)
{
temp=root;
do
{
while(temp!=NULL)
{
stk.push(temp);
temp=temp->left;
}/*end while*/
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
if(!stk.empty())
{
temp=stk.pop();
cout<<temp->data<<" ";
temp=temp->right;
}/*end if*/
else
break;
}while(1); /*end do while*/
}/* end if */
else
cout<<"Empty tree";
} /*end function */
/*--------------------------------------------------*/
/*PREORDER RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::preorder_rec(binarynode *root)
{
if(root!=NULL)
{
cout<<root->data<<" ";
preorder_rec(root->left);
preorder_rec(root->right);
}
}
/*--------------------------------------------------*/
/*PREORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::preorder_non_rec(binarynode *root)
{
stack stk;
binarynode *temp=root;
stk.push(temp);
while(!stk.empty())
{
temp=stk.pop();
if(temp!=NULL)
{
cout<<temp->data<<" ";
stk.push(temp->right);
stk.push(temp->left);
}
}
}
/*--------------------------------------------------*/
/*POSTRDER RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::postorder_rec(binarynode *root)
{
if(root!=NULL)
{
postorder_rec(root->left);
postorder_rec(root->right);
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout<<root->data<<" ";
}
}
/*--------------------------------------------------*/
/*POSTORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::postorder_non_rec(binarynode *root)
{
stack stk;
stack_int stk1;
int flag;
binarynode *temp=root;
do
{
if(temp!=NULL)
{
stk.push(temp);
stk1.push(1);
temp=temp->left;
}
else
{
if(stk.empty())
break;
temp=stk.pop();
flag=stk1.pop();
if(flag==2)
{
cout<<temp->data;
temp=NULL;
} /*end if */
else
{
stk.push(temp);
stk1.push(2);
temp=temp->right;
} /* end else */
} /* end if */
}while(1);/*end do while*/
}/*end function*/
/*--------------------------------------------------*/
/*FUNCTION TO PRINT INORDER RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_inorder_rec()
{
cout<<" ";
inorder_rec(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT INORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_inorder_non_rec()
{
cout<<" ";
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
inorder_non_rec(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT PREORDER RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_preorder_rec()
{
cout<<" ";
preorder_rec(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT PREORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_preorder_non_rec()
{
cout<<" ";
preorder_non_rec(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT POSTORDER RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_postorder_rec()
{
cout<<" ";
postorder_rec(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT POSTORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_postorder_non_rec()
{
cout<<" ";
postorder_non_rec(root);
cout<<" ";
}
/*--------------------------------------------------*/
/* MAIN FUNCTION */
/*---------------------------------------------------*/
void main()
{
binsrctree BST;
int ch,element;
clrscr();
do
{
cout<<" 1.Insert a node in binary tree";
cout<<" 2.Recursive Inorder traversal";
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout<<" 3.Non Recursive Inorder traversal";
cout<<" 4.Recursive preorder traversal";
cout<<" 5.Non recursive preorder traversal";
cout<<" 6.Recursive postorder traversal";
cout<<" 7.Non recursive postorder traversal";
cout<<" 8.Exit";
cout<<" Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<" Enter the element you wnat to insert";
cin>>element;
BST.insert(element);
break;
case 2:
cout<<" Recursive Inorder traversal ";
BST.print_inorder_rec();
break;
case 3:
cout<<" NonRecursive Inorder traversal ";
BST.print_inorder_non_rec();
break;
case 4:
cout<<" Recursive preorder traversal ";
BST.print_preorder_rec();
break;
case 5:
cout<<" Non recursive preorder traversal ";
BST.print_preorder_non_rec();
break;
case 6:
cout<<" Recursive postorder traversal";
BST.print_postorder_rec();
break;
case 7:
cout<<" Non recursive postorder traversal ";
BST.print_postorder_non_rec();
break;
case 8:
exit(1);
}
}while(ch<8);
}
/* write a CPP to compare two binary search trees */
#define true 1
#define false 0
class bs
{
public :
int data;
bs *left,*right;
}
class compare
{
private:
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
bs *root;
public:
compare();
void insert(bs *,bs *);
void inorder(bs *);
int equal(bs *,bs *);
void create(),convert(compare,compare);
void traverse();
};
compare::compare()
{
root=NULL;
}
void compare::create()
{
bs *new;
new=new bs;
cout<<"enter the elements";
cin>>new->data;
new->left=NULL;
new->right=NULL;
if(root,NULL)
root=new;
else
insert(root,new);
}
void compare::insert(bs *root,bs *new)
{
bs *new;
if(new->data<root->data)
{
if(root->left==NULL)
root->left=new;
else
insert(root->left,new);
}
if(new->data>root->data)
{
if(root->right==NULL)
root->right=new;
else
insert(root->right,new);
}}
void compare::traverse()
{inorder(root);
}
void compare::inorder(bs *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
cout<<" <<temp->data;
inorder(temp->right);
}}
void compare::convert(compare tr1,compare tr2)
{
int flag;
flag=equal(tr1.root,tr2.root);
if(flag==1)
cout<<"the two trees are identical";
else
cout<<"the two trees are not identical";
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
int compare::equal(bs *t1,bs *t2)
{
int flag;
flag=false;
if(t1==NULL && t2==NULL)
falg=true;
else
if(t1!=NULL && t2!=NULL)
if(t1->data==t2->data)
if(equal(t1->left,t2->left))
flag=equal(t1->right,t2->right);
return(flag);
}

Вам также может понравиться