0% found this document useful (0 votes)
59 views4 pages

Institute of Technology:, Dundigal, Hyderabad 500 043

This program implements a binary search tree (BST) data structure using C. It includes functions to create the BST by adding nodes, traverse the BST using inorder traversal, and push/pop nodes onto a stack. The main function displays a menu and allows the user to choose between creating the BST, performing inorder traversal, or exiting the program. Functions are defined to create and add nodes to the BST, push/pop nodes to/from a stack, check if the stack is empty, and perform inorder traversal using the stack to keep track of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views4 pages

Institute of Technology:, Dundigal, Hyderabad 500 043

This program implements a binary search tree (BST) data structure using C. It includes functions to create the BST by adding nodes, traverse the BST using inorder traversal, and push/pop nodes onto a stack. The main function displays a menu and allows the user to choose between creating the BST, performing inorder traversal, or exiting the program. Functions are defined to create and add nodes to the BST, push/pop nodes to/from a stack, check if the stack is empty, and perform inorder traversal using the stack to keep track of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Department of Information Technology

Lab: DS PROGRAMING LAB Roll no: !R"A"#$


Program%&'p(no: )&&*+$
Date: %#,%"#-
Name of the Program:
#include<stdio.h>
#include<stdlib.h>
struct BST
{
int data;
struct BST *left,*right;
}node;
int c,sum;
struct stack
{
struct BST*t;
struct stack*net;
};
struct BST *root!"#$$,*cur,*tem%;
int odd&int '
{
if&()!!*++<,*++>,**'
{
%rintf&-entered element is odd or not in range of ,*.,**-';
return );
}
}
/oid create&'
{
tem%!root;
cur!&struct BST*'malloc&si0eof&struct BST'';
scanf&-(d-,1cur.>data';
cur.>left!"#$$;
cur.>right!"#$$;
if&tem%!!"#$$'
root!cur;
else
{
2hile&tem%3!"#$$'
{
if&&cur.>data'<&tem%.>data''
{
if&tem%.>left!!"#$$'
{
tem%.>left!cur;
return;
}
else
tem%!tem%.>left;
4nstitute of Technolog5, 6undigal, 75derabad 8** *9:
Department of Information Technology
Lab: DS PROGRAMING LAB Roll no: !R"A"#$
Program%&'p(no: )&&*+$
Date: %#,%"#-
Name of the Program:
}
else
{
if&tem%.>right!!"#$$'
{
tem%.>right!cur;
return;
}
else
tem%!tem%.>right;
}
}
}
}
/oid %ush&struct stack**to%,struct BST*t'
{
struct stack*ne2!&struct stack*'malloc&si0eof&struct stack'';
if&ne2!!"#$$'
{
%rintf&-;n stack o/erflo2-';
return;
}
ne2.>t!t;
ne2.>net!&*to%';
&*to%'!ne2;
}
int isem%t5&struct stack*to%'
{
return&to%!!"#$$'<,=*;
}
struct BST*%o%&struct stack**to%'
{
struct BST*res;
struct stack*to%,;
if&isem%t5 &*to%''
%rintf&-;n stack underflo2-';
else
{
to%,!*to%;
res!to%,.>t;
*to%!to%,.>net;
free&to%,';
return res;
}
4nstitute of Technolog5, 6undigal, 75derabad 8** *9:
Department of Information Technology
Lab: DS PROGRAMING LAB Roll no: !R"A"#$
Program%&'p(no: )&&*+$
Date: %#,%"#-
Name of the Program:
}
/oid inorder&struct BST*root'
{
struct BST*cur!root;
struct stack *s!"#$$;
2hile&,'
{
if&cur3!"#$$'
{
%ush&1s,cur';
cur!cur.>left;
}
else
{
if&3isem%t5&s''
{
cur!%o%&1s';
%rintf&-;t (d-,cur.>data';
cur!cur.>right;
}
else
break;
}
}
}
int main&'
{
int ch,;
%rintf&-;n menu o%tion;n-';
%rintf&-;n,.create;n).inorder;n:.eit;n-';
2hile&,'
{
%rintf&-;n enter 5our choice=-';
scanf&-;n(d-,1ch';
s2itch&ch'
{
case ,= create&';
break;
case )= inorder&root';
break;
case := eit&*';
break;
default = %rintf&-;n in/alid o%tion=-';
}
4nstitute of Technolog5, 6undigal, 75derabad 8** *9:
Department of Information Technology
Lab: DS PROGRAMING LAB Roll no: !R"A"#$
Program%&'p(no: )&&*+$
Date: %#,%"#-
Name of the Program:
}
}
O.tp.t=.

4nstitute of Technolog5, 6undigal, 75derabad 8** *9:

You might also like