3
3
#include < list>
4
4
using namespace std ;
5
5
6
- list < int > arr;
6
+ // Create binary searching tree structure
7
7
struct node {
8
8
int val;
9
9
struct node * left, * right;
10
10
};
11
+
12
+ // Insert a number n to node's val, and pointer node's left and right
13
+ // branch to NULL
11
14
struct node * insert (int n) {
12
15
struct node * temp = new struct node ();
13
16
temp -> val = n;
14
17
temp -> left = NULL ;
15
18
temp -> right = NULL ;
16
19
return temp;
17
20
}
21
+
22
+ // Inorder permutation, permutate from left to root to right,
23
+ // accordingly
18
24
void inorder (struct node * root) {
19
25
if (root != NULL ) {
20
26
inorder (root -> left);
21
27
cout << root -> val << " " ;
22
- arr.push_back (root -> val);
23
28
inorder (root -> right);
24
29
}
25
30
}
31
+
32
+ // Postorder permutation, permutate from left to right to root,
33
+ // accoringly
26
34
void postorder (struct node * root) {
27
35
if (root != NULL ) {
28
36
postorder (root -> right);
29
37
postorder (root -> left);
30
38
cout << root -> val << " " ;
31
39
}
32
40
}
41
+
42
+ // Preorder permutation, permutate from root to left to right,
43
+ // accorginly
33
44
void preorder (struct node * root) {
34
45
if (root != NULL ) {
35
46
cout << root -> val << " " ;
36
47
preorder (root -> left);
37
48
preorder (root -> right);
38
49
}
39
50
}
51
+
52
+ // Create a new binary searching tree structure
40
53
struct node * create (struct node * root, int n) {
54
+ // If root(node) is NULL, then insert a value n as it's value
41
55
if (root == NULL )
42
56
insert (n);
57
+ // Find the node that is NULL, else if val > n, then check root's left
58
+ // node is empty or not, else if val < n, then check root's right node
59
+ // is empty or not
43
60
else if (root -> val > n) {
44
61
root -> left = create (root -> left, n);
45
62
} else
46
63
root -> right = create (root -> right, n);
47
64
}
48
65
66
+ // Find a specific node that the val is equal to key
49
67
bool find (struct node * root, int key) {
68
+ // If val > key and the next left node is NULL, return not found(0)
50
69
if (root -> val > key && root -> left == NULL )
51
70
return 0 ;
71
+ // If val < key and the next right node is NULL, return not found(0)
52
72
if (root -> val < key && root -> right == NULL )
53
73
return 0 ;
74
+ // If val = key then return found(1)
54
75
if (root -> val == key)
55
76
return 1 ;
77
+ // If val > key and the next left node is not NULL, then go to next
78
+ // left node and keep searching
79
+ // If val < key and the next right node is not NULL, then go to next
80
+ // right node and keep searching
81
+ // Else return not found(0)
56
82
else if (root -> val > key) {
57
83
find (root -> left, key);
58
84
} else if (root -> val < key) {
@@ -61,7 +87,9 @@ bool find(struct node * root, int key) {
61
87
return 0 ;
62
88
}
63
89
int main () {
90
+ // Init a empty node bst
64
91
struct node * bst = NULL ;
92
+ // Input each value of bst
65
93
bst = create (bst, 5 );
66
94
create (bst, 10 );
67
95
create (bst, 20 );
@@ -71,10 +99,13 @@ int main() {
71
99
create (bst, 3 );
72
100
create (bst, 12 );
73
101
create (bst, 21 );
102
+ // Inorder permutate from left -> root -> right
74
103
inorder (bst);
75
104
cout << endl;
105
+ // Postorder permutate from left -> right -> root
76
106
postorder (bst);
77
107
cout << endl;
108
+ // Preorder permutate from root -> left -> right
78
109
preorder (bst);
79
110
cout << endl;
80
111
0 commit comments