-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree2.cpp
More file actions
149 lines (135 loc) · 3.91 KB
/
binarytree2.cpp
File metadata and controls
149 lines (135 loc) · 3.91 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
#include<iostream>
using namespace std;
struct node{
int key;
struct node *left;
struct node *right;
};
struct node *getnode(int value){
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->key=value;
newnode->right=NULL;
newnode->left=NULL;
return newnode;
}
struct node *insert(struct node *root,int value){
if(root==NULL){
return getnode(value);
}
if(root->key<value){
root->right = insert(root->right,value);
}
if(root->key>value){
root->left = insert(root->left,value);
}
return root;
}
void inorder(struct node *root){
if(root == NULL){
return ;
}
inorder(root->left);
cout<<root->key<<" ";
inorder(root->right);
}
int search(struct node *root,int key){
if(root==NULL){
return 0;
}
if(root->key==key){
return 1;
}
if(root->key>key){
return search(root->left,key);
}else{
return search(root->right,key);
}
}
int getrightmin(struct node *root){
struct node *tem =root;
while(tem->left!=NULL){
tem=tem->left;
}
return tem->key;
}
struct node *deletenode(struct node *root,int value){
if(root==NULL){
return NULL;
}
if(root->key>value){
root->left = deletenode(root->left,value);
}
else if(root->key<value){
root->right = deletenode(root->right,value);
}
else{
if(root->left==NULL && root->right==NULL){
free(root);
return NULL;
}
else if(root->left==NULL){
struct node *temp = root->right;
free(root);
return temp;
}
else if(root->right==NULL){
struct node *temp = root->left;
free(root);
return temp;
}
else{
int rightmin = getrightmin(root);
root->key=rightmin;
root->right = deletenode(root->right,rightmin);
}
}
}
int main(){
int ch;
struct node *root = NULL;
do{
cout<<"\n 1.Create Binary Tree"<<endl;
cout<<"\n 2.Inorder Traversal"<<endl;
cout<<"\n 3.Search Element"<<endl;
cout<<"\n4.delete a node from binary tree"<<endl;
cin>>ch;
switch(ch){
case 1:{
int n;
cout<<"Enter the no of nodes"<<endl;
cin>>n;
for(int i=0;i<n;i++){
int val;
cout<<"Enter the value"<<endl;
cin>>val;
root =insert(root,val);
}
}
break;
case 2:
inorder(root);
break;
case 3:{
int key;
cout<<"Enter the key you want to search"<<endl;
cin>>key;
int f = search(root,key);
if(f==0){
cout<<"The Key is not present in the Tree"<<endl;
}else{
cout<<"The Key is present in the tree"<<endl;
}
}
break;
case 4: {
int val;
cout<<"\nEnter the node value you want to delete"<<endl;
cin>>val;
root = deletenode(root,val);
}
break;
}
}while(ch!=5);
return 0;
}