Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include "tree2.h"
  3. using namespace std;
  4. #include "queue.h"
  5. template<class T>
  6. void preorder(node2<T>* tree)
  7. {
  8. if (tree != NULL)
  9. {
  10. cout << tree->info << " ";
  11. preorder(tree->lnext);
  12. preorder(tree->rnext);
  13. }
  14. }
  15. template<class T>
  16. void inorder(node2<T>* tree)
  17. {
  18. if (tree != NULL)
  19. {
  20. inorder(tree->lnext);
  21. cout << tree->info << " ";
  22. inorder(tree->rnext);
  23. }
  24. }
  25. template<class T>
  26. void postorder(node2<T>* tree)
  27. {
  28. if (tree != NULL)
  29. {
  30. postorder(tree->lnext);
  31. postorder(tree->rnext);
  32. cout << tree->info << " ";
  33. }
  34. }
  35. template<class T>
  36. void levelorder(node2<T>* tree)
  37. {
  38. QUEUE<node2<T>*> q;
  39. if (tree != NULL)
  40. q.enqueue(tree);
  41. while (!q.isEmpty())
  42. {
  43. tree = q.dequeue();
  44. cout << tree->info << " ";
  45. if (tree->lnext != NULL)
  46. q.enqueue(tree->lnext);
  47. if (tree->rnext != NULL)
  48. q.enqueue(tree->rnext);
  49. }
  50. }
  51. template<class T>
  52. void findPredecessor(node2<T>* tree, T element, node2<T>* & res)
  53. {
  54. static bool found = false;
  55. if((tree != NULL) && (tree->info != element))
  56. {
  57. if(!found)
  58. {
  59. if(tree->lnext != NULL)
  60. if(tree->lnext->info == element)
  61. {
  62. res = tree;
  63. found = true;
  64. }
  65. else
  66. findPredecessor(tree->lnext, element, res);
  67. }
  68. if(!found)
  69. {
  70. if(tree->rnext != NULL)
  71. if(tree->rnext->info == element)
  72. {
  73. res = tree;
  74. found = true;
  75. }
  76. else
  77. findPredecessor(tree->rnext, element, res);
  78. }
  79. }
  80. if(!found)
  81. res = NULL;
  82. }
  83.  
  84. void main()
  85. {
  86. node2<int>* tree = NULL;
  87. int inf = 0;
  88. cout << "Enter the value for the root (-1 for empty tree): ";
  89. cin >> inf;
  90. if (inf != -1)
  91. tree = new node2<int>(inf);
  92. else
  93. tree = NULL;
  94. if (tree == NULL)
  95. cout << "The tree is empty." << endl;
  96. else
  97. {
  98. enterSubNodes(tree);
  99.  
  100. cout << endl;
  101. printTheTree(tree);
  102. }
  103. cout << "Preorder: ";
  104. preorder(tree);
  105. cout << endl;
  106. cout << "Inorder: ";
  107. inorder(tree);
  108. cout << endl;
  109. cout << "Postorder: ";
  110. postorder(tree);
  111. cout << endl;
  112. cout << "Level order: ";
  113. levelorder(tree);
  114. cout << endl;
  115. node2<int> *nod = NULL;
  116. if(tree != NULL)
  117. {
  118. cout << "Enter the element whose predecessor has to be found: ";
  119. cin >> inf;
  120. findPredecessor(tree, inf, nod);
  121. if(nod == NULL)
  122. //cout << "The predecessor of the element " << inf << " was not found in the tree, or the element is the root of the tree." << endl;
  123. cout << "NULL";
  124. cout << "";
  125. else
  126. cout << "The predecessor of the element " << inf << " is the element " << nod->info << " located on address " << nod << endl;
  127. }
  128.  
  129. system("PAUSE");
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement