Repository navigation
Expand file tree
/
Copy pathTest.js
More file actions
89 lines (80 loc) · 1.93 KB
/
Copy pathTest.js
File metadata and controls
89 lines (80 loc) · 1.93 KB
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
// 测试二叉树
var BST = require('./BSTI');
// 测试代码
var bst = new BST();
// 插入数据
bst.insert(11);
bst.insert(7);
bst.insert(15);
bst.insert(5);
bst.insert(3);
bst.insert(9);
bst.insert(8);
bst.insert(10);
bst.insert(13);
bst.insert(12);
bst.insert(14);
bst.insert(20);
bst.insert(18);
bst.insert(25);
bst.insert(6);
// 测试前序遍历结果
var resultString = "";
bst.preOrderTraversal(function (key) {
resultString += key + " ";
})
console.log(resultString); // 11 7 5 3 6 9 8 10 15 13 12 14 20 18 25
// 测试中序遍历结果
resultString = "";
bst.inOrderTraversal(function (key) {
resultString += key + " ";
})
console.log(resultString); // 3 5 6 7 8 9 10 11 12 13 14 15 18 20 25
// 测试后续遍历结果
resultString = "";
bst.postOrderTraversal(function (key) {
resultString += key + " ";
})
console.log(resultString); // 3 6 5 8 10 9 7 12 14 13 18 25 20 15 11
// 获取最值
console.log(bst.getMin()); // 3
console.log(bst.getMax()); // 25
// 查找特定的值
console.log(bst.search(10)); // true
console.log(bst.search(21)); // false
// 查找数据
console.log(bst.remove(10)); // true
console.log(bst.remove(20)); // true
console.log(bst.remove(21)); // false
// 测试前序遍历结果
var resultString = "";
bst.preOrderTraversal(function (key) {
resultString += key + " ";
})
console.log(resultString); // 11 7 5 3 6 9 8 15 13 12 14 25 18
console.log("=================================测试二分搜索树=============================");
var bstii = new require('./BSTII.js');
var b = new bstii.BST();
b.insert(1, 5);
b.insert(5, 9);
b.insert(3, 10);
b.insert(6, 5);
b.insert(9, 9);
b.insert(8, 10);
b.insert(12, 5);
b.insert(18, 9);
b.insert(20, 10);
b.insert(25, 5);
b.insert(13, 9);
b.insert(7, 10);
b.levelOrder();
b.inOrder();
console.log(b.minimun());
console.log(b.maximun());
b.removeMin();
b.removeMax();
b.postOrder();
b.remove(20);
b.postOrder();
console.log(b.size());
console.log(b.isEmpty());