-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
193 lines (185 loc) · 3.16 KB
/
Tree.cpp
File metadata and controls
193 lines (185 loc) · 3.16 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
}*root;
node* Insert(struct node *tree,int data);
node* Delete(struct node *tree,int data);
void Inorder(struct node *tree);
void preorder(struct node *tree);
void Postorder(struct node *tree);
node* FindMin(struct node *tree);
node* FindMax(node *tree);
main()
{
int choice,data,option=1;
while(option)
{
cout<<"\n\n1.Add Element\n";
cout<<"2.Delete Element\n";
cout<<"3.Inorder\n";
cout<<"4.Preorder\n";
cout<<"5.Postorder\n";
cout<<"6.Find Minimum element\n";
cout<<"7.Find Maximum element\n";
cout<<"8.Exit\n";
cout<<"Enter your choice :\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Your Element : ";
cin>>data;
root=Insert(root,data);
break;
case 2:
cout<<"Your Element : ";
cin>>data;
root=Delete(root,data);
cout<<data<<" Deleted\n";
break;
case 3:
cout<<"Inorder : \n";
Inorder(root);
break;
case 4:
cout<<"Preorder : \n";
preorder(root);
break;
case 5:
cout<<"Postorder : \n";
Postorder(root);
break;
case 6:
{
//When we declare new variable in swutch case it must be placed in block
node* newnode = FindMin(root);;
cout<<"Minimum element in tree is "<<newnode->data<<"\n";
}
break;
case 7:
{
node* newnode = FindMax(root);
cout<<"Maximum element in tree is "<<newnode->data<<"\n";
}
break;
case 8:
option=0;
break;
default:
cout<<"Wrong choice\n";
break;
}
}
return 0;
}
node* FindMax(node* tree)
{
while(tree->right!=NULL)
{
tree=tree->right;
}
return tree;
}
node* Insert(struct node *tree,int data)
{
if(tree==NULL)
{
struct node *newnode = new node();
newnode->data = data;
newnode->left=newnode->right=NULL;
return newnode;
}
else if(tree->data > data)
{
tree->left = Insert(tree->left,data);
}
else if(tree->data < data)
{
tree->right = Insert(tree->right,data);
}
return tree;
}
void preorder(struct node *tree)
{
if(tree == NULL)
{
return;
}
cout<<tree->data<<" ";
preorder(tree->left);
preorder(tree->right);
}
void Postorder(struct node *tree)
{
if(tree == NULL)
{
return;
}
Postorder(tree->left);
Postorder(tree->right);
cout<<tree->data<<" ";
}
void Inorder(struct node *tree)
{
if(tree == NULL)
{
return;
}
Inorder(tree->left);
cout<<tree->data<<" ";
Inorder(tree->right);
}
node* FindMin(struct node *tree)
{
while(tree->left != NULL)
{
tree=tree->left;
}
return tree;
}
node* Delete(struct node *tree,int data)
{
if(tree == NULL)
{
return tree;
}
else if(tree->data < data)
{
tree->right=Delete(tree->right,data);
}
else if(tree->data > data)
{
tree->left=Delete(tree->left,data);
}
else
{
if(tree->right==NULL && tree->left == NULL)
{
delete tree;
tree=NULL;
}
else if(tree->right==NULL)
{
struct node *newnode = tree;
tree=tree->left;
delete newnode;
}
else if(tree->left == NULL)
{
struct node *newnode = tree;
tree=tree->right;
delete newnode;
}
else
{
struct node *newnode = FindMin(tree->right);
tree->data=newnode->data;
tree->right=Delete(tree->right,newnode->data);
}
}
return tree;
}