Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add comments and explanation to binary_search_tree.cpp data_structures #891

Closed
wants to merge 49 commits into from
Closed
Changes from 3 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a702702
docs: add comments and explanation to binary_search_tree.cpp data_str…
Jun 24, 2020
7b53dda
formatting source-code for a70270287c018f6a6fcd8a398d5d6bacda5f1190
Jun 24, 2020
8d4bcc8
Merge branch 'master' into main
liushubin-gitHub Jun 24, 2020
f377860
test: add test for binary_search_tree.cpp data_structures
liushubin-gitHub Jun 25, 2020
3e8b19f
formatting source-code for f377860175a572d585f402912ae3986e5d044600
Jun 25, 2020
6b10a56
Update binary_search_tree.cpp
liushubin-gitHub Jun 25, 2020
dba2daf
test: add test for binary_search_tree.cpp data_structures
Jun 28, 2020
6910ca5
formatting source-code for dba2daf3425d648feb8ea04b435ce5dcc8e69ebc
Jun 28, 2020
4f62e91
fix: modify BFT() breath first search a tree
Jul 10, 2020
49ccaa6
formatting source-code for 4f62e91ef83b387fb68bf9bf0d3e9fc42a8b55e0
Jul 10, 2020
cf8917e
fix:free memory for auto test case
liushubin-gitHub Jul 10, 2020
9dafdfa
formatting source-code for cf8917e442723333efd6881e68fed934ef384168
Jul 10, 2020
c56b749
fix:use a pointer instead a non-const reference
liushubin-gitHub Jul 11, 2020
8e73926
fix code format auto test erros.
liushubin-gitHub Jul 11, 2020
12cbebc
formatting source-code for 8e73926f74b04086c1d40666aa08bb8fd4d0328a
Jul 11, 2020
e8033e3
fix:code format errors. part01
liushubin-gitHub Jul 11, 2020
1168770
formatting source-code for e8033e3db7dde6d206513ad3f2401e807ce11aaf
Jul 11, 2020
fa4fc23
use smart pointer.
liushubin-gitHub Jul 12, 2020
832934d
formatting source-code for fa4fc234c5c1bb090afa171faffcc0616e843334
Jul 12, 2020
cd80e82
use smart uniq_ptr
liushubin-gitHub Jul 12, 2020
6091828
formatting source-code for cd80e82567f7baac032695763eee8b7aabcad8d0
Jul 12, 2020
7756544
fix clang-tixy errors
kvedala Jul 12, 2020
67a5471
formatting source-code for 77565449f258e0cd16ee867a81590e3e9c758efc
Jul 12, 2020
9c5ab6d
organize code using namespaces
kvedala Jul 12, 2020
c1ab5ca
formatting source-code for 9c5ab6d955ed7b048dd4abfb31e82f8cbe7bb9df
Jul 12, 2020
bbbd574
reset smart pointer to release memory auto.
Jul 14, 2020
55d9ca7
fix:optimize Remove() function to use smart pointer.
Jul 14, 2020
f322675
formatting source-code for 55d9ca73a453770cf5b587d7fcbab4b9679a9d16
Jul 14, 2020
b5c8218
fix:auto test compile error.
Jul 14, 2020
511763a
formatting source-code for b5c821874e798836fd866fa883d06b320a050207
Jul 14, 2020
4b1c983
fix:auto test compile error v2.
Jul 14, 2020
0464553
formatting source-code for 4b1c9832d2f55151bc6eb4941e7bb7f1414f8870
Jul 14, 2020
481a382
formatting source-code for cd80e82567f7baac032695763eee8b7aabcad8d0
Jul 12, 2020
a4e9cd3
fix: nullptr special case when Remove a node dosen't exit.
liushubin-gitHub Jul 14, 2020
182de88
fix: add nullptr check.
liushubin-gitHub Jul 14, 2020
3f360b1
fix: add nullptr check v2.
liushubin-gitHub Jul 14, 2020
980a49c
formatting source-code for 3f360b18593b6d9694fcc12c07fa29cfe368c5bb
Jul 14, 2020
15fee70
optimize Remove() function .
Jul 15, 2020
6de45e7
Update binary_search_tree.cpp
liushubin-gitHub Jul 15, 2020
2852735
formatting source-code for 6de45e7553cae37b2cbfec2c6d60960eef1d27ea
Jul 15, 2020
55435a2
Update binary_search_tree.cpp
liushubin-gitHub Jul 15, 2020
d51dfff
prompt message optimize.
Jul 16, 2020
fb5971d
formatting source-code for d51dfff1bd5f8cf96d5e2ee593b90ed4bf025d39
Jul 16, 2020
adb42e4
optimize Insert() function as review suggestion.
liushubin-gitHub Jul 16, 2020
2e13fd6
formatting source-code for adb42e42c6412d56f91fe27e1f2f5906ca98cb55
Jul 16, 2020
051bc1b
Revert "optimize Insert() function as review suggestion."
Jul 17, 2020
19bdd29
optimize Insert() Remove() function to use const reference.
liushubin-gitHub Jul 17, 2020
0d844ff
formatting source-code for 19bdd2911faa63a702955caa30c22f6b3a0e939a
Jul 17, 2020
0932f88
Revert "optimize Insert() Remove() function to use const reference."
liushubin-gitHub Jul 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 67 additions & 7 deletions data_structures/binary_search_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
*/
#include <iostream>

