Skip to content

Commit

Permalink
prompt message optimize.
Browse files Browse the repository at this point in the history
  • Loading branch information
liushubin lwx470335 committed Jul 16, 2020
1 parent 55435a2 commit d51dfff
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions data_structures/binary_search_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,33 @@ struct node {

/** 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] root root node of a tree
* \param[in] x a node with value to be insert
*/
void Insert(node* n, int x) {
assert(n != NULL);
if (x < n->val) {
if (n->left == nullptr) {
n->left = std::shared_ptr<node>(new node);
n->left->val = x;
n->left->left = nullptr;
n->left->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;
}

if (x < root->val) {
if (root->left == nullptr) {
root->left = std::shared_ptr<node>(new node);
root->left->val = x;
root->left->left = nullptr;
root->left->right = nullptr;
} else {
Insert(n->left.get(), x);
Insert(root->left.get(), x);
}
} else {
if (n->right == nullptr) {
n->right = std::shared_ptr<node>(new node);
n->right->val = x;
n->right->left = nullptr;
n->right->right = nullptr;
if (root->right == nullptr) {
root->right = std::shared_ptr<node>(new node);
root->right->val = x;
root->right->left = nullptr;
root->right->right = nullptr;
} else {
Insert(n->right.get(), x);
Insert(root->right.get(), x);
}
}
}
Expand Down Expand Up @@ -247,7 +252,8 @@ int main() {
<< "\n3. Breadth First"
<< "\n4. Preorder Depth First"
<< "\n5. Inorder Depth First"
<< "\n6. Postorder Depth First";
<< "\n6. Postorder Depth First"
<< "\n7. any other number will exit.";

std::cout << "\nEnter Your Choice : ";
std::cin >> ch;
Expand Down

0 comments on commit d51dfff

Please sign in to comment.