1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ struct Node {
5
+ int data;
6
+ Node *left;
7
+ Node *right;
8
+ };
9
+
10
+ struct Node * create (int item)
11
+ {
12
+ struct Node * node = new Node;
13
+ node->data = item;
14
+ node->left = node->right = NULL ;
15
+ return node;
16
+ }
17
+
18
+ void search (struct Node * &cur, int item, struct Node * &parent)
19
+ {
20
+ while (cur != NULL && cur->data != item)
21
+ {
22
+ parent = cur;
23
+ if (item < cur->data )
24
+ cur = cur->left ;
25
+ else
26
+ cur = cur->right ;
27
+ }
28
+ }
29
+
30
+ struct Node * findMinimum (struct Node * cur)
31
+ {
32
+ while (cur->left != NULL ) {
33
+ cur = cur->left ;
34
+ }
35
+ return cur;
36
+ }
37
+
38
+ struct Node * findMaximum (struct Node * cur)
39
+ {
40
+ while (cur->right != NULL ) {
41
+ cur = cur->right ;
42
+ }
43
+ return cur;
44
+ }
45
+
46
+ struct Node * insertion (struct Node * root, int item)
47
+ {
48
+ if (root == NULL )
49
+ return create (item); // return new node if tree is empty
50
+ if (item < root->data )
51
+ root->left = insertion (root->left , item);
52
+ else
53
+ root->right = insertion (root->right , item);
54
+ return root;
55
+ }
56
+ void deletion (struct Node *& root, int item) // function to delete a struct Node/
57
+ {
58
+ struct Node * parent = NULL ;
59
+ struct Node * cur = root;
60
+ search (cur, item, parent); // find the node to be deleted/
61
+ if (cur == NULL )
62
+ return ;
63
+ if (cur->left == NULL && cur->right == NULL ) // When node has no children/
64
+ {
65
+ if (cur != root)
66
+ {
67
+ if (parent->left == cur)
68
+ parent->left = NULL ;
69
+ else
70
+ parent->right = NULL ;
71
+ }
72
+ else
73
+ root = NULL ;
74
+ free (cur);
75
+ }
76
+ else if (cur->left && cur->right )
77
+ {
78
+ struct Node * succ = findMinimum (cur->right );
79
+ int val = succ->data ;
80
+ deletion (root, succ->data );
81
+ cur->data = val;
82
+ }
83
+ else
84
+ {
85
+ struct Node * child = (cur->left )? cur->left : cur->right ;
86
+ if (cur != root)
87
+ {
88
+ if (cur == parent->left )
89
+ parent->left = child;
90
+ else
91
+ parent->right = child;
92
+ }
93
+ else
94
+ root = child;
95
+ free (cur);
96
+ }
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+
104
+ int count = 0 ;
105
+ struct Node * kthSmallest (struct Node * root, int k)
106
+ {
107
+ if (root == NULL )
108
+ return NULL ;
109
+
110
+ struct Node * left = kthSmallest (root->left , k);
111
+
112
+ if (left != NULL )
113
+ return left;
114
+
115
+ count++;
116
+ if (count == k)
117
+ return root;
118
+
119
+ return kthSmallest (root->right , k);
120
+ }
121
+
122
+ void printKthSmallest (struct Node * root, int k)
123
+ {
124
+ struct Node * res = kthSmallest (root, k);
125
+ if (res == NULL )
126
+ cout << " Not found" ;
127
+ else
128
+ cout << res->data ;
129
+ }
130
+
131
+
132
+ int main (){
133
+ struct Node * root = NULL ;
134
+ int choice,data;
135
+ do {
136
+ cin >> choice;
137
+ switch (choice){
138
+ case 1 :
139
+ cin >> data;
140
+ root = insertion (root,data);
141
+ break ;
142
+ case 2 :
143
+ cin >> data;
144
+ deletion (root,data);
145
+ break ;
146
+ case 3 :
147
+ cout << (findMinimum (root))->data << endl << (findMaximum (root))->data << endl;
148
+ break ;
149
+ case 4 :
150
+ cin >> data;
151
+ printKthSmallest (root,data);
152
+ cout << endl;
153
+ break ;
154
+ }
155
+ }while (choice != 0 );
156
+ return 0 ;
157
+ }
0 commit comments