Skip to content

Commit e2cc753

Browse files
Merge pull request #389 from LuckyPigeon/bst_explanation
Add explanation of bst.cpp
2 parents 92bc79a + d5b1956 commit e2cc753

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

BST/bst.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,82 @@
33
#include <list>
44
using namespace std;
55

6-
list < int > arr;
6+
// Create binary searching tree structure
77
struct node {
88
int val;
99
struct node * left, * right;
1010
};
11+
12+
// Insert a number n to node's val, and pointer node's left and right
13+
// branch to NULL
1114
struct node * insert(int n) {
1215
struct node * temp = new struct node();
1316
temp -> val = n;
1417
temp -> left = NULL;
1518
temp -> right = NULL;
1619
return temp;
1720
}
21+
22+
// Inorder permutation, permutate from left to root to right,
23+
// accordingly
1824
void inorder(struct node * root) {
1925
if (root != NULL) {
2026
inorder(root -> left);
2127
cout << root -> val << " ";
22-
arr.push_back(root -> val);
2328
inorder(root -> right);
2429
}
2530
}
31+
32+
// Postorder permutation, permutate from left to right to root,
33+
// accoringly
2634
void postorder(struct node * root) {
2735
if (root != NULL) {
2836
postorder(root -> right);
2937
postorder(root -> left);
3038
cout << root -> val << " ";
3139
}
3240
}
41+
42+
// Preorder permutation, permutate from root to left to right,
43+
// accorginly
3344
void preorder(struct node * root) {
3445
if (root != NULL) {
3546
cout << root -> val << " ";
3647
preorder(root -> left);
3748
preorder(root -> right);
3849
}
3950
}
51+
52+
// Create a new binary searching tree structure
4053
struct node * create(struct node * root, int n) {
54+
// If root(node) is NULL, then insert a value n as it's value
4155
if (root == NULL)
4256
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
4360
else if (root -> val > n) {
4461
root -> left = create(root -> left, n);
4562
} else
4663
root -> right = create(root -> right, n);
4764
}
4865

66+
// Find a specific node that the val is equal to key
4967
bool find(struct node * root, int key) {
68+
// If val > key and the next left node is NULL, return not found(0)
5069
if (root -> val > key && root -> left == NULL)
5170
return 0;
71+
// If val < key and the next right node is NULL, return not found(0)
5272
if (root -> val < key && root -> right == NULL)
5373
return 0;
74+
// If val = key then return found(1)
5475
if (root -> val == key)
5576
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)
5682
else if (root -> val > key) {
5783
find(root -> left, key);
5884
} else if (root -> val < key) {
@@ -61,7 +87,9 @@ bool find(struct node * root, int key) {
6187
return 0;
6288
}
6389
int main() {
90+
// Init a empty node bst
6491
struct node * bst = NULL;
92+
// Input each value of bst
6593
bst = create(bst, 5);
6694
create(bst, 10);
6795
create(bst, 20);
@@ -71,10 +99,13 @@ int main() {
7199
create(bst, 3);
72100
create(bst, 12);
73101
create(bst, 21);
102+
// Inorder permutate from left -> root -> right
74103
inorder(bst);
75104
cout << endl;
105+
// Postorder permutate from left -> right -> root
76106
postorder(bst);
77107
cout << endl;
108+
// Preorder permutate from root -> left -> right
78109
preorder(bst);
79110
cout << endl;
80111

0 commit comments

Comments
 (0)