-
Notifications
You must be signed in to change notification settings - Fork 0
/
prac_BST.cpp
169 lines (148 loc) · 3.1 KB
/
prac_BST.cpp
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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
if(root == NULL) {
root = tempNode;
}else {
current = root;
parent = NULL;
while(1) {
parent = current;
if(data < parent->data) {
current = current->leftChild;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}
else {
current = current->rightChild;
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}
struct node* search(int data) {
struct node *current = root;
printf("Visiting elements: ");
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
if(current->data > data) {
current = current->leftChild;
}
else {
current = current->rightChild;
}
if(current == NULL) {
return NULL;
}
}
return current;
}
void max(){
node* temp=root;
while(temp!=NULL){
if(temp->rightChild==NULL){
printf("Max is %d",temp->data);
temp=temp->rightChild;
}
else{
temp=temp->rightChild;
}
}
}
void min(){
node* temp=root;
while(temp!=NULL){
if(temp->leftChild==NULL){
printf("Min is %d",temp->data);
temp=temp->leftChild;
}
else{
temp=temp->leftChild;
}
}
}
void preorder(struct node* root) {
if(root != NULL) {
printf("%d ",root->data);
preorder(root->leftChild);
preorder(root->rightChild);
}
}
void inorder(struct node* root) {
if(root != NULL) {
inorder(root->leftChild);
printf("%d ",root->data);
inorder(root->rightChild);
}
}
void postorder(struct node* root) {
if(root != NULL) {
postorder(root->leftChild);
postorder(root->rightChild);
printf("%d ", root->data);
}
}
node* Delete(node* link,int data){
if(link==NULL)
return link;
else if(data<link->data)
link->leftChild=Delete(link->leftChild,data);
else if(data>link->data)
link->data=Delete(link->rightChild,data);
else{
if(link->leftChild==NULL&&link->rightChild==NULL){
delete link;
link=NULL;
}
else if(link->leftChild==NULL){
node* temp=link;
link=link->rightChild;
delete temp;
}
else if(link->rightChild==NULL){
node* temp=link;
link=link->leftChild;
delete temp;
}
else{
node* temp=min(link->rightChild);
link->data=temp->data;
link->data=Delete(link->rightChild,temp->data);
}
}
return link;
}
main(){
insert(5);
insert(2);
insert(1);
insert(-56);
node* temp=search(12);
if(temp!=NULL)
printf("\n%d",temp->data);
else{
puts("not found");
}
puts("max");
max();
puts("min");
min();
}