Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. int bst::insert()
  2. {
  3. int add = 1;
  4. /* cout << "Enter value you wish to insert \n";
  5. cin >> add;
  6. cin.ignore(100, '\n');*/
  7. node * temp = new node;
  8. temp->data = add;
  9. insert(&root, add, &temp);
  10. return 1;
  11. }
  12.  
  13. int bst::insert(node ** root, int add, node ** temp)
  14. {
  15. if(root == NULL);
  16. {
  17. return 0;
  18. }
  19. if(add >= (*root)->data)
  20. {
  21. //go right
  22. if((*root)->right != NULL)
  23. {
  24. insert(&(*root)->right, add, temp); //check if correct
  25. }
  26. else
  27. {
  28. (*root)->right = *temp; // check if correct
  29. (*temp)->left = NULL;
  30. (*temp)->right = NULL;
  31. }
  32. }
  33. else if(add < (*root)->data)
  34. {
  35. if((*root)->left != NULL)
  36. {
  37. insert(&(*root)->left, add, temp); //check if correct
  38. }
  39. else
  40. {
  41. (*root)->left = *temp; // check if correct
  42. (*temp)->left = NULL;
  43. (*temp)->right = NULL;
  44. }
  45. }
  46. return 1;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement