Advertisement
Guest User

Untitled

a guest
May 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. void Insert(NOD<T>* currentNode,const T &dataToInsert,NOD<T>* previous,int direction)
  2. {
  3. if (root == NULL)
  4. {
  5. NOD<T>* NodeToInsert = new NOD<T>(dataToInsert);
  6. root = NodeToInsert;
  7. return;
  8. }
  9. if (currentNode == NULL)
  10. {
  11. NOD<T>* NodeToInsert = new NOD<T>(dataToInsert);
  12. NodeToInsert->par = previous;
  13. if (direction == 1)
  14. previous->st = NodeToInsert;
  15. else
  16. previous->dr = NodeToInsert;
  17. Insert_Repair(NodeToInsert);
  18. return;
  19. }
  20. else
  21. if (dataToInsert < currentNode->data)
  22. {
  23. direction = 1;
  24. Insert(currentNode->st, dataToInsert,currentNode,direction);
  25. currentNode->setHeight();
  26. }
  27. else
  28. {
  29. direction = 2;
  30. Insert(currentNode->dr, dataToInsert,currentNode,direction);
  31. currentNode->setHeight();
  32. }
  33.  
  34. }
  35. void Insert_Repair(NOD<T>* currentNode)
  36. {
  37. if (currentNode->par == nullptr)
  38. return;
  39. NOD<T>* x = currentNode->par;
  40. x->setBalance();
  41. if (x->balance == 0)
  42. return;
  43. else
  44. if (x->balance == 1 || x->balance == -1)
  45. Insert_Repair(x);
  46. else
  47. {
  48. if (x->balance == -2)
  49. {
  50. if (x->st->balance == -1)
  51. {
  52. NOD<T>* y = x->st;
  53. right_Rotation(x);
  54. x->setHeight();
  55. y->setHeight();
  56. x->setBalance();
  57. y->setBalance();
  58. }
  59. else
  60. {
  61. NOD<T>* y = x->st;
  62. NOD <T>* yd = y->dr;
  63. Left_Rotation(y);
  64. right_Rotation(x);
  65. x->setHeight();
  66. y->setHeight();
  67. yd->setHeight();
  68. x->setBalance();
  69. y->setBalance();
  70. yd->setBalance();
  71. }
  72. }
  73. else
  74. {
  75. if (x->dr->balance == 1)
  76. {
  77. NOD<T>* y = x->dr;
  78. Left_Rotation(x);
  79. x->setHeight();
  80. y->setHeight();
  81. x->setBalance();
  82. y->setBalance();
  83. }
  84. else
  85. {
  86. NOD<T>* y = x->dr;
  87. NOD<T>* yd = y->st;
  88. right_Rotation(y);
  89. Left_Rotation(x);
  90. x->setHeight();
  91. y->setHeight();
  92. yd->setHeight();
  93. x->setBalance();
  94. y->setBalance();
  95. yd->setBalance();
  96. }
  97. }
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement