Implementing the binary search tree algorithm

write a C++ program to implement the binary search tree algorithm, and the in-order, pre-order, and post-order traversal algorithms. show your output for these three traversal algorithms on the date set 2,45,0,97,22,41,17,95,64,28.

can anyone help me out with this?

I am also trying to make Binary tree which can sort the data in ascending order. I have used recursion with which I am not fully comfortable as it is very difficult to dry run the program. I am trying to find new ways to avoid recursion. If anyone can provide me the same please help.

Here is my code. It is incomplete, but may help you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Binary Tree.cpp : Defines the entry point for the console application.
//

//To create Binary tree and realize TRAVERSE

#include "stdafx.h"
#include "iostream.h"

class tree
{
private:
	struct node
	{
		int data;
		node *left; //Binary tree have two branches carrying addresses of child
		node *right;
	}*p;

public:
	tree();
	~tree();
	void create_tree(int num);
	void traverse();
	void inorder(node *);
	void preorder(node *);
	void postorder(node *);


};

int main(int argc, char* argv[])
{
	tree t;
	t.create_tree(20);
	t.create_tree(17);
	t.create_tree(6);
	t.create_tree(18);
	t.create_tree(19);
	t.create_tree(1);
	t.traverse();
	return 0;
}

tree::tree()//Constructor
{
	p=NULL;
}

tree::~tree()//Destructor not made so far....wil make when return

{
	
}

void tree::create_tree(int num)//defination of member function outside class.
{
	node *q,*temp,*parent;
	q=p;
	parent=NULL;
	if(p==NULL)//FIRST ELEMENT OF TREE
	{
		p=new node;
		p->data=num;
		p->left=NULL;
		p->right=NULL;
		return;
	}
	else
	{
		temp=new node; //New element created. However has not been linked so far
		temp->data=num;
		temp->left=NULL;
		temp->right=NULL;

		while(q!=NULL)
		{
			if(num<q->data)//for LEFT side of root
			{
				parent=q;//needed to know the root
				q=q->left;
				if(q==NULL)
					parent->left=temp;	

			}
			else//For RIGHT side of root
			{
				parent=q;
				q=q->right;
				if(q==NULL)
					parent->right=temp;	
				

			}
		}

	}
		
			
}

void tree::traverse()
{	
	cout<<endl<<"Through Inorder technique"<<endl;
	inorder(p);

	cout<<endl<<"Through Preorder technique"<<endl;
	preorder(p);
	cout<<endl<<"Through Postorder technique"<<endl;
	postorder(p);


	/*node *q,*parent;
	if(p==NULL)
		cout<<endl<<"Tree is empty.";
	else
	{
		for(q=p;q!=NULL;q=q->left)
		{
			parent=q;
		}
		cout<<endl<<"Smallest numbers in tree is "<<parent->data;
	}*/

	

}

void tree::inorder(node *q)
{
	if(q!=NULL)
	{
		inorder(q->left); //By recurssion
		cout<<"\t "<<q->data<<flush;
		inorder(q->right);//By recurssion
	}

}


void tree::preorder(node *q)
{
	if(q!=NULL)
	{
		
		cout<<"\t "<<q->data<<flush;
		inorder(q->left); //By recurssion
		inorder(q->right);//By recurssion
	}
	

}

void tree::postorder(node *q)
{
	if(q!=NULL)
	{
	
		
		inorder(q->left); //By recurssion
		inorder(q->right);//By recurssion
		cout<<"\t "<<q->data<<flush;
	}
	

}



Thanks and Regards,
Abhishek
Topic archived. No new replies allowed.