-
Notifications
You must be signed in to change notification settings - Fork 0
/
11- Binary Search Tree.cpp
57 lines (48 loc) · 1.21 KB
/
11- 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
50
51
52
53
54
55
56
57
//in the name of God
#include <iostream>
using namespace std;
struct Node {
int key;
Node *right, *left;
};
Node* search(Node *root, int key) {
if (!root || root->key == key)
return root;
if (root->key < key)
return search(root->right, key);
return search(root->left, key);
}
Node* createNode(int item) {
Node *nd = (Node *)malloc(sizeof(Node));
nd->key = item;
nd->left = nd->right = NULL;
return nd;
}
void inorder(Node *root) {
if (root) {
inorder(root->left);
printf("%d \n", root->key);
inorder(root->right);
}
}
Node* insert(Node *nd, int key) {
if (!nd)
return createNode(key);
if (key < nd->key)
nd->left = insert(nd->left, key);
else if (key > nd->key)
nd->right = insert(nd->right, key);
return nd;
}
int main() {
Node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}