Guest User

Untitled

a guest
Jul 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /*********************************
  2. Name: Mary Soy
  3. Date: 11-13-09
  4. Class: 3333
  5. Professor: Schweller
  6. File: AVLTree.h
  7. *********************************/
  8. #include <iostream>
  9. #include <string>
  10. using namespace std;
  11.  
  12. class node
  13. {
  14. public:
  15. node()
  16. {
  17. right = NULL;
  18. left = NULL;
  19. height = 0;
  20. }
  21.  
  22. string data;
  23. node * right;
  24. node * left;
  25. int height;
  26. };
  27.  
  28. class bst
  29. {
  30. public:
  31. bst();
  32.  
  33. void insert(string s);
  34. void display();
  35.  
  36. //string find(string s);
  37.  
  38. private:
  39. void rightRotation( node * & r );
  40. void leftRotation( node * & r );
  41. void doubleRotationRight( node * & r );
  42. void doubleRotationLeft( node * & r );
  43.  
  44. int height(node *);
  45. void recInsert(string s, node * & root);
  46. void recDisplay(node * r);
  47. node * root;
  48. };
  49.  
  50. bst::bst()
  51. {
  52. root = NULL;
  53. }
  54.  
  55. int bst::height(node * p)
  56. {
  57. if( p == NULL )
  58. return -1;
  59. else
  60. return p->height;
  61. }
  62.  
  63.  
  64. void bst::rightRotation( node * & r )
  65. {
  66. node * a = r;
  67. node * b = r->right;
  68. node * bleft = b->left;
  69.  
  70. r = b;
  71. a->right = bleft;
  72. b->left = a;
  73.  
  74. a->height = max( height(a->right), height(a->left) ) +1;
  75. b->height = max( height(b->right), height(b->left) ) +1;
  76. }
  77.  
  78.  
  79. void bst::leftRotation( node * & r )
  80. {
  81. node * a = r;
  82. node * b = r->left;
  83. node * bright = b->right;
  84.  
  85. r = b;
  86. a->left = bright;
  87. b->right = a;
  88.  
  89. a->height = max( height(a->right), height(a->left) ) +1;
  90. b->height = max( height(b->right), height(b->left) ) +1;
  91. }
  92. void bst::doubleRotationRight( node * & r )
  93. {
  94. leftRotation( r->right );
  95. rightRotation( r );
  96. }
  97. void bst::doubleRotationLeft( node * & r )
  98. {
  99. rightRotation( r->left );
  100. leftRotation( r );
  101. }
  102.  
  103.  
  104.  
  105.  
  106. void bst::recInsert(string s, node * & r)
  107. {
  108. if( r == NULL ) //base case: empty tree
  109. {
  110. r = new node;
  111. r->data = s;
  112. r->height = 0;
  113. }
  114. else if( s < r->data ) //insert left
  115. {
  116. recInsert(s, r->left);
  117.  
  118. if( height(r->left) - height(r->right) > 1 )
  119. { //violation!
  120. //do some type of rotation
  121. if( s < r->left->data )
  122. leftRotation(r);
  123. else //its a double
  124. doubleRotationLeft(r);
  125. }
  126.  
  127. r->height = max( height(r->left), height(r->right) ) +1;
  128. }
  129. else//insert right, if greater or equal to root
  130. {
  131. recInsert(s, r->right);
  132.  
  133. if( height(r->right) - height(r->left) > 1 )
  134. {
  135. if( s > r->right->data )
  136. rightRotation(r);
  137. else
  138. doubleRotationRight(r);
  139. }
  140.  
  141. r->height = max( height(r->left), height(r->right) ) +1;
  142. }
  143. }
  144.  
  145. void bst::insert(string s)
  146. {
  147. recInsert(s, root);
  148. }
  149.  
  150. void bst::recDisplay(node * r)
  151. {
  152. if( r != NULL )
  153. {
  154. cout << r->data << endl;
  155. recDisplay(r->left);
  156. recDisplay(r->right);
  157. }
  158. }
  159.  
  160. void bst::display()
  161. {
  162. recDisplay(root);
  163. }
Add Comment
Please, Sign In to add comment