-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinarySearchTree
59 lines (52 loc) · 1.21 KB
/
binarySearchTree
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
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class bst {
constructor() {
this.root = null;
}
insertNode(reference_node, newnode) {
if (reference_node < newnode.data) {
if (reference_node.right === null) {
reference_node.right = newnode;
} else {
this.insertNode(reference_node.right, newnode);
}
} else {
if (reference_node.left === null) {
reference_node.left = newnode;
} else {
this.insertNode(reference_node.left, newnode);
}
}
}
insert(data) {
var newnode = new Node(data);
if (this.root === null) {
this.root = newnode;
} else {
this.insertNode(this.root, newnode);
}
}
inorder(node) {
if (node !== null) {
this.inorder(node.left);
console.log(node.data);
this.inorder(node.right);
}
}
}
var binary_s_tree_obj = new bst();
binary_s_tree_obj.insert(89);
binary_s_tree_obj.insert(50);
binary_s_tree_obj.insert(19);
binary_s_tree_obj.insert(30);
binary_s_tree_obj.insert(39);
binary_s_tree_obj.insert(40);
binary_s_tree_obj.insert(69);
binary_s_tree_obj.insert(80);
binary_s_tree_obj.inorder(binary_s_tree_obj.root);