-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.js
171 lines (162 loc) · 3.67 KB
/
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
/**
* 搜索二叉树
*/
class SearchTree {
constructor() {
this.root = null;
}
insert(num) {
let node = new Node(num);
if (this.root === null) {
this.root = node;
return;
}
let parent = this.getPrev(num);
if (num < parent.value) {
parent.left = node;
} else {
parent.right = node;
}
}
remove(num) {
let point = this.root;
let parent = null;
let tree = this;
let res = null;
while (true) {
if (point.left) {
if (num < point.left.value || num < point.value) {
parent = point;
point = point.left;
continue;
}
}
if (point.right) {
if (num >= point.right.value || num >= point.value) {
if (num === point.value) {
delMethod(point, parent);
if (point === null) {
point = this.root;
} else {
parent = parent;
point = parent.right;
}
res = true;
continue;
}
parent = point;
point = point.right;
continue;
}
}
if (point.value === null) {
res = true;
delMethod(point, parent);
}
break;
}
return res;
function delMethod(delNode, parent) {
let p = delNode;
let pp = parent;
// 删除的节点有左右两个子节点
if (p.left != null && p.right != null) {
// 查找右子树中最小节点
let minP = p.right;
let minPP = p;
// 左边的值小于右边的值
while (minP.left != null) {
minPP = minP;
minP = minP.left;
}
// 用找到最小节点值赋值给要删除的节点的value
p.value = minP.value;
p = minP;
pp = minPP;
}
let child;
if (p.left !== null) {
child = p.left;
} else if (p.right !== null) {
child = p.right;
} else {
child = null;
}
if ((pp = null)) {
tree.root = child;
} else if (pp.left === p) {
pp.left = child;
} else {
pp.right = child;
}
}
}
//中序遍历
print() {
let point = this.root;
if (point) {
printAll(point.left);
console.log(point.value);
printAll(point.right);
}
function printAll(point) {
if (point == null) {
return;
}
printAll(point.left);
console.log(point.value);
printAll(point.right);
}
}
//添加和查找的公用部分
getPrev(num, find = false) {
let point = this.root;
let res = [];
while (true) {
if (point.left) {
if (num < point.left.value || num < point.value) {
point = point.left;
continue;
}
}
if (point.right) {
if (num >= point.right.value || num >= point.value) {
//搜索时如果有多个值则缓存
if (find && num === point.value) {
res.push(point.value);
}
point = point.right;
continue;
}
}
//如果是搜索
if (find) {
if (point.value === num) {
res.push(point.value);
}
if (res.length === 0) {
return null;
}
if (res.length === 1) {
return res[0];
}
return res;
}
//如果是添加 返回的是应该添加的那各节点的父节点
return point;
}
}
}
let searchTree = new SearchTree();
searchTree.insert(4);
searchTree.insert(1);
searchTree.insert(2);
searchTree.insert(5);
searchTree.print();