473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

stack

I have to make the program of inorder ,preorder,postorder traversal of the binary search tree using recursion in stacks.I have made it by recursion only.How can I make this .
I have written the code for the recursive method.

void gettree(struct node **, int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *left;
struct node *right;
int data;

};
void main()
{
struct node *bt;
int a,n,i;
clrscr();
bt=NULL;
printf("\n\nEnter the no of the nodes you want to enter :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter the elments of the tree :");
scanf("%d",&a);
gettree (&bt,a);
}
printf("\n\nElements in INORDER are : \n\n");
inorder(bt);
printf("\n\nElements in PREORDER are : \n\n");
preorder(bt);
printf("\n\nElements in POSTORDER are : \n\n");
postorder(bt);
getch();
}

void gettree(struct node **root,int a)
{
struct node *temp;
if(*root==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
temp->data=a;
*root=temp;
return;
}
if((*root)->data>a)
gettree(&((*root)->left),a);
else
gettree(&((*root)->right),a);

}
void inorder(struct node *bt)
{
if(bt!=NULL)
{
inorder(bt->left);
printf("[ %d ]\t",bt->data);
inorder(bt->right);

}
else
return ;
}

void postorder(struct node *bt)
{
if(bt!=NULL)
{
postorder(bt->left);
postorder(bt->right);
printf("[ %d ]\t",bt->data);
}
else
return ;
}

void preorder(struct node *bt)
{
if(bt!=NULL)
{
printf("[ %d ]\t",bt->data);
preorder(bt->left);
preorder(bt->right);

}
else
return ;
}
Oct 5 '06 #1
1 2706
D_C
293 100+
I have made it by recursion only. How can I make this .
Is that the question? If so, I hope you clarify.
Oct 5 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
8
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
1
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting...
11
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.