/* C program to implement binary search tree and display all node without using recursion */
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *lc,*rc;
};
typedef struct node NODE;
int top=-1,stack[20];
void create(NODE **,int);
void inorder(NODE *);
void main()
{
NODE *root=NULL;
int data;
char ch;
clrscr();
printf("Enter the root element:- ");
scanf("%d",&data);
create(&root,data);
while(1)
{
printf("\n MENU \n");
printf("\n1.Insert.");
printf("\n2.Display.");
printf("\n3.Exit\n");
printf("\nEnter your choice:- ");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case '1':
printf("\nEnter the data :- ");
scanf("%d",&data);
create(&root,data);
break;
case '2':
printf("\nIn-order Traversal:\n");
inorder(root);
break;
case '3':
exit(1);
default:
printf("\nYou have entered a wrong choice.");
}
}
}
/* Creating Binary Search Tree */
void create(NODE **root,int d)
{
if(*root==NULL)
{
*root=(NODE *)malloc(sizeof(NODE));
(*root)->data=d;
(*root)->lc=NULL;
(*root)->rc=NULL;
return;
}
else
{
if( d < (*root)->data )
create(&((*root)->lc),d);
if( d > (*root)->data )
create(&((*root)->rc),d);
if( d == (*root)->data )
printf("\nData already exist.");
}
}
/* Displaying all elements of binary search tree using stack */
void inorder(NODE *root)
{
NODE *ptr;
(ptr)=(root);
while(top!=-1||(ptr)!=NULL)
{
if((ptr)!=NULL)
{
stack[++top]=(ptr);
(ptr)=(ptr)->lc;
}
else
{
(ptr)=stack[top--];
printf("%d\t",(ptr)->data);
(ptr)=(ptr)->rc;
}
}
}
/* Output of Binary Search Tree without using recursion Program */
For more related to Data Structure see List of Data Structure Programs. If you like this program, Please share and comment to improve this blog.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *lc,*rc;
};
typedef struct node NODE;
int top=-1,stack[20];
void create(NODE **,int);
void inorder(NODE *);
void main()
{
NODE *root=NULL;
int data;
char ch;
clrscr();
printf("Enter the root element:- ");
scanf("%d",&data);
create(&root,data);
while(1)
{
printf("\n MENU \n");
printf("\n1.Insert.");
printf("\n2.Display.");
printf("\n3.Exit\n");
printf("\nEnter your choice:- ");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case '1':
printf("\nEnter the data :- ");
scanf("%d",&data);
create(&root,data);
break;
case '2':
printf("\nIn-order Traversal:\n");
inorder(root);
break;
case '3':
exit(1);
default:
printf("\nYou have entered a wrong choice.");
}
}
}
/* Creating Binary Search Tree */
void create(NODE **root,int d)
{
if(*root==NULL)
{
*root=(NODE *)malloc(sizeof(NODE));
(*root)->data=d;
(*root)->lc=NULL;
(*root)->rc=NULL;
return;
}
else
{
if( d < (*root)->data )
create(&((*root)->lc),d);
if( d > (*root)->data )
create(&((*root)->rc),d);
if( d == (*root)->data )
printf("\nData already exist.");
}
}
/* Displaying all elements of binary search tree using stack */
void inorder(NODE *root)
{
NODE *ptr;
(ptr)=(root);
while(top!=-1||(ptr)!=NULL)
{
if((ptr)!=NULL)
{
stack[++top]=(ptr);
(ptr)=(ptr)->lc;
}
else
{
(ptr)=stack[top--];
printf("%d\t",(ptr)->data);
(ptr)=(ptr)->rc;
}
}
}
/* Output of Binary Search Tree without using recursion Program */
Output of BST without using recursion |
Output of BST without using recursion |
For more related to Data Structure see List of Data Structure Programs. If you like this program, Please share and comment to improve this blog.
Title is missleading! you used recursion while create binary search tree
ReplyDeletesir you used recursion. please submit without recursion.
ReplyDeleteYou used recursion and give a title without recursion !!
ReplyDeleteDo you mind if I quote a few of your posts as long as I provide credit and sources back to your post? My post site is in the exact same niche as yours and my users would really benefit from some of the information you present here. Please let me know if this okay with you. Cheers! web developer singapore
ReplyDeleteThanks for the good writeup. It actually was once a leisure account it. Glance advanced to far brought agreeable from you! By the way, how can we communicate? maternity shops Singapore
ReplyDeleteHello there! I could have sworn I've been to this blog before but after reading through some of the blog postI realized it's new to me. Anyways, I'm definitely happy I found it and I'll be bookmarking and checking back frequently! seo expert singapore
ReplyDeleteAw, this was a very nice post. In thought I want to put in writing like this moreover – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and in no way appear to get something done. sem singapore
ReplyDeleteGreat write-up, I am regular visitor of one’s site, maintain up the nice operate, and It's going to be a regular visitor for a lengthy time. best private jets
ReplyDeleteThanks on your marvelous posting! I seriously enjoyed reading it, you happen to be a great author.I will be sure to bookmark your post and definitely will come back someday. I want to encourage you to ultimately continue your great work, have a nice weekend! rented car in wayanad
ReplyDeleteOh my goodness! a tremendous article dude. Thank you Nonetheless I am experiencing difficulty with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss downside? Anybody who knows kindly respond. Thnkx
ReplyDeletemarketing companies in singapore