473,396 Members | 2,002 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,396 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 2714
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.