-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_tree.cpp
More file actions
59 lines (50 loc) · 1.08 KB
/
Binary_Search_tree.cpp
File metadata and controls
59 lines (50 loc) · 1.08 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
#include<iostream>
using namespace std;
struct Node{
int value;
struct Node* left;
struct Node* right;
};
struct Node* root = NULL;
void insertNode(int value) {
struct Node* newNode = new Node();
newNode->value = value;
newNode->left = newNode->right = NULL;
if (root == NULL) {
root = newNode;
return;
}
struct Node* current = root;
struct Node* parent = NULL;
while (current != NULL) {
parent = current;
if (value < current->value) {
current = current->left;
} else {
current = current->right;
}
}
if (value < parent->value) {
parent->left = newNode;
} else {
parent->right = newNode;
}
}
void nodeTravers(struct Node* root) {
if (root == NULL)
return;
cout << root->value << " ";
nodeTravers(root->left);
nodeTravers(root->right);
}
int main() {
insertNode(40);
insertNode(20);
insertNode(55);
insertNode(48);
insertNode(10);
insertNode(11);
insertNode(65);
nodeTravers(root);
return 0;
}