Live Note

Remain optimistic

6.006 - 5

AVL

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
class Node {
constructor(key) {
this.key = key
this.leftChild = null
this.rightChild = null
}
}

class AVL {
constructor() {
this.root = null
this.parent = null
this.deleted = null
}

getRoot() {
return this.root
}

_heightNode(node) {
return node === null
? -1
: Math.max(
this._heightNode(node.leftChild),
this._heightNode(node.rightChild)
) + 1
}

_rotationLeftLeft(node) {
let tmp = node.leftChild
node.leftChild = tmp.rightChild
tmp.rightChild = node

return tmp
}

_rotationRightRight(node) {
let tmp = node.rightChild
node.rightChild = tmp.leftChild
tmp.leftChild = node

return tmp
}

_rotationLeftRight(node) {
node.leftChild = this._rotationRightRight(node.leftChild)
return this._rotationLeftLeft(node)
}

_rotationRightLeft(node) {
node.rightChild = this._rotationLeftLeft(node.rightChild)
return this._rotationRightRight(node)
}

_insertNode(node, value) {
if (node === null) {
node = new Node(value) // put the node
} else if (value < node.key) {
node.leftChild = this._insertNode(node.leftChild, value)
if (node.leftChild !== null) {
if (
this._heightNode(node.leftChild) - this._heightNode(node.rightChild) >
1
) {
// check the height
if (value < node.leftChild.key) {
node = this._rotationLeftLeft(node)
} else {
node = this._rotationLeftRight(node)
}
}
}
} else if (value > node.key) {
node.rightChild = this._insertNode(node.rightChild, value)
if (node.rightChild !== null) {
if (
this._heightNode(node.rightChild) - this._heightNode(node.leftChild) >
1
) {
// check the height
if (value > node.rightChild.key) {
node = this._rotationRightRight(node)
} else {
node = this._rotationRightLeft(node)
}
}
}
}

return node
}

insert(value) {
this.root = this._insertNode(this.root, value)
}

_removeNode(node, value) {
if (node === null) return node

this.parent = node
if (value < node.key) {
node.leftChild = _removeNode(node.leftChild, value)
} else {
this.deleted = node
node.rightChild = _removeNode(node.rightChild, value)
}

if (node === this.parent) {
if (this.deleted !== null && value === this.deleted.key) {
if (this.deleted === this.parent) {
node = node.leftChild
} else {
;[this.deleted.key, this.parent.key] = [
this.parent.key,
this.deleted.key,
]
node = node.rightChild
}
}
} else {
if (
this._heightNode(node.leftChild) - this._heightNode(node.rightChild) ===
2
) {
if (value < node.leftChild.key) {
node = this._rotationLeftRight(node)
} else {
node = this._rotationLeftLeft(node)
}
}

if (
this._heightNode(node.rightChild) - this._heightNode(node.leftChild) ===
2
) {
if (value > node.rightChild.key) {
node = this._rotationRightLeft(node)
} else {
node = this._rotationRightRight(node)
}
}
}

return node
}

remove(value) {
this.parent = null
this.deleted = null
this.root = this._removeNode(this.root, value)
}

inOrderTraverse() {
this._inOrderTraverse(this.root)
}

_inOrderTraverse(node) {
if (node) {
this._inOrderTraverse(node.leftChild)
console.log(node.key)
this._inOrderTraverse(node.rightChild)
}
}
}

let avl = new AVL()

avl.insert(6)
avl.insert(7)
avl.insert(14)
avl.insert(15)
avl.insert(13)

console.log(avl.getRoot())
avl.inOrderTraverse()

result:

1
2
3
4
5
6
Node {key: 7, leftChild: Node, rightChild: Node}
6
7
13
14
15