Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. void bst::recInsert(string s, node * & r)
  2. {
  3. if( r == NULL ) //base case: empty tree
  4. {
  5. r = new node;
  6. r->data = s;
  7. r->height = 0;
  8. }
  9. else if( s < r->data ) //insert left
  10. {
  11. recInsert(s, r->left);
  12.  
  13. if( height(r->left) - height(r->right) > 1 )
  14. { //violation!
  15. //do some type of rotation
  16. else if( s < r->left->data )
  17. leftRotation(r);
  18. else //its a double
  19. doubleRotationLeft(r);
  20. if(s>r->data)
  21. }
  22.  
  23. r->height = max( height(r->left), height(r->right) ) +1;
  24. }
  25. else//insert right, if greater or equal to root
  26. {
  27. recInsert(s, r->right);
  28.  
  29. if( height(r->right) - height(r->left) > 1 )
  30. {
  31. if( s > r->right->data )
  32. rightRotation(r);
  33. else
  34. doubleRotationRight(r);
  35. }
  36.  
  37. r->height = max( height(r->left), height(r->right) ) +1;
  38. }
  39. }
Add Comment
Please, Sign In to add comment