Advertisement
Guest User

My Spaghetti Code

a guest
Nov 13th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. template <class T>
  2. bool binary_search_tree<T>::insert(const T &item) {
  3. binary_tree_node<T> *p = root;
  4. if(root == NULL){
  5. root = new binary_tree_node<T>(item);
  6. return true;
  7. }
  8. while((root->left() != NULL || item > root->data()) && (root->right() != NULL || item < root->data())) {
  9. if(item < root->data())
  10. root = root->left();
  11. else if(item > root->data())
  12. root = root->right();
  13. else
  14. return false;
  15. }
  16. if(item < root->data() && root->left() == NULL)
  17. root->set_left(new binary_tree_node<T>(item));
  18. if(item > root->data() && root->right() == NULL)
  19. root->set_right(new binary_tree_node<T>(item));
  20. root = p;
  21. return true;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement