-
Notifications
You must be signed in to change notification settings - Fork 0
/
22-BinaryTree.js
145 lines (126 loc) · 3.73 KB
/
22-BinaryTree.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BST {
constructor() {
this.root = null;
}
// ============ INSERT ============
insert(value) {
// 1. Create a new node
const newNode = new Node(value);
// 2. Start at the root
// Check if there is root, if not root is now new node
if (this.root === null) {
this.root = newNode;
return this;
} else {
// If there is root, check if value of new node is greater or lesser than value of root
let current = this.root;
while (true) {
if (value === current.value) return undefined;
const isLesser = value < current.value;
const isGreater = value > current.value;
// 4. If lesser
if (isLesser) {
// check if there is a left node
if (current.left === null) {
// if no left node, add new node to left
current.left = newNode;
return this;
} else {
// if there is left node, move to that node and repeat
current = current.left;
}
// If greater,
} else if (isGreater) {
// check if there is a right node
if (current.right === null) {
// if no right right, add new node to right
current.right = newNode;
return this;
} else {
// if there is right node, move to that node and repeat
current = current.right;
}
}
}
//
}
}
// ========== FIND ==========
find(searchValue) {
// Check if there is root
// If no root, done searching
if (this.root === null) return console.log(false);
// If there is root, check if value is what we are looking for
// If so, done searching
if (this.root.value === searchValue)
return console.log({ result: true, node: this.root });
// Otherwise,
let current = this.root;
while (true) {
if (current.value === searchValue)
return console.log({ result: true, node: current });
// check if value is greater or lesser than value
const isLesser = searchValue < current.value;
const isGreater = searchValue > current.value;
// If lesser,
if (isLesser) {
// check to see if there is a left node
// If no, done searching
if (current.left === null) return console.log(false);
// If yes, move down to left node and check value
current = current.left;
}
// If greater,
if (isGreater) {
// check to see if there is a right node
// If no, done searching
if (current.right === null) return console.log(false);
// If yes, move down to right node and check value
current = current.right;
}
}
}
}
// 10
// 5 13
// 2 7 11 16
const tree = new BST();
tree.insert(10);
tree.insert(5);
tree.insert(13);
tree.insert(2);
tree.insert(7);
tree.insert(11);
tree.insert(16);
tree.find(13);
console.log(serialise(tree.root));
// console.log(deserialize("1,2,X,X,3,4,X,X,5,X,X"));
// =========== SERIALISE =================
function serialise(rootNode) {
if (rootNode === null) return "X";
const stringLeftSerialised = serialise(rootNode.left);
const stringRightSerialised = serialise(rootNode.right);
return `${rootNode.value},${stringLeftSerialised},${stringRightSerialised}`;
}
function deserialize(string) {
const arr = string.split(",")
return innerDeserialise()
function innerDeserialise() {
if(!arr.length) return
let val = arr.shift()
if(val === "X") return null
let node = {
value: val
}
node.left = innerDeserialise()
node.right = innerDeserialise()
return node
}
}