-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy path排序二叉树.js
181 lines (162 loc) · 3.53 KB
/
排序二叉树.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
172
173
174
175
176
177
178
179
180
181
/*
*
*
*
*/
function BinaryTree() {
var Node = function(key) {
this.key = key;
this.left = null;
this.right = null;
}
var root = null;
var insertNode = function(node, newNode) {
if (node.key > newNode.key) {
if (node.left) {
insertNode(node.left, newNode);
} else {
node.left = newNode;
}
} else {
if (node.right) {
insertNode(node.right, newNode);
} else {
node.right = newNode;
}
}
}
this.insert = function(key) {
var newNode = new Node(key);
if (root) {
insertNode(root, newNode);
} else {
root = newNode;
}
}
var inOrderTraverseNode = function(node, callback) {
if (node) {
inOrderTraverseNode(node.left, callback);
callback & callback(node.key);
inOrderTraverseNode(node.right, callback);
}
}
this.inOrderTraverse = function(callback) {
inOrderTraverseNode(root, callback);
}
var preOrderTraverseNode = function(node, callback) {
if (node) {
callback & callback(node.key);
preOrderTraverseNode(node.left, callback);
preOrderTraverseNode(node.right, callback);
}
}
this.preOrderTraverse = function(callback) {
preOrderTraverseNode(root, callback);
}
var postOrderTraverseNode = function(node, callback) {
if (node) {
postOrderTraverseNode(node.left, callback);
postOrderTraverseNode(node.right, callback);
callback & callback(node.key);
}
}
this.postOrderTraverse = function(callback) {
postOrderTraverseNode(root, callback);
}
var minNode = function(node) {
if (node) {
while(node && node.left) {
node = node.left;
}
return node.key
}
return null;
}
this.min = function() {
return maxNode(root);
}
var maxNode = function(node) {
if (node) {
while(node && node.right) {
node = node.right;
}
return node.key;
}
return null;
}
this.max = function() {
return minNode(root);
}
var searchNode = function(node, key) {
if (!node) {
return false;
}
if (node.key > key) {
searchNode(node.left, key);
} else if (node.key < key) {
searchNode(node.right, key);
}
else {
return true;
}
}
this.search = function(key) {
searchNode(root, key);
}
var findMinNode = function(node) {
if (node) {
while(node && node.left) {
node = node.left;
}
return node
}
return null;
}
var removeNode = function(node, key) {
if (node === null) {
return null;
}
if (node.key > key) {
node.left = removeNode(node.left, key);
} else if (node.key < key){
node.right = removeNode(node.right, key);
} else {
if (node.left === null && node.right === null) {
node = null;
}
if (node.left === null) {
node = node.right;
}
if (node.right === null) {
node = node.left;
}
var aux = findMinNode(node.right);
node.key = aux.key;
node.right = removeNode(node.right, aux.key);
}
return node;
}
this.remove = function(key) {
root = removeNode(root, key)
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach((key) =>{
binaryTree.insert(key);
})
var preOrderTraverseRet = []
var inOrderTraverseRet = []
var postOrderTraverseRet = []
var callback = function(key) {
console.log(key);
}
binaryTree.preOrderTraverse(key => {
preOrderTraverseRet.push(key);
})
binaryTree.inOrderTraverse(key => {
inOrderTraverseRet.push(key);
})
binaryTree.postOrderTraverse(key => {
postOrderTraverseRet.push(key);
})