-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Search Tree.cpp
49 lines (45 loc) · 986 Bytes
/
Binary Search Tree.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
#include <iostream>
using namespace std;
struct node {
int info;
node *left, *right;
};
class tree {
public:
tree() {
root = nullptr;
}
node *insert_aux (node *t, int data) {
if (t == nullptr) {
node *p = new node;
p -> info = data;
p -> left = nullptr;
p -> right = nullptr;
return p;
}
if (t -> info > data) t -> left = insert_aux(t -> left, data);
else t -> right = insert_aux(t -> right, data);
return t;
}
void insert(int data) {
root = insert_aux (root, data);
}
void print_aux (node *t) {
if (t != nullptr) {
cout << t -> info << endl;
print_aux(t -> left);
print_aux(t -> right);
}
}
void print () {
print_aux (root);
}
private:
node *root;
};
int main () {
tree t;
for (int i = 1; i < 8; ++i) t.insert(i);
t.print();
return 0;
}