-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-Binary-Tree.js
153 lines (142 loc) · 3.36 KB
/
09-Binary-Tree.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
class Node {
constructor(value, parent, left, right) {
this.value = value;
this.parent = parent;
this.left = left;
this.right = right;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insert(value) {
const node = new Node(value, null, null, null);
if (!this.root) {
this.root = node;
return;
}
let parent = this.root;
let found = false;
while(!found) {
if (value < parent.value) {
if (!parent.left) {
parent.left = node;
node.parent = parent;
found = true;
} else {
parent = parent.left;
}
} else if (value > parent.value) {
if (!parent.right) {
parent.right = node;
node.parent = parent;
found = true;
} else {
parent = parent.right;
}
}
}
}
remove(value) {
let node = this.search(value);
// 为根结点
if (!node.parent) {
this.root = null;
return;
}
// 没有子节点
if (!node.left && !node.right) {
if (node.value < node.parent.value) {
node.parent.left = null;
} else {
node.parent.right = null;
}
} else if (node.left && node.right) {
// 有两个节点
if (node.value < node.parent.value) {
node.parent.left = node.right;
node.right.parent = node.parent
const leftNode = this.searchLeftNode(node.right);
leftNode.left = node.left;
node.left.parent = leftNode;
} else {
node.parent.right = node.left;
node.left.parent = node.parent
const rightNode = this.searchRightNode(node.left);
rightNode.right = node.right;
node.right.parent = rightNode;
}
} else if (node.left && !node.right) {
// 有一个左节点
if (node.value < node.parent.value) {
node.parent.left = node.left;
node.left.parent = node.parent;
} else {
node.parent.right = node.left;
node.left.parent = node.parent;
}
} else if (!node.left && node.right) {
// 有一个右节点
if (node.value < node.parent.value) {
node.parent.left = node.right;
node.right.parent = node.parent;
} else {
node.parent.right = node.right;
node.right.parent = node.parent;
}
}
}
search(value) {
let parent = this.root;
if (!parent) {
return null;
}
let node = null;
while(!node) {
if (parent.value === value) {
node = parent;
} else if (parent.value < value) {
parent = parent.left;
} else {
parent = parent.right;
}
}
return node;
}
searchLeftNode(node) {
if (!node.left) {
return node;
} else {
return this.searchLeft(node.left);
}
}
searchRightNode(node) {
if (!node.right) {
return node;
} else {
return this.searchRightNode(node.right);
}
}
height(node = this.root){
if(!node) return 0;
let leftHeight = this.height(node.left);
let rightHeight = this.height(node.right);
return Math.max(leftHeight, rightHeight) + 1;
}
}
function test () {
const BST = new BinaryTree();
BST.insert(7);
BST.insert(13);
BST.insert(3);
BST.insert(1);
BST.insert(5);
BST.insert(6);
BST.insert(15);
BST.insert(14);
BST.insert(12);
BST.insert(11);
console.log(BST);
}
test();