-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearchTree.ts
132 lines (115 loc) · 3.39 KB
/
binarySearchTree.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
class BinarySearchTreeNode {
value: number;
left: BinarySearchTreeNode | null = null;
right: BinarySearchTreeNode | null = null;
constructor(value: number) {
this.value = value;
}
}
const insertNode = (root: BinarySearchTreeNode, value: number): void => {
let inserted = false;
let node = root;
const newNode = new BinarySearchTreeNode(value);
while (!inserted) {
if (value > node.value) {
const rightNode = node.right;
if (!rightNode) {
node.right = newNode;
inserted = true;
} else {
node = rightNode;
}
} else {
const leftNode = node.left;
if (!leftNode) {
node.left = newNode;
inserted = true;
} else {
node = leftNode;
}
}
}
};
const findMinValue = (root: BinarySearchTreeNode | null): BinarySearchTreeNode | null => {
if (root === null) {
return null;
}
const ret = findMinValue(root.left);
return ret === null ? root : ret;
};
const deleteNode = (root: BinarySearchTreeNode | null, key: number): BinarySearchTreeNode | null => {
if (root === null) {
return null;
}
if (key < root.value) {
root.left = deleteNode(root.left, key);
} else if (key > root.value) {
root.right = deleteNode(root.right, key);
} else {
if (root.left === null && root.right === null) {
root = null;
} else if (root.left === null) {
root = root.right;
} else if (root.right === null) {
root = root.left;
} else {
const nextMinRoot = findMinValue(root.right);
const nextMinValue = nextMinRoot?.value ?? null;
if (nextMinValue !== null) {
root.value = nextMinValue;
root.right = deleteNode(root.right, nextMinValue);
}
}
}
return root;
};
const bstFromPreorder = (preOrder: number[]): BinarySearchTreeNode | null => {
let i = 1;
const root = new BinarySearchTreeNode(preOrder[0]);
while (i < preOrder.length) {
insertNode(root, preOrder[i]);
i++;
}
return root;
};
const invertTree = (root: BinarySearchTreeNode | null): BinarySearchTreeNode | null => {
if (root === null) {
return null;
}
const left = invertTree(root.left);
const right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
};
const isValid = (
root: BinarySearchTreeNode | null,
): {
max: number | null;
min: number | null;
isValidSubtrees: boolean;
} => {
if (root === null) {
return {
isValidSubtrees: true,
min: null,
max: null,
};
}
const { isValidSubtrees: isLeftValid, max: leftMax, min: leftMin } = isValid(root.left);
const { isValidSubtrees: isRightValid, max: rightMax, min: rightMin } = isValid(root.right);
const tempLeftMax = leftMax === null ? Number.MIN_SAFE_INTEGER : leftMax;
const tempRightMax = rightMax === null ? Number.MIN_SAFE_INTEGER : rightMax;
const treeMaxAtRoot = Math.max(tempLeftMax, tempRightMax, root.value);
const tempLeftMin = leftMin === null ? Number.MAX_SAFE_INTEGER : leftMin;
const tempRightMin = rightMin === null ? Number.MAX_SAFE_INTEGER : rightMin;
const treeMinAtRoot = Math.min(tempLeftMin, tempRightMin, root.value);
return {
isValidSubtrees: isLeftValid && isRightValid && root.value > tempLeftMax && root.value < tempRightMin,
max: treeMaxAtRoot,
min: treeMinAtRoot,
};
};
function isValidBST(root: BinarySearchTreeNode | null): boolean {
return isValid(root).isValidSubtrees;
}