Skip to content

Commit

Permalink
Revert "optimize Insert() function as review suggestion."
Browse files Browse the repository at this point in the history
revert to last good code。
This reverts commit adb42e4.
  • Loading branch information
liushubin lwx470335 committed Jul 17, 2020
1 parent 2e13fd6 commit 051bc1b
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions data_structures/binary_search_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ struct node {
* \param[in] root root node of a tree
* \param[in] x a node with value to be insert
*/
void Insert(std::shared_ptr<node>& root, int x) {
if (root == nullptr) {
root = std::make_shared<node>();
root->val = x;
root->left = nullptr;
root->right = nullptr;
void Insert(node* root, int x) {
if (root == NULL) {
std::cout << " \n invalid root node = " << root << std::endl;
std::cout << " \n please call Insert() with valid root node!!! \n";
return;
}

Expand All @@ -51,7 +49,7 @@ void Insert(std::shared_ptr<node>& root, int x) {
root->left->left = nullptr;
root->left->right = nullptr;
} else {
Insert(root->left, x);
Insert(root->left.get(), x);
}
} else {
if (root->right == nullptr) {
Expand All @@ -60,7 +58,7 @@ void Insert(std::shared_ptr<node>& root, int x) {
root->right->left = nullptr;
root->right->right = nullptr;
} else {
Insert(root->right, x);
Insert(root->right.get(), x);
}
}
}
Expand Down Expand Up @@ -203,12 +201,12 @@ void test_tree() {
root->left = nullptr;
root->right = nullptr;
// test Insert()
data_structure::BST::Insert(root, 2);
data_structure::BST::Insert(root, 1);
data_structure::BST::Insert(root, 3);
data_structure::BST::Insert(root, 6);
data_structure::BST::Insert(root, 5);
data_structure::BST::Insert(root, 7);
data_structure::BST::Insert(root.get(), 2);
data_structure::BST::Insert(root.get(), 1);
data_structure::BST::Insert(root.get(), 3);
data_structure::BST::Insert(root.get(), 6);
data_structure::BST::Insert(root.get(), 5);
data_structure::BST::Insert(root.get(), 7);
std::cout
<< "after Insert() ,the expected output should be : 1, 2, 3, 4, 5, 6 ,7"
<< std::endl;
Expand Down Expand Up @@ -240,7 +238,13 @@ int main() {
test_tree();
int value = 0;
int ch = 0;
std::shared_ptr<data_structure::BST::node> root = nullptr;
std::shared_ptr<data_structure::BST::node> root(
new data_structure::BST::node);
std::cout << "\nEnter the value of root node :";
std::cin >> value;
root->val = value;
root->left = nullptr;
root->right = nullptr;

do {
std::cout << "\n1. Insert"
Expand All @@ -258,7 +262,14 @@ int main() {
case 1:
std::cout << "\nEnter the value to be Inserted : ";
std::cin >> x;
data_structure::BST::Insert(root, x);
if (root.get())
data_structure::BST::Insert(root.get(), x);
else {
root = std::make_shared<data_structure::BST::node>();
root->val = x;
root->left = nullptr;
root->right = nullptr;
}
break;
case 2:
std::cout << "\nEnter the value to be Deleted : ";
Expand Down

0 comments on commit 051bc1b

Please sign in to comment.