二叉搜索树

二叉搜索数:父节点值大于左儿子的值小于右儿子的值

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
function Node(val) {
this.data = val;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
}
BinarySearchTree.prototype = {
insert: function(val, node) {
if (this.root == null) {
this.root = new Node(val);
return ;
}
if (node.data > val) {
if (node.left == null) {
node.left = new Node(val);
} else {
this.insert(val, node.left);
}
} else if (node.data < val) {
if (node.right == null) {
node.right = new Node(val);
} else {
this.insert(val, node.right);
}
} else {
console.log('节点已经存在');
}
},
insert2: function(val) {
if (this.root == null) {
this.root = new Node(val);
return ;
}
var tmpNode = this.root;
while (true) {
if (tmpNode.data > val) {
if (tmpNode.left) {
tmpNode = tmpNode.left;
} else {
tmpNode.left = new Node(val);
return ;
}
} else if (tmpNode.data < val) {
if (tmpNode.right) {
tmpNode = tmpNode.right;
} else {
tmpNode.right = new Node(val);
return ;
}
} else {
console.log('节点已经存在');
return;
}
}
},
preOrderTraverseNode: function(node) {
if (node) {
console.log(node.data);
this.preOrderTraverseNode(node.left);
this.preOrderTraverseNode(node.right);
}
},
inOrderTraverseNode: function(node) {
if (node) {
this.inOrderTraverseNode(node.left);
console.log(node.data);
this.inOrderTraverseNode(node.right);
}
},
postOrderTraverseNode: function(node) {
if (node) {
this.postOrderTraverseNode(node.left);
this.postOrderTraverseNode(node.right);
console.log(node.data);
}
},
breadthTraverseNode: function() {
var stack = [this.root];
while (stack.length > 0) {
var tmpNode = stack.shift();
console.log(tmpNode.data);
if (tmpNode.left)
stack.push(tmpNode.left);
if (tmpNode.right)
stack.push(tmpNode.right);
}
},
minNode: function() {
if (!this.root)
return null;
var tmpNode = this.root;
while (tmpNode && tmpNode.left) {
tmpNode = tmpNode.left;
}
return tmpNode.data;
},
maxNode: function() {
if (!this.root)
return null;
var tmpNode = this.root;
while (tmpNode && tmpNode.right) {
tmpNode = tmpNode.right;
}
return tmpNode.data;
},
searchNodeWithData: function(val, node) {
if (!node)
return null;
if (node.data > val) {
return this.searchNodeWithData(val, node.left);
} else if (node.data < val) {
return this.searchNodeWithData(val, node.right);
} else {
return node;
}
return null;
},
// 删除的节点有左右孩子,则取右孩子的最小值,左孩子的最大值
delNodeWithData: function(val, node) {
if (!node)
return null;
if (node.data > val) {
node.left = this.delNodeWithData(val, node.left);
} else if (node.data < val) {
node.right = this.delNodeWithData(val, node.right);
} else {
if (node.left && node.right) {
var tmpNode = this._findMinNode(node.right);
node.data = tmpNode.data;
node.right = this.delNodeWithData(node.data, node.right);
} else {
if (!node.left) {
node = node.right;
} else if (!node.right) {
node = node.left;
} else {
node = null;
}
}
}
return node;
},
_findMinNode: function(node) {
if (!node)
return null;
while (node && node.right) {
node = node.right;
}
return node;
}
}
var bst = new BinarySearchTree();
// bst.insert(10, bst.root);
// bst.insert(20, bst.root);
// bst.insert(5, bst.root);
bst.insert2(10);
bst.insert2(20);
bst.insert2(5);
bst.insert2(15);
bst.insert2(29);
bst.insert2(25);