-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBST implementation.cpp
More file actions
123 lines (122 loc) · 3.03 KB
/
BST implementation.cpp
File metadata and controls
123 lines (122 loc) · 3.03 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
//PROGRAM TO PERFORM BASIC OPERATIONS ON A BINARY SEARCH TREE (BST)
#include<bits/stdc++.h>
using namespace std;
struct tree{
int info;
struct tree *right,*left;
};
void insert(struct tree **ptr,int item){
if(*ptr==NULL){
*ptr=(struct tree *)malloc(sizeof(struct tree));
(*ptr)->left=(*ptr)->right=NULL;
(*ptr)->info=item;
return;
}
else {
if(item<(*ptr)->info){
insert(&((*ptr)->left),item);
}
else{
insert(&((*ptr)->right),item);
}
return;
}
}
void delete_tree(struct tree **ptr,int item){
struct tree *move,*back,*temp;
if(*ptr==NULL){
cout<<"\nEmpty tree..............\n";
return;
}
else{
move=*ptr;
back=move;
while(move->info!=item){
back=move;
if(item<move->info){
move=move->left;
}
else{
move=move->right;
}
}
if(move->left!=NULL && move->right!=NULL){
temp=move->right;
while(temp->left!=NULL){
back=temp;
temp=temp->left;
}
move->info=temp->info;
move=temp;
}
if(move->left==NULL && move->right==NULL){
if(back->right==move){
back->right=NULL;
}
else{
back->left=NULL;
}
free(move);
return;
}
if(move->left==NULL && move->right!=NULL){
if(back->left==move){
back->left=move->right;
}
else{
back->right=move->right;
}
free(move);
return;
}
if(move->left!=NULL && move->right==NULL){
if(back->left==move){
back->left=move->left;
}
else{
back->right=move->left;
}
free(move);
return;
}
}
}
void inorder(struct tree *ptr){
struct tree *move;
move=ptr;
if(move!=NULL){
inorder(move->left);
cout<<move->info<<" ";
inorder(move->right);
}
else
return;
}
int main(){
struct tree *root=NULL;
int item,ch,order;
char choice,ch1;
do{
cout<<"\n____________Tree MENU_______________\n\n";
cout<<"1.Insert.\n";
cout<<"2.Delete.\n";
cout<<"3.Traversal.\n";
cout<<"4.Exit.\n";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch){
case 1: cout<<"\nEnter the element to be inserted : ";
cin>>item;
insert(&root,item);
break;
case 2: cout<<"\nEnter the element to be deleted : ";
cin>>item;
delete_tree(&root,item);
break;
case 3: inorder(root);
break;
default: exit(0);
}
fflush(stdin);
}while(ch!=4);
}