/** An struct node type.
* Define a element for tree insert/remove operation
*/
struct node {
int val;
node *left;
node *right;
int val; /**< int value of a node struct*/
node *left; /**< left subtree pointer */
node *right; /**< right subtree pointer */
};

struct Queue {
Expand All @@ -26,6 +29,11 @@ void enqueue(node *n) { queue.t[queue.rear++] = n; }

node *dequeue() { return (queue.t[queue.front++]); }

/** insert a node to tree
* with greater value to right subtree, smaller value node to left subtree
* \param[in] n root node of a tree
* \param[in] x a node with value to be insert
*/
void Insert(node *n, int x) {
if (x < n->val) {
if (n->left == NULL) {
Expand All @@ -50,37 +58,50 @@ void Insert(node *n, int x) {
}
}

/** find max node value of a left subtree
* \param[in] n the root node pointer of subtree
* \return the max node int value
*/
int findMaxInLeftST(node *n) {
while (n->right != NULL) {
n = n->right;
}
return n->val;
}

/** remove a node from tree
* travesal a tree and find a node ,then delete it.
* \param[in] p parent node of start node
* \param[in] n start node to search a node with value x
* \param[in] x the int value of a node
*/
void Remove(node *p, node *n, int x) {
if (n->val == x) {
if (n->right == NULL && n->left == NULL) {
if (x < p->val) {
if (x > p->val) {
p->right = NULL;
} else {
p->left = NULL;
}
delete n;
} else if (n->right == NULL) {
if (x < p->val) {
if (x > p->val) {
p->right = n->left;
} else {
p->left = n->left;
}
delete n;
} else if (n->left == NULL) {
if (x < p->val) {
if (x > p->val) {
p->right = n->right;
} else {
p->left = n->right;
}
delete n;
} else {
int y = findMaxInLeftST(n->left);
n->val = y;
Remove(n, n->right, y);
Remove(n, n->left, y);
}
} else if (x < n->val) {
Remove(n, n->left, x);
Expand All @@ -98,6 +119,10 @@ void BFT(node *n) {
}
}

/** traverse a tree with Pre order
* and print the node value
* \param[in] n the root node pointer of a tree
*/
void Pre(node *n) {
if (n != NULL) {
std::cout << n->val << " ";
Expand All @@ -106,6 +131,10 @@ void Pre(node *n) {
}
}

/** traverse a tree with In order
* and print the node value
* \param[in] n the root node pointer of a tree
*/
void In(node *n) {
if (n != NULL) {
In(n->left);
Expand All @@ -114,6 +143,10 @@ void In(node *n) {
}
}

/** traverse a tree with Post order
* and print the node value
* \param[in] n the root node pointer of a tree
*/
void Post(node *n) {
if (n != NULL) {
Post(n->left);
Expand All @@ -122,7 +155,33 @@ void Post(node *n) {
}
}

void test_tree() {
node *root = new node;
liushubin-gitHub marked this conversation as resolved.
Show resolved Hide resolved
root->val = 4;
root->left = NULL;
root->right = NULL;
// test Insert()
Insert(root, 2);
Insert(root, 1);
Insert(root, 3);
Insert(root, 6);
Insert(root, 5);
Insert(root, 7);
std::cout
liushubin-gitHub marked this conversation as resolved.
Show resolved Hide resolved
<< "after Insert() ,the expected output should be : 1, 2, 3, 4, 5, 6 ,7"
<< std::endl;
In(root);
// test Remove()
Remove(root, root, 2);
std::cout << "\n after Remove() node 2 , the expected output should be : "
"1, 3, 4, 5, 6, 7"
<< std::endl;
In(root);
}

int main() {
// test tree
test_tree();
queue.front = 0;
queue.rear = 0;
int value;
Expand All @@ -133,6 +192,7 @@ int main() {
root->val = value;
root->left = NULL;
root->right = NULL;

do {
std::cout << "\n1. Insert"
<< "\n2. Delete"
Expand Down