-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathbinary-search-tree.ts
191 lines (162 loc) · 4.23 KB
/
binary-search-tree.ts
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
/**
* Binary Search Tree (BST) implementation
*/
export interface Node {
parent?: Node;
key: number;
left?: Node;
right?: Node;
}
export interface Tree {
root: Node;
}
/**
* Create a tree node. Used to build a tree in tests.
*/
export function createNode(key: number, left: Node = null, right: Node = null): Node {
const node = {
key,
left,
right,
parent: null,
};
if (left) left.parent = node;
if (right) right.parent = node;
return node;
}
/**
* Print a BST in order.
* Time complexity: O(n)
*/
export function inOrderWalk(node: Node) {
if (!node) return;
inOrderWalk(node.left);
console.log(node);
inOrderWalk(node.right);
}
/**
* Search a key in the BST
* Time complexity: O(lg(n))
* @param node BST root node
* @param key Key to look for
*/
export function search(node: Node, key: number): Node {
if (!node) return null;
if (node.key === key) return node;
else if (node.key < key) return search(node.right, key);
else return search(node.left, key);
}
/**
* Return the minimum key of the BST
* Time complexity: O(lg(n))
* @param node BST root node
*/
export function minimum(node: Node): Node {
let leftMost: Node = node;
while (leftMost !== null && leftMost.left !== null) {
leftMost = leftMost.left;
}
return leftMost;
}
/**
* Return the maximum key of the BST
* Time complexity: O(lg(n))
* @param node BST root node
*/
export function maximum(node: Node): Node {
let rightMost: Node = node;
while (rightMost !== null && rightMost.right !== null) {
rightMost = rightMost.right;
}
return rightMost;
}
/**
* Return the successor node in a in-order walk
* Time complexity: O(lg(n))
* @param node BST root node
*/
export function successor(node: Node): Node {
if (node.right !== null) return minimum(node.right);
let parent = node.parent;
let current = node;
while (parent !== null && parent.right === current) {
current = parent;
parent = parent.parent;
}
return parent;
}
/**
* Return the predessor node in a in-order walk
* Time complexity: O(lg(n))
* @param node BST root node
*/
export function predecessor(node: Node): Node {
if (node.left !== null) return node.left;
let parent = node.parent;
let current = node;
while (parent !== null && parent.left === current) {
current = parent;
parent = parent.parent;
}
return parent;
}
/**
* Insert a leaf node in a BST
* Time complexity: O(lg(n))
* @param tree BST tree
* @param leaf New leaf node to add
*/
export function insert(tree: Tree, leaf: Node) {
let parent: Node = null;
let current = tree.root;
while (current !== null) {
parent = current;
if (leaf.key >= current.key) {
current = current.right;
} else {
current = current.left;
}
}
if (parent === null) tree.root = leaf;
else if (leaf.key >= parent.key) parent.right = leaf;
else parent.left = leaf;
leaf.parent = parent;
}
/**
* Replace a node with a new one in a BST
* @NOTE: it does not update the childs nor does it check if the BST is still
* valid.
* Time complexity: O(1)
* @param tree BST root node
* @param oldNode Node to be replaced. It cannot be null.
* @param newNode Replacement node
*/
export function transplant(tree: Tree, oldNode: Node, newNode: Node) {
if (oldNode.parent === null) tree.root = newNode;
else if (oldNode.parent.left === oldNode) oldNode.parent.left = newNode;
else oldNode.parent.right = newNode;
if (newNode !== null) newNode.parent = oldNode.parent;
}
/**
* Remove a node from a BST.
* Time complexity: O(lg(n))
* @param tree BST root node
* @param removed Node to be removed
*/
export function remove(tree: Tree, removed: Node) {
if (removed.left === null) transplant(tree, removed, removed.right);
else if (removed.right === null) transplant(tree, removed, removed.left);
else {
const minRight = minimum(removed.right);
if (minRight.parent !== removed) {
transplant(tree, minRight, minRight.right);
// Attach the removed node right subtree to minRight
minRight.right = removed.right;
minRight.right.parent = minRight;
}
transplant(tree, removed, minRight);
// Attach the removed node left subtree to minRight
minRight.left = removed.left;
minRight.left.parent = minRight;
}
}