-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprg19.cpp
More file actions
135 lines (134 loc) · 2.28 KB
/
Copy pathprg19.cpp
File metadata and controls
135 lines (134 loc) · 2.28 KB
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
//binary tree
#include<iostream>
using namespace std;
typedef struct tree
{
struct tree *left;
struct tree *right;
int data;
}TREENODE;
class bintree
{
private: TREENODE *tree;
public: bintree();
void maketree(int num);
void postfix(TREENODE *t);
void infix(TREENODE *t);
void prefix(TREENODE *t);
void setleft(int data,TREENODE *p,TREENODE *newnode);
void setright(int data,TREENODE *p,TREENODE *newnode);
TREENODE *getHead();
};
bintree::bintree()
{
tree=NULL;
}
void bintree::maketree(int num)
{
TREENODE *newnode,*p,*q;
newnode=(TREENODE *)malloc(sizeof(TREENODE));
newnode->data=num;
newnode->left=NULL;
newnode->right=NULL;
p=tree;
q=tree;
if(tree==NULL){
tree=newnode;
}
else{
while(q!=NULL && newnode->data!=p->data)
{
p=q;
if(newnode->data<p->data)
q=p->left;
else
q=p->right;
}
if(newnode->data==p->data)
cout<<"Trying to insert a duplicate element!!\n";
else if(newnode->data<p->data)
setleft(newnode->data,p,newnode);
else
setright(newnode->data,p,newnode);
}
}
void bintree::setleft(int data,TREENODE *p,TREENODE *newnode)
{
TREENODE *q;
q=p->left;
if(q!=NULL)
cout<<"Invalid location!";
else
p->left=newnode;
}
void bintree::setright(int data,TREENODE *p,TREENODE *newnode)
{
TREENODE *q;
q=p->right;
if(q!=NULL)
cout<<"Invalid location!";
else
p->right=newnode;
}
TREENODE *bintree::getHead()
{
return tree;
}
void bintree::infix(TREENODE *t)
{
if(t!=NULL){
infix(t->left);
cout<<t->data<<" ";
infix(t->right);
}
}
void bintree::postfix(TREENODE *t)
{
if(t!=NULL){
postfix(t->left);
postfix(t->right);
cout<<t->data<<" ";
}
}
void bintree::prefix(TREENODE *t)
{
if(t!=NULL){
cout<<t->data<<" ";
prefix(t->left);
prefix(t->right);
}
}
main()
{
bintree bt;
TREENODE *t1;
int ch,ele;
while(1)
{
cout<<"\nMenu\n";
cout<<"1.Insert node\n";
cout<<"2.Infix\n";
cout<<"3.Postfix\n";
cout<<"4.Prefix\n";
cout<<"5.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element to be inserted: ";
cin>>ele;
bt.maketree(ele);
break;
case 2: t1=bt.getHead();
bt.infix(t1);
break;
case 3: t1=bt.getHead();
bt.postfix(t1);
break;
case 4: t1=bt.getHead();
bt.prefix(t1);
break;
case 5: exit(0);
}
}
}