<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>binary tree | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/binary-tree/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Mon, 31 Jan 2011 12:21:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>C++ program for creation and traversal of a Binary Tree</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-for-creation-and-traversal-of-a-binary-tree/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-for-creation-and-traversal-of-a-binary-tree/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Wed, 02 Feb 2011 12:19:34 +0000</pubDate>
				<category><![CDATA[Data structure]]></category>
		<category><![CDATA[C Programs]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[linked list]]></category>
		<category><![CDATA[traversal]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1271</guid>

					<description><![CDATA[<p>#include #include #include struct tree_node { tree_node *left; tree_node *right; int data; } ; class bst { tree_node *root; public: bst() { root=NULL; } int isempty() { return(root==NULL); } void insert(int item); void inordertrav(); void inorder(tree_node *); void postordertrav(); void postorder(tree_node *); void preordertrav(); void preorder(tree_node *); }; void bst::insert(int item) { tree_node *p=new tree_node;</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-for-creation-and-traversal-of-a-binary-tree/">C++ program for creation and traversal of a Binary Tree</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<pre lang="cpp">
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct tree_node
{
	tree_node *left;
	tree_node *right;
	int data;
} ;
class bst
{
	tree_node *root;
	public:
	bst()
	{
		root=NULL;
	}
	int isempty() 
	{
		return(root==NULL);
	}
	void insert(int item);
	void inordertrav();
	void inorder(tree_node *);
	void postordertrav();
	void postorder(tree_node *);
	void preordertrav();
	void preorder(tree_node *);
};
void bst::insert(int item)
{
	tree_node *p=new tree_node;
	tree_node *parent;
	p->data=item;
	p->left=NULL;
	p->right=NULL;
	parent=NULL;
	if(isempty())
		root=p;
	else
	{
		tree_node *ptr;
		ptr=root;
		while(ptr!=NULL)
		{
			parent=ptr;
			if(item>ptr->data)		
				ptr=ptr->right;
			else
				ptr=ptr->left;
		}	
		if(item<parent->data)
			parent->left=p;
		else
			parent->right=p;
	}
}
void bst::inordertrav()
{
	inorder(root);
}
void bst::inorder(tree_node *ptr)
{
	if(ptr!=NULL)
	{
		inorder(ptr->left);
		cout<<"  "<<ptr->data<<"     ";
		inorder(ptr->right);
	}
}
void bst::postordertrav()
{
	postorder(root);
}
void bst::postorder(tree_node *ptr)
{
	if(ptr!=NULL)
	{
		postorder(ptr->left);
		postorder(ptr->right);
		cout<<"  "<<ptr->data<<"     ";
	}
}
void bst::preordertrav()
{
	preorder(root);
}
void bst::preorder(tree_node *ptr)
{
	if(ptr!=NULL)
	{
		cout<<"  "<<ptr->data<<"     ";
		preorder(ptr->left);
		preorder(ptr->right);
	}
}
void main()
{
	bst b;
	b.insert(52);
	b.insert(25);
	b.insert(50);
	b.insert(15);
	b.insert(40);
	b.insert(45);
	b.insert(20); cout<<"inorder"<<endl;
	b.inordertrav();
	cout<<endl<<"postorder"<<endl;
	b.postordertrav();
	cout<<endl<<"preorder"<<endl;
	b.preordertrav();
	getch();
}
</pre><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-for-creation-and-traversal-of-a-binary-tree/">C++ program for creation and traversal of a Binary Tree</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-for-creation-and-traversal-of-a-binary-tree/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		
		
			</item>
		<item>
		<title>C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-dynamic-programming-algorithm-to-solve-the-optimal-binary-search-tree-problem/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-dynamic-programming-algorithm-to-solve-the-optimal-binary-search-tree-problem/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 11 Mar 2010 17:39:44 +0000</pubDate>
				<category><![CDATA[Data structure]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[C Programs]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[optimal binary search tree]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1042</guid>

					<description><![CDATA[<p>/* Write a C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem */ #include #include #include using namespace std; #define MAX 10 int find(int i,int j); void print(int,int); int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m; char idnt[7][10]; main() { cout >n; cout</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-dynamic-programming-algorithm-to-solve-the-optimal-binary-search-tree-problem/">C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>/* Write a C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem */</p>
<pre lang="cpp">
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
#define MAX 10
int find(int i,int j);
void print(int,int);
int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m;
char idnt[7][10];

main()
{
	cout << "enter the no, of identifiers";
	cin >>n;
	cout <<"enter identifiers";
	for(i=1;i<=n;i++)
	gets(idnt[i]);
	cout <<"enter success propability for identifiers";
	for(i=1;i<=n;i++)
		cin >>p[i];
	cout << "enter failure propability for identifiers";
	for(i=0;i<=n;i++)
		cin >> q[i];
	for(i=0;i<=n;i++)
	{
		w[i][i]=q[i];
		c[i][i]=r[i][i]=0;
		w[i][i+1]=q[i]+q[i+1]+p[i+1];
		r[i][i+1]=i+1;
		c[i][i+1]=q[i]+q[i+1]+p[i+1];
	}
	w[n][n]=q[n];
	r[n][n]=c[n][n]=0;
	for(m=2;m<=n;m++)
	{
		for(i=0;i<=n-m;i++)
		{
		     j=i+m;
		     w[i][j]=w[i][j-1]+p[j]+q[j];
		     k=find(i,j);
		     r[i][j]=k;
		     c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
		}
	}
       cout <<"\n";
       print(0,n); }

int find(int i,int j)
{
int min=2000,m,l;
for(m=i+1;m<=j;m++)
if(c[i][m-1]+c[m][j]<min)
{
min=c[i][m-1]+c[m][j];
l=m;
}
return l;
}
void print(int i,int j)
{
if(i<j)
puts(idnt[r[i][j]]);
else
return;
print(i,r[i][j]-1);
print(r[i][j],j);
}
</pre>
<p><strong>OUTPUT</strong></p>
<p>enter the no, of identifiers4<br />
enter identifiers<br />
do<br />
if<br />
int<br />
while<br />
enter success propability for identifiers3 3 1 1<br />
enter failure propability for identifiers2 3 1 1 1</p>
<p>tree in preorder form<br />
if<br />
do<br />
int<br />
while</p><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-dynamic-programming-algorithm-to-solve-the-optimal-binary-search-tree-problem/">C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-dynamic-programming-algorithm-to-solve-the-optimal-binary-search-tree-problem/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>C++ program that uses non-recursive functions to traverse a binary tree in Post-order</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-post-order/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-post-order/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 11 Mar 2010 17:22:47 +0000</pubDate>
				<category><![CDATA[Data structure]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[Post-order]]></category>
		<category><![CDATA[non-recursive functions]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1032</guid>

					<description><![CDATA[<p>/* Write C++ program that uses non-recursive functions to traverse a binary tree in Post-order */ #include #include #include class node { public: class node *left; class node *right; int data; }; class tree: public node { public: int stk[50],top; node *root; tree() { root=NULL; top=0; } void insert(int ch) { node *temp,*temp1; if(root== NULL)</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-post-order/">C++ program that uses non-recursive functions to traverse a binary tree in Post-order</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>/* Write C++ program that uses non-recursive functions to traverse a binary tree in Post-order */</p>
<pre lang="cpp">
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class node
{
public:
class node *left;
class node *right;
int data;
};

class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
	node *temp,*temp1;
	if(root== NULL)
	{
		root=new node;
		root->data=ch;
		root->left=NULL;
		root->right=NULL;
		return;
	}
	temp1=new node;
	temp1->data=ch;
	temp1->right=temp1->left=NULL;
	temp=search(root,ch);
	if(temp->data>ch)
		temp->left=temp1;
	else
		temp->right=temp1;

}
node *search(node *temp,int ch)
{
	if(root== NULL)
	{
		cout <<"no node present";
		return NULL;
	}
	if(temp->left==NULL && temp->right== NULL)
		return temp;

	if(temp->data>ch)
	     {  if(temp->left==NULL) return temp;
		search(temp->left,ch);}
	else
	      { if(temp->right==NULL) return temp;
	      search(temp->right,ch);

}              }

void display(node *temp)
{
	if(temp==NULL)
	 return ;
	display(temp->left);
       cout<<temp->data << " ";
	display(temp->right);
}
void postorder( node *root)
{
	node *p;
	p=root;
	top=0;

	while(1)
	{
	while(p!=NULL)
	{
		stk[top]=p->data;
		top++;
		if(p->right!=NULL)
			stk[top++]=-p->right->data;
		p=p->left; 
	}
	while(stk[top-1] > 0 || top==0)
	{
	   if(top==0) return;
	   cout << stk[top-1] <<" ";
	   p=pop(root);
	}
	if(stk[top-1]<0)
	{
	  stk[top-1]=-stk[top-1];
	  p=pop(root);
      }	}

}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
void main()
{
	tree t1;
	int ch,n,i;
	clrscr();
	while(1)
	{
		cout <<"\n1.INSERT\n2.DISPLAY 3.POSTORDER TRAVERSE\n4.EXIT\nEnter your choice:";
		cin >> ch;
		switch(ch)
		{
		case 1:   cout <<"enter no of elements to insert:";
			  cout<<"\n enter the elements";
			  cin >> n;
			  for(i=1;i<=n;i++)
			  {  cin >> ch;
			     t1.insert(ch);
			  }
			   break;
		case 2:   t1.display(t1.root);break;
		case 3:   t1.postorder(t1.root); break;
		case 4:   exit(1);
		}
	}
}

</pre>
<p><strong>OUTPUT</strong></p>
<p>1.INSERT<br />
2.DISPLAY 3.POSTORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:1<br />
enter no of elements to insert:<br />
 enter the elements7<br />
5 24 36 11 44 2 21</p>
<p>1.INSERT<br />
2.DISPLAY 3.POSTORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:2<br />
2 5 11 21 24 36 44</p>
<p>1.INSERT<br />
2.DISPLAY 3.POSTORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:3<br />
2 21 11 44 36 24 5</p>
<p>1.INSERT<br />
2.DISPLAY 3.POSTORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:4</p><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-post-order/">C++ program that uses non-recursive functions to traverse a binary tree in Post-order</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-post-order/feed/</wfw:commentRss>
			<slash:comments>54</slash:comments>
		
		
			</item>
		<item>
		<title>C++ program that uses non-recursive functions to traverse a binary tree in In-order</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-in-order/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-in-order/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 11 Mar 2010 17:20:22 +0000</pubDate>
				<category><![CDATA[Data structure]]></category>
		<category><![CDATA[programs]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[non-recursive]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[In-order]]></category>
		<category><![CDATA[binary tree]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1030</guid>

					<description><![CDATA[<p>/* Write C++ program that uses non-recursive functions to traverse a binary tree in In-order */ #include #include #include using namespace std; class node { public: class node *left; class node *right; int data; }; class tree: public node { public: int stk[50],top; node *root; tree() { root=NULL; top=0; } void insert(int ch) { node</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-in-order/">C++ program that uses non-recursive functions to traverse a binary tree in In-order</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>/* Write C++ program that uses non-recursive functions to traverse a binary tree in In-order */</p>
<pre lang="pre">
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};

class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
	node *temp,*temp1;
	if(root== NULL)
	{
		root=new node;
		root->data=ch;
		root->left=NULL;
		root->right=NULL;
		return;
	}
	temp1=new node;
	temp1->data=ch;
	temp1->right=temp1->left=NULL;
	temp=search(root,ch);
	if(temp->data>ch)
		temp->left=temp1;
	else
		temp->right=temp1;

}

node *search(node *temp,int ch)
{
	if(root== NULL)
	{
		cout <<"no node present";
		return NULL;
	}
	if(temp->left==NULL && temp->right== NULL)
		return temp;

	if(temp->data>ch)
	     {  if(temp->left==NULL) return temp;
		search(temp->left,ch);}
	else
	      { if(temp->right==NULL) return temp;
	      search(temp->right,ch);

}              }

void display(node *temp)
{
	if(temp==NULL)
	    return ;
	display(temp->left); 
	cout<<temp->data;
	display(temp->right);
}
void inorder( node *root)
{
	node *p;
	p=root;
	top=0;
	do
	{
		while(p!=NULL)
		{
			stk[top]=p->data;
			top++;
			p=p->left;
		}
		if(top>0)
		{
			p=pop(root);
			cout << p->data;
			p=p->right;
		}
	}while(top!=0 || p!=NULL);
}


node * pop(node *p)
{
	int ch;
	ch=stk[top-1];
	if(p->data==ch)
	{
		top--;
		return p;
	}
	if(p->data>ch)
		pop(p->left);
	else
		pop(p->right);
}
};

main()
{
	tree t1;
	int ch,n,i;
	while(1)
	{
		cout <<"\n1.INSERT\n2.DISPLAY 3.INORDER TRAVERSE\n4.EXIT\nEnter your choice:";
		cin >> ch;
		switch(ch)
		{
		case 1:   cout <<"enter no of elements to insert:";
			  cin >> n;
			  for(i=1;i<=n;i++)
			  {  cin >> ch;
			     t1.insert(ch);
			  }
			   break;
		case 2:   t1.display(t1.root);break;
		case 3:   t1.inorder(t1.root); break;
		case 4:   exit(1);
		}
	}
}
</pre>
<p><strong>OUTPUT</strong></p>
<p>1.INSERT<br />
2.DISPLAY 3.INORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:1<br />
enter no of elements to inser<br />
5 24 36 11 44 2 21</p>
<p>1.INSERT<br />
2.DISPLAY 3.INORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:3<br />
251121243644</p>
<p>1.INSERT<br />
2.DISPLAY 3.INORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:3<br />
251121243644</p>
<p>1.INSERT<br />
2.DISPLAY 3.INORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:4</p><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-in-order/">C++ program that uses non-recursive functions to traverse a binary tree in In-order</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-in-order/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>C++ program that uses non-recursive functions to traverse a binary tree in Pre-order</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-pre-order/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-pre-order/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 11 Mar 2010 17:16:36 +0000</pubDate>
				<category><![CDATA[Data structure]]></category>
		<category><![CDATA[Datastructure]]></category>
		<category><![CDATA[C Programs]]></category>
		<category><![CDATA[non-recursive]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[traverse]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1028</guid>

					<description><![CDATA[<p>/* Write C++ program that uses non-recursive functions to traverse a binary tree in Pre-order */ #include #include #include using namespace std; class node { public: class node *left; class node *right; int data; }; class tree: public node { public: int stk[50],top; node *root; tree() { root=NULL; top=0; } void insert(int ch) { node</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-pre-order/">C++ program that uses non-recursive functions to traverse a binary tree in Pre-order</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>/* Write C++ program that uses non-recursive functions to traverse a binary tree in Pre-order */</p>
<pre lang="cpp">
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};

class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
	node *temp,*temp1;
	if(root== NULL)
	{
		root=new node;
		root->data=ch;
		root->left=NULL;
		root->right=NULL;
		return;
	}
	temp1=new node;
	temp1->data=ch;
	temp1->right=temp1->left=NULL;
	temp=search(root,ch);
	if(temp->data>ch)
		temp->left=temp1;
	else
		temp->right=temp1;

}

node *search(node *temp,int ch)
{
	if(root== NULL)
	{
		cout <<"no node present";
		return NULL;
	}
	if(temp->left==NULL && temp->right== NULL)
		return temp;

	if(temp->data>ch)
	     {  if(temp->left==NULL) return temp;
		search(temp->left,ch);}
	else
	      { if(temp->right==NULL) return temp;
	      search(temp->right,ch);

}              }

void display(node *temp)
{
	if(temp==NULL)
	 return ;
	display(temp->left);
       cout<<temp->data <<" ";
	display(temp->right);
}
void preorder( node *root)
{
	node *p,*q;
	p=root;
	q=NULL;
	top=0;

	while(p!=NULL)
	{
		cout <<p->data  << " ";
		if(p->right!=NULL)
		{
			stk[top]=p->right->data;
			top++;
		}
		p=p->left;
		if(p==NULL && top>0)
	{
		p=pop(root);
	}
	}
}

node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
main()
{
	tree t1;
	int ch,n,i;
	while(1)
	{
		cout <<"\n1.INSERT\n2.DISPLAY 3.PREORDER TRAVERSE\n4.EXIT\nEnter your choice:";
		cin >> ch;
		switch(ch)
		{
		case 1:   cout <<"enter no of elements to insert:";
			  cout<<"\n enter the elements";
			  cin >> n;
			  for(i=1;i<=n;i++)
			  {  cin >> ch;
			     t1.insert(ch);
			  }
			   break;
		case 2:   t1.display(t1.root);break;
		case 3:   t1.preorder(t1.root); break;
		case 4:   exit(1);
		}
	}
}
</pre>
<p><strong>OUTPUT</strong></p>
<p>1.INSERT<br />
2.DISPLAY 3.PREORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:1<br />
enter no of elements to insert<br />
 enter the elements7<br />
5 24 36 11 44 2 21</p>
<p>1.INSERT<br />
2.DISPLAY 3.PREORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:2<br />
2 5 11 21 24 36 44</p>
<p>1.INSERT<br />
2.DISPLAY 3.PREORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:3<br />
5 2 24 11 21 36 44</p>
<p>1.INSERT<br />
2.DISPLAY 3.PREORDER TRAVERSE<br />
4.EXIT<br />
Enter your choice:4</p><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-pre-order/">C++ program that uses non-recursive functions to traverse a binary tree in Pre-order</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-that-uses-non-recursive-functions-to-traverse-a-binary-tree-in-pre-order/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>C++ program to perform Insert, Delete, Search an element into a binary search tree</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-to-perform-insert-delete-search-an-element-into-a-binary-search-tree/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-to-perform-insert-delete-search-an-element-into-a-binary-search-tree/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Wed, 10 Mar 2010 15:14:15 +0000</pubDate>
				<category><![CDATA[Data structure]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[C++ Programs]]></category>
		<category><![CDATA[Delete]]></category>
		<category><![CDATA[Insert]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=995</guid>

					<description><![CDATA[<p>/* Write a C++ program to perform the following operations:<br />
a) Insert an element into a binary search tree.<br />
b) Delete an element from a binary search tree.<br />
c) Search for a key element in a binary search tree. */</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-to-perform-insert-delete-search-an-element-into-a-binary-search-tree/">C++ program to perform Insert, Delete, Search an element into a binary search tree</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>/* Write a C++ Data structure program to perform the following operations:<br />
a) Insert an element into a binary search tree.<br />
b) Delete an element from a binary search tree.<br />
c) for a key element in a binary search tree. */</p>
<pre lang="cpp">
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;

main()
{
	int ch,y;
	for(i=1;i<40;i++)
	tree[i]=-1;
	while(1)
	{
cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:";
		cin >> ch;
		switch(ch)
		{
		case 1:
			cout <<"enter the element to insert";
			cin >> ch;
			insert(1,ch);
			break;
		case 2:
			cout <<"enter the element to delete";
			cin >>x;
			y=search(1);
			if(y!=-1) delte(y);
			else cout<<"no such element in tree";
			break;
		case 3:
			display(1);
			cout<<"\n";
			for(int i=0;i<=32;i++)
			cout <<i;
			cout <<"\n";
			break;
case 4:
			cout <<"enter the element to search:";
			cin >> x;
			y=search(1);
			if(y == -1) cout <<"no such element in tree";
			else cout <<x << "is in" <<y <<"position";
			break;
		case 5:
			exit(0);
		}
	}
}

void insert(int s,int ch )
{
	int x;
	if(t==1)
	{
		tree[t++]=ch;
		return;
	}
	x=search1(s,ch);
	if(tree[x]>ch)
		tree[2*x]=ch;
	else
		tree[2*x+1]=ch;
	t++;
}
void delte(int x)
{
	if( tree[2*x]==-1 && tree[2*x+1]==-1)
		tree[x]=-1;
	else if(tree[2*x]==-1)
	      {	tree[x]=tree[2*x+1];
		tree[2*x+1]=-1;
	      }
	else if(tree[2*x+1]==-1)
	      {	tree[x]=tree[2*x];
		tree[2*x]=-1;
	      }
	else
	{
	  tree[x]=tree[2*x];
	  delte(2*x);
	}
	t--;
}

int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}

void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}

int search1(int s,int ch)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}
</pre>
<p><strong>OUTPUT</strong><br />
1.INSERT<br />
2.DELETE<br />
3.DISPLAY<br />
4.SEARCH<br />
5.EXIT<br />
Enter your choice:3</p>
<p>no element in tree:<br />
0123456789011121314151617181920212223242526272829303132</p>
<p>1.INSERT<br />
2.DELETE<br />
3.DISPLAY<br />
4.SEARCH<br />
5.EXIT<br />
Enter your choice:1</p>
<p>Enter the element to insert 10<br />
1.INSERT<br />
2.DELETE<br />
3.DISPLAY<br />
4.SEARCH<br />
5.EXIT<br />
Enter your choice:4</p>
<p>Enter the element to search: 10<br />
10 is in 1 position<br />
1.INSERT<br />
2.DELETE<br />
3.DISPLAY<br />
4.SEARCH<br />
5.EXIT</p>
<p>Enter your choice:5</p><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-to-perform-insert-delete-search-an-element-into-a-binary-search-tree/">C++ program to perform Insert, Delete, Search an element into a binary search tree</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-data-structure/c-program-to-perform-insert-delete-search-an-element-into-a-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
	</channel>
</rss>
