-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.js
156 lines (137 loc) · 3.87 KB
/
BinarySearchTree.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
function BinarySearchTree() { //create a tree obj
var Node = function (key) { //a node and it's elements
this.left = null;
this.key = key;
this.right = null;
};
var root = null;
/*
* 插入
* 1.构造一个新节点
* 2.判断是否是树根
* 3.判断大小,构造私有方法进行递归插入
* 从根向下判断插入位置
* */
this.insert = function (key) {
var newNode = new Node(key);
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
};
var insertNode = function (node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
insertNode(node.right, newNode);
}
}
};
/*中序遍历*/
this.inOrderTraverse = function (callback) {
inOrderTraverseNode(root, callback);
};
var inOrderTraverseNode = function (node, callback) {
if (node !== null) {
inOrderTraverseNode(node.left, callback);
callback(node.key);
inOrderTraverseNode(node.right, callback);
}
};
/*先序遍历*/
this.preOrderTraverse = function (callback) {
preOrderTraverseNode(root, callback);
};
var preOrderTraverseNode = function (node, callback) {
if (node !== null) {
callback(node.key);
preOrderTraverseNode(node.left, callback);
preOrderTraverseNode(node.right, callback);
}
};
/*后序遍历*/
this.postOrderTraverse = function (callback) {
postOrderTraverseNode(root, callback);
};
var postOrderTraverseNode = function (node, callback) {
if (node !== null) {
postOrderTraverseNode(node.left, callback);
postOrderTraverseNode(node.right, callback);
callback(node.key);
}
};
/*搜索最小值
* 二叉搜索树的最小值为最后一层最左侧的节点
* */
this.min = function () {
return minNode(root);
};
var minNode = function (node) {
if (node) {
while (node && node.left !== null) {
node = node.left;
}
return node.key;
}
return null;
};
/*搜索最大值
* 二叉搜索树的最大值为最后一层最右侧的节点
* */
this.max = function () {
return maxNode(root);
};
var maxNode = function (node) {
if (node) {
while (node && node.right !== null) {
node = node.right;
}
return node.key;
}
return null;
};
this.search = function (key) {
return searchNode(root, key)
};
var searchNode = function (node, key) {
if (node === null) { //递归出口1
return false
}
if (key < node.key) {
return searchNode(node.left, key);
} else if (key > node.key) {
return searchNode(node.right, key);
} else {
return true
}
}
}
var tree = new BinarySearchTree();
[3, 4, 5, 6, 34, 21, 7, 8, 14, 15, 10].forEach(function (item) {
tree.insert(item);
});
console.log('前序遍历: \n');
tree.inOrderTraverse(function (val) {
console.log(val)
});
console.log('中序遍历: \n');
tree.preOrderTraverse(function (val) {
console.log(val)
});
console.log('后序遍历:\n');
tree.postOrderTraverse(function (val) {
console.log(val)
});
console.log('最小值和最大值:');
console.log(tree.min());
console.log(tree.max()+'\n');
console.log('搜索:');
console.log(tree.search(4)+'\n');