-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree-node.js
195 lines (173 loc) · 3.96 KB
/
binary-tree-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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const LEFT = Symbol('left');
const RIGHT = Symbol('right');
// tag::snippet[]
/**
* Binary Tree Node
*
*/
class BinaryTreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
this.meta = {};
// end::snippet[]
this.parent = null;
this.parentSide = null;
}
// tag::setAndUpdateParent[]
/**
* Set a left node descendants.
* Also, children get associated to parent.
*/
setLeftAndUpdateParent(node) {
this.left = node;
if (node) {
node.parent = this;
node.parentSide = LEFT;
}
}
/**
* Set a right node descendants.
* Also, children get associated to parent.
*/
setRightAndUpdateParent(node) {
this.right = node;
if (node) {
node.parent = this;
node.parentSide = RIGHT;
}
}
// end::setAndUpdateParent[]
/**
* Tell if is parent's left or right child
*
* @returns {string} side (left or right) this node is of its parent
*/
get parentChildSide() {
if (this.parent) {
return this.isParentLeftChild ? 'left' : 'right';
}
return 'root';
}
/**
* Return true if this node is its parent left child
*/
get isParentLeftChild() {
return this.parentSide === LEFT;
}
/**
* Return true if this node is its parent right child
*/
get isParentRightChild() {
return this.parentSide === RIGHT;
}
/**
* Node is leaf is it has no descendants
*/
get isLeaf() {
return !this.left && !this.right;
}
/**
* Get sibling of current node
*/
get sibling() {
const { parent } = this;
if (!parent) return null;
return parent.right === this ? parent.left : parent.right;
}
/**
* Get parent sibling = uncle (duh)
*/
get uncle() {
const { parent } = this;
if (!parent) return null;
return parent.sibling;
}
get grandparent() {
const { parent } = this;
return parent && parent.parent;
}
/**
* Get color
*/
get color() {
return this.meta.color;
}
/**
* Set Color
*/
set color(value) {
this.meta.color = value;
}
// tag::avl[]
/**
* @returns {Number} left subtree height or 0 if no left child
*/
get leftSubtreeHeight() {
return this.left ? this.left.height + 1 : 0;
}
/**
* @returns {Number} right subtree height or 0 if no right child
*/
get rightSubtreeHeight() {
return this.right ? this.right.height + 1 : 0;
}
/**
* Get the max height of the subtrees.
*
* It recursively goes into each children calculating the height
*
* Height: distance from the deepest leaf to this node
*/
get height() {
return Math.max(this.leftSubtreeHeight, this.rightSubtreeHeight);
}
/**
* Returns the difference the heights on the left and right subtrees
*/
get balanceFactor() {
return this.leftSubtreeHeight - this.rightSubtreeHeight;
}
// end::avl[]
/**
* Serialize node's values
*/
toValues() {
return {
value: this.value,
left: this.left && this.left.value,
right: this.right && this.right.value,
parent: this.parent && this.parent.value,
parentSide: this.parentSide,
};
}
/**
* Get and Set data value
* @param {any} value (optional) if not provided is a getter, otherwise a setter.
*/
data(value) {
if (value === undefined) {
return this.meta.data;
}
this.meta.data = value;
return this;
}
/**
* Convert Binary tree from an iterable (e.g. array)
* @param {array|string} iterable - The iterable
*/
static from(iterable = []) {
const toBinaryTree = (array, index = 0) => {
if (index >= array.length) return null;
const node = new BinaryTreeNode(array[index]);
node.setLeftAndUpdateParent(toBinaryTree(array, index * 2 + 1));
node.setRightAndUpdateParent(toBinaryTree(array, index * 2 + 2));
return node;
};
return toBinaryTree(Array.from(iterable));
}
}
BinaryTreeNode.RIGHT = RIGHT;
BinaryTreeNode.LEFT = LEFT;
module.exports = BinaryTreeNode;