Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. //LEVELORDER
  2. void levelorder() const {
  3. queue<Node*> que;
  4. que.push(root);
  5.  
  6. while(!que.empty()) {
  7. Node* current = que.front();
  8. que.pop();
  9. if (current) {
  10. cout << current->data << " ";
  11.  
  12. if (current->left) {
  13. que.push(current->left);
  14. }
  15. if (current->right) {
  16. que.push(current->right);
  17. }
  18. }
  19. }
  20. }
  21.  
  22. //REMOVE ITEM
  23. Node* _remove(int value, Node* current) {
  24. if (!current) {
  25. return nullptr;
  26. }
  27.  
  28. if (value < current->data) {
  29. current->left = _remove(value, current->left);
  30. } else if (value > current->data) {
  31. current->right = _remove(value, current->right);
  32. } else { // value = current->data;
  33. if (!current->left && !current->right) {
  34. free(current);
  35. return nullptr;
  36. } else if (!current->left) {
  37. Node* tempRight = current->right;
  38. free(current);
  39. return tempRight;
  40. } else if (!current->right) {
  41. Node* tempLeft = current->left;
  42. free(current);
  43. return tempLeft;
  44. } else {
  45. Node* swapWith = current->right;
  46. while (swapWith->left) {
  47. swapWith = swapWith->left;
  48. }
  49.  
  50. current->data = swapWith->data;
  51. current->right = _remove(swapWith->data, current->right);
  52. }
  53. }
  54.  
  55. current->calculateHeight();
  56. current->fixTree();
  57.  
  58. return current;
  59. }
  60.  
  61. // Function to print Nodes in a binary tree in Level order
  62. void LevelOrder(Node* root) //DLR (Data-Left-Right)
  63. {
  64. if (root == nullptr) return;
  65. std::queue<Node*> Q;
  66. Q.push(root);
  67. while (!Q.empty())
  68. {
  69. Node* current = Q.front();
  70. Q.pop(); // removing the element at front
  71. std::cout << current->data << " ";
  72. if (current->left != nullptr) Q.push(current->left);
  73. if (current->right != nullptr) Q.push(current->right);
  74. }
  75. }
  76.  
  77. // Function to print Nodes in a binary tree in Level order
  78. void LevelOrder(Node* root) //DLR (Data-Left-Right)
  79. {
  80. if (root == nullptr) return;
  81. std::queue<Node*> Q;
  82. Q.push(root);
  83. Q.push(nullptr); // whenever a level is over we will insert nullptr
  84. //while there is at least one discovered node
  85. while (!Q.empty())
  86. {
  87. Node* current = Q.front();
  88.  
  89. if (current == nullptr)
  90. {
  91. Q.pop(); // removing the element at front
  92. Q.push(nullptr); // takes care of the next level
  93. if (Q.front()==nullptr)
  94. {
  95. return; // if there are two consecutive nullptr in the queue
  96. }
  97. std::cout << '\n';
  98. }
  99. else
  100. {
  101. if (current->left != nullptr) Q.push(current->left);
  102. if (current->right != nullptr) Q.push(current->right);
  103. std::cout << current->data << " ";
  104. Q.pop();
  105. }
  106.  
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement