-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.cpp
130 lines (113 loc) · 2.83 KB
/
BinarySearchTree.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
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
} *root;
class BST {
public:
int cnt;
int ct;
BST() {
ct = 0;
}
node *create(node *n, int key) {
if (n == NULL) {
n = new node;
n->data = key;
n->left = NULL;
n->right = NULL;
return n;
} else if (key < n->data) {
n->left = create(n->left, key);
} else if (key > n->data) {
n->right = create(n->right, key);
}
return n;
}
void inorder(node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
void preorder(node *root) {
if (root != NULL) {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
void small(node *root) {
if (root != NULL) {
if (root->left != NULL) {
small(root->left);
} else
cout << " " << root->data;
}
}
void large(node *root) {
if (root != NULL) {
if (root->right != NULL) {
large(root->right);
} else
cout << " " << root->data;
}
}
int height(node *root) {
if (root == NULL)
return 0;
else {
int lb = height(root->left);
int rb = height(root->right);
if (lb > rb)
return (lb + 1);
else
return (rb + 1);
}
}
int count(node *root) {
if (root != NULL) {
count(root->left);
ct++;
count(root->right);
}
return ct;
}
};
int main() {
BST b;
int no, i, data;
cout << "\n Enter how many elements do you want to insert: ";
cin >> no;
cout << "\n Enter the data of the tree:\n";
for (i = 0; i < no; i++) {
cin >> data;
root = b.create(root, data);
}
cout << "\n Inorder of given binary search tree is: ";
b.inorder(root);
cout << "\n Preorder of given binary search tree is: ";
b.preorder(root);
cout << "\n Postorder of given binary search tree is: ";
b.postorder(root);
cout << "\n Smallest element in the tree is: ";
b.small(root);
cout << "\n Largest element in the tree is: ";
b.large(root);
cout << "\n The height of the tree is: ";
cout << b.height(root);
cout << "\n The count of the nodes in the tree is: ";
cout << b.count(root);
cout << endl;
return 0;
}