-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
39 lines (33 loc) · 823 Bytes
/
node.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
export default class Node {
constructor(value = null) {
this.LEFT = null;
this.RIGHT = null;
this.PARENT = null;
this.depth = 0;
this.value = value;
Node.number = ++Node.number || 1;
}
getSide() {
const parent = this.PARENT;
if (parent == null) return "ROOT";
if (parent.LEFT != null && parent.LEFT.value == this.value) return "LEFT";
if (parent.RIGHT != null && parent.RIGHT.value == this.value)
return "RIGHT";
}
addLeft(newNode) {
newNode.PARENT = this;
this.LEFT = newNode;
newNode.depth = this.depth + 1;
}
addRight(newNode) {
newNode.PARENT = this;
this.RIGHT = newNode;
newNode.depth = this.depth + 1;
}
isLeaf() {
return this.LEFT == null && this.RIGHT == null;
}
isRoot() {
return this.PARENT == null;
}
}