-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
112 lines (97 loc) · 1.93 KB
/
Copy pathbst.cpp
File metadata and controls
112 lines (97 loc) · 1.93 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
#include<iostream>
using namespace std;
struct Node{
int data;
Node*left;
Node*right;
Node(int value){
data=value;
left=nullptr;
right=nullptr;
}
};
Node*insert(Node*root,int value){
if(root==nullptr){
return new Node(value);
}
if(value < root->data){
root->left=insert(root->left,value);
}
else{
root->right=insert(root->right,value);
}
return root;
}
int findh(Node*root){
if(root==nullptr)
return 0;
else if(root->left==nullptr && root->right==nullptr)
return 0;
return (1 + max(findh(root->left),findh(root->right)));
}
int minimum(Node*root){
while(root->left != nullptr){
root=root->left;
}
return root->data;
}
Node*mirror(Node*root){
if(root==nullptr){
return nullptr;
}
Node*temp=root->left;
root->left=mirror(root->right);
root->right=mirror(temp);
return root;
}
bool search(Node*root,int key){
if(root==nullptr){
return false;
}
if(key== root->data){
return true;
}
else if(key <root->data){
return search(root->left,key);
}
else{
return search(root->right,key);
}
}
void inorder(Node*root){
if(root==nullptr){
return;
}
inorder(root->left);
cout<<root->data;
inorder(root->right);
}
int main() {
Node* root = nullptr;
int n, value, key;
cout << "enter the number of nodes to insert: ";
cin >> n;
cout << "enter the values of the nodes: ";
for (int i = 0; i < n; ++i) {
cin >> value;
root = insert(root, value);
}
cout << "inorder traversal of the BST: ";
inorder(root);
cout << endl;
cout << "enter the key to search: ";
cin >> key;
if (search(root, key)) {
cout << key << " found in the BST." << endl;
} else {
cout << key << " not found in the BST." << endl;
}
cout << "minimum value in BST: " << minimum(root) << endl;
cout << "height of the BST: " << findh(root) << endl;
cout << "mirroring the BST..." << endl;
root = mirror(root);
cout << "inorder traversal of the mirrored BST: ";
inorder(root);
cout << endl;
return 0;
}