473,466 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

stack

5 New Member
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 2717
D_C
293 Contributor
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.