-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.ts
More file actions
164 lines (147 loc) · 4.03 KB
/
binaryTree.ts
File metadata and controls
164 lines (147 loc) · 4.03 KB
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
(function () {
enum removedSideEnum {
root = "root",
left = "left",
right = "right",
}
class TreeNode {
public value: any;
public left: TreeNode | null;
public right: TreeNode | null;
constructor(val) {
this.value = val;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
private root: TreeNode | null;
constructor() {
this.root = null;
}
public getRootNode(): TreeNode | null {
return this.root;
}
public insert<T>(value: T): TreeNode | null {
const newTreNode = new TreeNode(value);
if (!this.root) {
this.root = newTreNode;
return newTreNode;
}
let currentNode = this.root;
while (currentNode) {
if (value === currentNode.value) return null;
if (value > currentNode.value) {
if (!currentNode.right) {
currentNode.right = newTreNode;
return newTreNode;
}
currentNode = currentNode.right;
} else {
if (!currentNode.left) {
currentNode.left = newTreNode;
return newTreNode;
}
currentNode = currentNode.left;
}
}
return null;
}
public find<T>(value: T): TreeNode | null {
let currentNode = this.root;
while (currentNode) {
if (value === currentNode.value) return currentNode;
if (value > currentNode.value) {
currentNode = currentNode.right;
} else {
currentNode = currentNode.left;
}
}
return null;
}
public remove<T>(value: T): void {
if (!this.root) return;
let preNodeSide = removedSideEnum.root;
let preNode = this.root;
let currentNode: TreeNode | null = this.root;
while (currentNode) {
if (value === currentNode.value) {
switch (preNodeSide) {
case removedSideEnum.root: {
this.root = null;
break;
}
case removedSideEnum.left: {
preNode.left = this.removeHelper(currentNode);
break;
}
case removedSideEnum.right: {
preNode.right = this.removeHelper(currentNode);
break;
}
default:
break;
}
}
if (value > currentNode.value) {
preNode = currentNode;
preNodeSide = removedSideEnum.right;
currentNode = currentNode.right;
} else {
preNode = currentNode;
preNodeSide = removedSideEnum.left;
currentNode = currentNode.left;
}
}
}
public loopTree(): any[] {
let result = [];
function _recursive(node: TreeNode | null) {
if (node) {
const nodeVal: any = node.value;
result = result.concat(nodeVal);
}
if (node?.right) {
_recursive(node?.right);
}
if (node?.left) {
_recursive(node?.left);
}
}
const rootNode = this.getRootNode();
_recursive(rootNode);
return result;
}
private removeHelper(node: TreeNode): TreeNode | null {
const { left, right } = node;
const onlyRight = !left && right;
const onlyLeft = left && !right;
const hasBoth = left && right;
if (onlyRight) return right;
if (onlyLeft) return left;
if (hasBoth) {
const leftestNodeForRightSide = this.findLeftestNode(right);
leftestNodeForRightSide.left = left;
return right;
}
return null;
}
private findLeftestNode(node: TreeNode): TreeNode {
if (node.left) {
return this.findLeftestNode(node.left);
}
return node;
}
}
// const BST = new BinarySearchTree();
// const initValues = [8, 3, 1, 6, 4, 7, 10, 14, 13];
// initValues.forEach((num) => {
// BST.insert(num);
// });
// const removedNums = [3];
// removedNums.forEach((num) => {
// BST.remove(num);
// });
// const test = BST.find(6);
// console.log("test", test);
})();