-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathBST.cpp
executable file
·50 lines (42 loc) · 1.32 KB
/
BST.cpp
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
//
// Created by light on 19-9-2.
//
#include "BST.h"
// 测试二分搜索树中的predecessor和successor两个函数
int main(){
// 生成 0 到 N-1 一共 N 个数字的数组
int N = 1000;
int* nums = new int[N];
for( int i = 0 ; i < N ; i ++)
nums[i] = i;
// 将数组中的数组乱序
shuffle(nums, N);
// 将这个N个数插入到二叉树中
BST<int,int> bst;
for(int i = 0 ; i < N ; i ++ )
bst.insert(i, i);
// 测试前驱算法, 除了数字0没有前驱, 每个数字x的前驱应该为x-1
for(int i = 0 ; i < N ; i ++){
if( i == 0 ){
assert(bst.predecessor(i) == NULL);
cout << "The predesessor of 0 is NULL" << endl;
}
else{
assert(bst.predecessor(i)->key == i-1);
cout << "The predesessor of " << i << " is " << i-1 << endl;
}
}
cout<<endl;
// 测试后继算法, 除了数字没有N-1后继, 每个数字x的后继应该为x+1
for(int i = 0 ; i < N ; i ++){
if( i == N-1 ){
assert(bst.successor(i) == NULL);
cout << "The successor of " << i << " is NULL" << endl;
}
else{
assert(bst.successor(i)->key == i+1);
cout << "The successor of " << i << " is " << i+1 << endl;
}
}
return 0;
}