-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_BST.cpp
More file actions
77 lines (61 loc) · 2.04 KB
/
test_BST.cpp
File metadata and controls
77 lines (61 loc) · 2.04 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
#include <iostream>
#include "BST.h" // 假设 BinarySearchTree 类定义在这个头文件中
void testBinarySearchTree() {
BinarySearchTree<int> bst;
// 测试插入功能
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
bst.insert(12);
bst.insert(18);
// 测试打印树结构
std::cout << "Initial Tree:" << std::endl;
bst.printTree();
// 测试查找最小和最大元素
std::cout << "Minimum element: " << bst.findMin() << std::endl;
std::cout << "Maximum element: " << bst.findMax() << std::endl;
// 测试 contains 功能
std::cout << "Contains 7? " << (bst.contains(7) ? "Yes" : "No") << std::endl;
std::cout << "Contains 20? " << (bst.contains(20) ? "Yes" : "No") << std::endl;
// 测试删除功能
bst.remove(7);
std::cout << "Tree after removing 7:" << std::endl;
bst.printTree();
bst.remove(10);
std::cout << "Tree after removing 10:" << std::endl;
bst.printTree();
// 测试清空树
bst.makeEmpty();
std::cout << "Tree after making empty:" << std::endl;
bst.printTree();
// 测试是否为空
std::cout << "Is tree empty? " << (bst.isEmpty() ? "Yes" : "No") << std::endl;
// 测试拷贝构造函数和赋值运算符
BinarySearchTree<int> bst2;
bst2.insert(1);
bst2.insert(3);
bst2.insert(2);
BinarySearchTree<int> bst3(bst2);
std::cout << "Copied Tree (bst3):" << std::endl;
bst3.printTree();
BinarySearchTree<int> bst4;
bst4 = bst2;
std::cout << "Assigned Tree (bst4):" << std::endl;
bst4.printTree();
// 测试移动构造函数和移动赋值运算符
BinarySearchTree<int> bst5(std::move(bst2));
std::cout << "Moved Tree (bst5):" << std::endl;
bst5.printTree();
BinarySearchTree<int> bst6;
bst6 = std::move(bst5);
std::cout << "Move Assigned Tree (bst6):" << std::endl;
bst6.printTree();
BinarySearchTree<int> bst7;
bst7.findMax(); // 测试异常处理
}
int main() {
testBinarySearchTree();
return 0;
}