...::: Recent Updates :::...

Monday, April 25, 2011

BST WITHOUT recursion

/*Binary search tree creation without recursion
LICENCE:-GNU/GPL :)
Authore:-Maulin Desai :)*/
#include

#include

struct maulin

{

int n;

struct maulin *left;

struct maulin *right;

};

int main()

{

void create(struct maulin **,int );

void preorder_disp(struct maulin *);

void postorder_disp(struct maulin *);

void inorder_disp(struct maulin *);

//void dele(struct maulin **,struct maulin **);

//void search(struct maulin **,struct maulin **,int no);
int no;
int ch;
char dec='y';

struct maulin *root=(struct maulin *)malloc(sizeof(struct maulin));

struct maulin *f=(struct maulin *)malloc(sizeof(struct maulin));
f=NULL;

root->n=0;
do

{

printf("\n1)create\n2)preorder Disp\n3)exit\n4)Postorder disp\n5)inorder display\n6)delete");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("\nEnter Number to insert in tree:-\t");

scanf("%d",&no);

create(&root,no);

break;

case 2:

preorder_disp(root);

break;

case 4:

postorder_disp(root);

break;

case 5:

inorder_disp(root);

break;

/*case 6:

dele(&root,&f);

break;*/

case 3:

exit(0);

}

printf("\nDo you want to continue:-\t");

scanf("%s",&dec);

}while(dec=='y'||dec=='Y');

return 0;

}

void inorder_disp(struct maulin *d)

{

if(d!=NULL)

{

inorder_disp(d->left);

printf("\n%d",d->n);

inorder_disp(d->right);

}

}

void postorder_disp(struct maulin *g)

{

if(g!=NULL)

{

postorder_disp(g->left);

postorder_disp(g->right);

printf("\n%d",g->n);

}

}

void preorder_disp(struct maulin *h)

{

// printf("\n\n\n\n The value of root node is:-\t%d",h->n);

if(h!=NULL)

{

printf("\n\t%d",h->n);

//printf("\nTraversing from Left sight:-:(");

preorder_disp(h->left);

//printf("\nNow Traversing From Right sight:0:-\t%d",h->right);

preorder_disp(h->right);

}



}

void create(struct maulin **h,int no)

{

int flag=0;

struct maulin *temp=(struct maulin *)malloc(sizeof(struct maulin));

struct maulin *new_node=(struct maulin *)malloc(sizeof(struct maulin));

//new_node=NULL;

//temp=NULL;

printf("\n value of head node:-\t%d",(*h));

if((*h)->n==0)

{

printf("\nRoot Node created:-\t");

(*h)->n=no;

(*h)->left=NULL;

(*h)->right=NULL;

return;

}

else if((*h)->n
{

temp=(*h);

printf("\nTraversing from right in subtree");

b:

while(temp->right!=0)

{

if(temp->n
{

flag=0;

temp=temp->right;

}

if(temp->n>no)

{

printf("\nMaulin :-\t%d %d",temp->n,no);

flag=1;

break;

}

}

if(flag==0)

{

printf("\n\n\t\t\t\t\t\t\t node inserted at right sight dude:-%d\t\t\t\n",no);

new_node->left=NULL;

new_node->n=no;

new_node->right=NULL;

temp->right=new_node;

// temp->left=NULL;

temp=(*h);

return;

}

else

{

printf("\nJump down word");

goto a;

}

}

else if((*h)->n>no)

{

a:

printf("\nTraversing from Left in subtree:-\t");

temp=(*h);

while(temp->left!=NULL)

{

if(temp->n>no)

{

temp=temp->left;

}

else if(temp->n
{

printf("\nGoto up like mario game:-\t");

goto b;

}

}

printf("\n\n\n\t\t\t\t\t\t\tInserting node at left sightv in tree:-%d\t\t\t\t\n",no);

new_node->left=NULL;

new_node->n=no;

new_node->right=NULL;

temp->left=new_node;

//temp->right=NULL;

}

else

{

printf("\nNo condition satisfy:-\t");

}

}

No comments:

Post a Comment