-
Notifications
You must be signed in to change notification settings - Fork 1
/
4.BST.cpp
65 lines (58 loc) · 1.49 KB
/
4.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*************************************************************************
> File Name: 4.BST.cpp
> Author: rEn Chong
> Mail: [email protected]
> Created Time: 四 2/16 01:22:22 2023
************************************************************************/
#include <iostream>
#include <ctime>
using std::cin;
using std::cout;
using std::endl;
struct Node {
public :
Node(int key, Node *lchild, Node *rchild) : key(key), lchild(lchild), rchild(rchild) {}
int key;
struct Node *lchild;
struct Node *rchild;
};
Node *getNewNode(int key) {
Node *p = new Node(key, nullptr, nullptr);
return p;
}
Node *random_insert(Node *root, int key) {
if (root == nullptr) return getNewNode(key);
if (rand() % 2) {
root->lchild = random_insert(root->lchild, key);
} else {
root->rchild = random_insert(root->rchild, key);
}
return root;
}
void pre_order(Node *root) {
if (root == nullptr) return ;
cout << root->key << " ";
pre_order(root->lchild);
pre_order(root->rchild);
return ;
}
void in_order(Node *root) {
if (root == nullptr) return ;
in_order(root->lchild);
cout << root->key << " ";
in_order(root->rchild);
return ;
}
int main(int argc, char *argv[]) {
srand(time(0));
if (argc != 2) return 0;
int MAX_N = atoi(argv[1]);
Node *root = nullptr;
for (int i = 1; i <= MAX_N; i++) {
root = random_insert(root, i);
}
pre_order(root);
cout << endl;
in_order(root);
return 0;
}