Showing posts with label trees. Show all posts
#include< iostream >
#include< cstdlib >
#include< string >
using namespace std;
struct tree
{
int info;
tree *Left, *Right;
};
tree *root;
class Binary_tree
{
string path;
public:
Binary_tree();
void insert1(int);
tree *insert2(tree *, tree *);
void search(int);
void pretrav(tree *);
void intrav(tree *);
void posttrav(tree *);
};
Binary_tree::Binary_tree()
{
root = NULL;
}
tree* Binary_tree::insert2(tree *temp,tree *newnode)
{
if(temp==NULL)
{
temp=newnode;
}
else if(temp->info < newnode->info)
{
insert2(temp->Right,newnode);
if(temp->Right==NULL)
temp->Right=newnode;
}
else
{
insert2(temp->Left,newnode);
if(temp->Left==NULL) temp->Left=newnode;
}
return temp;
}
void Binary_tree::insert1(int n)
{
tree *temp=root,*newnode;
newnode=new tree;
newnode->Left=NULL;
newnode->Right=NULL;
newnode->info=n;
root=insert2(temp,newnode);
}
void Binary_tree::pretrav(tree *t = root)
{
if(root == NULL)
{
cout<<"Nothing to display";
}
else if(t != NULL)
{
cout<< t->info<<" ";
pretrav(t->Left);
pretrav(t->Right);
}
}
void Binary_tree::intrav(tree *t = root)
{
if(root==NULL)
{
cout<<"Nothing to display";
}
else if(t!=NULL)
{
intrav(t->Left);
cout<< t->info<<" ";
intrav(t->Right);
}
}
void Binary_tree::posttrav(tree *t = root)
{
if(root==NULL)
{
cout<<"Nothing to display";
}
else if(t!=NULL)
{
posttrav(t->Left);
posttrav(t->Right);
cout<< t->info<<" ";
}
}
void Binary_tree::search(int key)
{
tree *temp=root,*parent=root;
path = "";
if(temp==NULL)
{
cout<<"The tree is empty"<< endl;
}
else
{
path = "root";
while(temp!=NULL && temp->info!=key)
{
parent=temp;
if(temp->info< key)
{
temp=temp->Right;
path += "->Right";
}
else
{
temp=temp->Left;
path += "->Left";
}
}
}
if(temp==NULL)
{
cout<<"Key not found!";
}
else
{
cout<<"Key is found\n";
cout<<"Paht is: ";
cout<< path;
}
}
int main()
{
Binary_tree bt;
int choice, n, key;
while(1)
{
cout<<"\n\t1. Insert\n\t2. Search\n\t3. Preorder Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n\t6. Exit"<< endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter item: ";
cin>>n;
bt.insert1(n);
break;
case 2:
cout<<"Enter element to search: ";
cin>>key;
bt.search(key);
break;
case 3:
cout<< endl;
bt.pretrav();
break;
case 4:
cout<< endl;
bt.intrav();
break;
case 5:
cout<< endl;
bt.posttrav();
break;
case 6:
exit(0);
}
}
return 0;
}
Program Limitations
* This program Only process single digit operations
* Can't handle unary operation
* Only process left to right associativity
#include< iostream >
#include< cmath >
#include< cstdlib >
#include< string >
#define MAX_SIZE 20
using namespace std;
template< class T >
class Stack
{
private:
T item[MAX_SIZE];
int top;
public:
Stack()
{
top = -1;
}
void push(T data)
{
if(!this->is_full())
item[++top] = data;
else
{
cout<<"Stack Error"<< endl;
exit(10);
}
}
T pop()
{
if(!this->is_empty())
return item[top--];
else
{
cout<<"Stack is Empty"<< endl;
exit(11);
}
}
int size()
{
return top+1;
}
bool is_empty()
{
if(top==-1)
return true;
else
return false;
}
bool is_full()
{
if(top==MAX_SIZE-1)
return true;
else
return false;
}
void display()
{
for(int i=0; i< this->size(); i++)
{
cout<< item[i]<<" ";
}
cout<< endl;
}
T return_top()
{
return item[top];
}
};
class Convert
{
private:
bool num_flag;
bool two_digit_flag;
public:
Convert();
string return_with_bracket(string infix);
void to_Postfix(string infix,char postfix[]);
bool prcd(char op1, char op2);
int isOperand(char op);
int isOperator(char op);
bool return_flag()
{
return num_flag;
}
};
Convert::Convert()
{
this->num_flag = false;
this->two_digit_flag = false;
}
string Convert::return_with_bracket(string infix)
{
return("(" + infix + ")");
}
bool Convert::prcd(char op1, char op2)
{
if((op1=='+' || op1=='-' || op1=='*' || op1=='/') && op2=='(' )
return true;
if(op1=='+' && op2=='+')
return true;
if(op1=='-' && op2=='-')
return false;
if(op1=='-' && op2=='+')
return false;
if(op1=='+' && op2=='-')
return false;
if(op1=='/' && op2=='/')
return false;
if(op1=='/' && (op2=='-' || op2=='+'))
return true;
if(op1=='*' && (op2=='+' || op2=='-'))
return true;
if((op1 == '-' || op1 == '+') && (op2 =='*' || op2 == '/'))
return false;
if((op1 == '$' || op1 == '+') && (op2 =='*' || op2 == '/' || op2=='-'))
return true;
if((op1 == '-' || op1 == '+' || op1 =='*' || op1 == '/')&& op2=='^')
return false;
if(op1 == '^' && ( op2 == '+' || op2 =='*' || op2 == '/' || op2=='-'))
return false;
}
int Convert::isOperand(char op)
{
return(op>= '0' && op <= '9');
}
int Convert::isOperator(char op)
{
return(op=='+' || op=='-' || op == '/' || op=='*' || op=='^');
}
void Convert::to_Postfix(string infix, char postfix[])
{
int position, outpos=0;
char c;
int count = 0;
char temp;
char stacktop ;
Stack < char > stack;
for(position = 0; (c = infix[position])!='\0'; position++)
{
if(this->isOperand(c))
{
postfix[outpos++] = c;
this->num_flag = true;
count++;
if(count>=2)
{
this->two_digit_flag = true;
}
}
else if(this->isOperator(c))
{
count = 0;
if(isOperator(infix[position]) && isOperator(infix[position+1]))
{
cout<<"\aMissing argument in between "<< infix[position]<<" and "<< infix[position+1]
<<" in column "<< position+1<< endl;
exit(9);
}
if(this->prcd(c, stacktop))
{
stacktop=stack.return_top();
stack.push(c);
stacktop = c;
}
else
{
while(true)
{
temp = stack.pop();
postfix[outpos++] =temp;
stacktop = stack.return_top();
if(prcd(c, stacktop) || stacktop=='(')
break;
}
stack.push(c);
stacktop = stack.return_top();
}
}
else if(c=='(')
{
count = 0;
stack.push(c);
stacktop = stack.return_top();
}
else if(c==')')
{
count = 0;
while(1)
{
if(stack.size()==0)
{
cout<<"Warning!! Number of ')' is greater than '('" << endl;
exit(2);
}
temp = stack.pop();
if(temp!='(')
{
postfix[outpos++] = temp;
}
else
{
break;
}
}
stacktop =stack.return_top();
}
else
{
cout<<"Invalid input";
exit(3);
}
if(infix[position]==')' && infix[position+1]=='(')
{
stack.push('*');
stacktop = stack.return_top();
}
}
if(stack.size()!=0)
{
cout<<"Warning!!Number of '(' is greater than ')'"<< endl;
// exit(6);
}
if(!this->return_flag())
{
cout<<"You must Enter Numeric value for calculation"<< endl;
cout<<"This program cannot perform operations on variables";
exit(5);
}
if(this->two_digit_flag)
{
cout<<"Sory! Althoug u may have entered right string"<< endl;
cout<<"this program is only for single digit operation"<< endl;
exit(8);
}
postfix[outpos] = '\0';
}
class Evaluate
{
public:
double eval(char expr[], Convert &);
double oper(int symb, double op1, double op2);
};
double Evaluate::oper(int symb, double op1, double op2)
{
switch(symb)
{
case '+': return (op1 + op2);
case '-': return (op1 - op2);
case '*': return (op1 * op2);
case '/': return (op1 / op2);
case '^': return (pow(op1, op2));
}
}
double Evaluate::eval(char expr[],Convert &convert)
{
int c, position;
char temp1;
int count = 0;
double opnd1, opnd2, value;
Stack< double > stack;
for(position = 0; (c = expr[position])!='\0'; position++)
{
if(convert.isOperand(c))
{
temp1 = double(c-'0');
stack.push(temp1);
}
else
{
opnd2 = stack.pop();
if(stack.size()==0)
{
cout<<"This program cannot process unary operation";
exit(1);
}
opnd1 = stack.pop();
value = oper(c, opnd1, opnd2);
stack.push(value);
}
}
if(stack.size()>=2)
{
cout<<"Sory! this program cannot calculate this"<< endl;
cout<<"Enter +, *, /, - or ^ between bracket"<< endl;
exit(4);
}
return (stack.pop());
}
int main()
{
Convert convert;
Evaluate evaluate;
string bracketted_infix;
char infix[50], postfix[50];
char choice;
while(1)
{
cout<<"Enter string: ";
cin>>infix;
cout<< endl;
cout<<"Entered String: "<< infix<< endl;
bracketted_infix = convert.return_with_bracket(infix);
convert.to_Postfix(bracketted_infix, postfix);
cout<<"Equivalent Postfix string: "<< postfix<< endl;
cout<<"RESULT: ";
cout<< evaluate.eval(postfix, convert);
cout<<"\nCalculate another string?(y/n) ";
cin>>choice;
cout<< endl;
if(choice=='n')
break;
}
return 0;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
node *left;
int value;
node *right;
};
node *curr=NULL;
int addnode(node *, node *);
int inorder(node *);
int preorder(node *);
int postorder(node *);
void main()
{
char c;
int v;
clrscr();
do
{
cout<<"Select any one";
cout<<"0 ->Exit";
cout<<"1 ->Add node";
cout<<"2 ->Inorder traversal";
cout<<"3 ->Preorder traversal";
cout<<"4 ->Postorder trversal :";
cin>>c;
switch(c)
{
case '0':
exit(1);
case '1':
node *temp;
temp = new node;
cout<<" Enter the value of the node : ";
cin>>temp->value;
if(curr==NULL)
{
curr=new node;
curr->value=temp->value;
curr->left=NULL;
curr->right=NULL;
cout<<" The root node is added";
}
else
v=addnode(curr,temp);
if(v==1)
cout<<" The node is added to the left";
else if(v==2)
cout<<" The node is added to the right";
else if(v==3)
cout<<" The same value exists";
break;
case '2':
v=inorder(curr);
if(v==0)
cout<<"The tree is empty";
break;
case '3':
v=preorder(curr);
if(v==0)
cout<<"The tree is empty";
break;
case '4':
v=postorder(curr);
if(v==0)
cout<<"The tree is empty";
break;
default:
cout<<"Invalid entry";
break;
}
}while(c!='0');
getch();
}
int addnode(node *fcurr, node *fnew )
{
if(fcurr->value==fnew->value)
{
return 3;
}
else
{
if(fcurr->value > fnew->value)
{
if(fcurr->left != NULL)
addnode(fcurr->left, fnew);
else
{
fcurr->left = fnew;
(fcurr->left)->left=NULL;
(fcurr->left)->right=NULL;
return 1;
}
}
else
{
if(fcurr->right != NULL)
addnode(fcurr->right, fnew);
else
{
fcurr->right = fnew;
(fcurr->right)->left=NULL;
(fcurr->right)->right=NULL;
return 2;
}
}
}
}
int inorder(node *fincurr)
{
if(fincurr == NULL)
return 0;
else
{
if(fincurr->left != NULL)
inorder(fincurr->left);
cout << fincurr->value<<" ";
if(fincurr->right != NULL)
inorder(fincurr->right);
}
}
int preorder(node *fprcurr)
{
if(fprcurr == NULL)
return 0;
else
{
cout << fprcurr->value<<" ";
if(fprcurr->left != NULL)
preorder(fprcurr->left);
if(fprcurr->right != NULL)
preorder(fprcurr->right);
}
}
int postorder(node *fpocurr)
{
if(fpocurr == NULL)
return 0;
else
{
if(fpocurr->left != NULL)
postorder(fpocurr->left);
if(fpocurr->right != NULL)
postorder(fpocurr->right);
cout << fpocurr->value<<" ";
}
}
AVL Tree with Insertion,Deletion and balancing Height
Posted by Naveen's Blogs
Tag :
Data Structures,
trees
#include<iostream>
#include<stdlib>
#include<conio>
using namespace std;
struct node
{
int element;
node *left;
node *right;
int height;
};
typedef struct node *nodeptr;
class bstree
{
public:
void insert(int,nodeptr &);
void del(int, nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
nodeptr findmin(nodeptr);
nodeptr findmax(nodeptr);
void copy(nodeptr &,nodeptr &);
void makeempty(nodeptr &);
nodeptr nodecopy(nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
; int bsheight(nodeptr);
nodeptr srl(nodeptr &);
nodeptr drl(nodeptr &);
nodeptr srr(nodeptr &);
nodeptr drr(nodeptr &);
int max(int,int);
int nonodes(nodeptr);
};
// Inserting a node
void bstree::insert(int x,nodeptr &p)
{
if (p == NULL)
{
p = new node;
p->element = x;
p->left=NULL;
p->right = NULL;
p->height=0;
if (p==NULL)
cout<<"Out of Space";
}
else
{
if (x< p-> element)
{
insert(x,p->left);
if ((bsheight(p->left) - bsheight(p->right))==2)
{
if (x < p->left->element)
p=srl(p);
else
p = drl(p);
}
}
else if (x>p->element)
{
insert(x,p->right);
if ((bsheight(p->right) - bsheight(p->left))==2)
{
if (x > p->right->element)
p=srr(p);
else
p = drr(p);
}
}
else
cout<<"Element Exists";
}
int m,n,d;
m=bsheight(p->left);
n=bsheight(p->right);
d=max(m,n);
p->height = d + 1;
}
// Finding the Smallest
nodeptr bstree::findmin(nodeptr p)
{
if (p==NULL)
{
cout<<"Empty Tree";
return p;
}
else
{
while(p->left !=NULL)
p=p->left;
return p;
}
}
// Finding the Largest
nodeptr bstree::findmax(nodeptr p)
{
if (p==NULL)
{
cout<<"Empty Tree";
return p;
}
else
{
while(p->right !=NULL)
p=p->right;
return p;
}
}
// Finding an element
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"Element not found";
else
if (x < p->element)
find(x,p->left);
else
if (x>p->element)
find(x,p->right);
else
cout<<"Element found !";
}
// Copy a tree
void bstree::copy(nodeptr &p,nodeptr &p1)
{
makeempty(p1);
p1 = nodecopy(p);
}
// Make a tree empty
void bstree::makeempty(nodeptr &p)
{
nodeptr d;
if (p != NULL)
{
makeempty(p->left);
makeempty(p->right);
d=p;
free(d);
p=NULL;
}
}
// Copy the nodes
nodeptr bstree::nodecopy(nodeptr &p)
{
nodeptr temp;
if (p==NULL)
return p;
else
{
temp = new node;
temp->element = p->element;
temp->left = nodecopy(p->left);
temp->right = nodecopy(p->right);
return temp;
}
}
// Deleting a node
void bstree::del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
cout<<"Element not found ";
else if ( x < p->element)
del(x,p->left);
else if (x > p->element)
del(x,p->right);
else if ((p->left == NULL) && (p->right == NULL))
{
d=p;
free(d);
p=NULL;
cout<<"Element deleted !";
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
cout<<"Element deleted !";
}
else if (p->right == NULL)
{
d=p;
p=p->left;
free(d);
cout<<"Element deleted !";
}
else
p->element = deletemin(p->right);
}
int bstree::deletemin(nodeptr &p)
{
int c;
cout<<"inside deltemin ";
if (p->left == NULL)
{
c=p->element;
p=p->right;
return c;
}
else
{
c=deletemin(p->left);
return c;
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<< p-> element <<"-->";
preorder(p->left);
preorder(p->right);
}
}
// Inorder Printing
void bstree::inorder(nodeptr p)
{
if (p!=NULL)
{
inorder(p->left);
cout<< p->element <<"-->";
inorder(p->right);
}
}
// PostOrder Printing
void bstree::postorder(nodeptr p)
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<< p->element<<"-->";
}
}
int bstree::max(int value1, int value2)
{
return ((value1 > value2) ? value1 : value2);
}
int bstree::bsheight(nodeptr p)
{
int t;
if (p == NULL)
return -1;
else
{
t = p->height;
return t;
}
}
nodeptr bstree:: srl(nodeptr &p1)
{
nodeptr p2;
p2 = p1->left;
p1->left = p2->right;
p2->right = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(bsheight(p2->left),p1->height) + 1;
return p2;
}
nodeptr bstree:: srr(nodeptr &p1)
{
nodeptr p2;
p2 = p1->right;
p1->right = p2->left;
p2->left = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(p1->height,bsheight(p2->right)) + 1;
return p2;
}
nodeptr bstree:: drl(nodeptr &p1)
{
p1->left=srr(p1->left);
return srl(p1);
}
nodeptr bstree::drr(nodeptr &p1)
{
p1->right = srl(p1->right);
return srr(p1);
}
int bstree::nonodes(nodeptr p)
{
int count=0;
if (p!=NULL)
{
nonodes(p->left);
nonodes(p->right);
count++;
}
return count;
}
int main()
{
clrscr();
nodeptr root,root1,min,max;//,flag;
int a,choice,findele,delele,leftele,rightele,flag;
char ch='y';
bstree bst;
//system("clear");
root = NULL;
root1=NULL;
cout<<"AVL Tree";
cout<<"========";
do
{
cout<<"
1.Insertion
2.FindMin
";
cout<<"3.FindMax
4.Find
5.Copy
";
cout<<"6.Delete
7.Preorder
8.Inorder
";
cout<<"9.Postorder
10.height
";
cout<<"Enter the choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"New node's value ?";
cin>>a;
bst.insert(a,root);
break;
case 2:
if (root !=NULL)
{
min=bst.findmin(root);
cout<<"Min element : "<< min->element;
}
break;
case 3:
if (root !=NULL)
{
max=bst.findmax(root);
cout<<"Max element : "<< max->element;
}
break;
case 4:
cout<<"Search node : ";
cin>>findele;
if (root != NULL)
bst.find(findele,root);
break;
case 5:
bst.copy(root,root1);
bst.inorder(root1);
break;
case 6:
cout<<"Delete Node ?";
cin>>delele;
bst.del(delele,root);
bst.inorder(root);
break;
case 7:
cout<<"Preorder Printing... :";
bst.preorder(root);
break;
case 8:
cout<<"Inorder Printing.... :";
bst.inorder(root);
break;
case 9:
cout<<"Postorder Printing... :";
bst.postorder(root);
break;
case 10:
cout<<"Height and Depth is ";
cout<< bst.bsheight(root);
cout<<"No. of nodes:- "<< bst.nonodes(root);
break;
}
cout<<"Do u want to continue (y/n) ?";
cin>>ch;
}while(ch=='y');
return 0;
}