DilyaraL

дерево

May 20th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<fstream>
  4. using namespace std;
  5. ////////////////////////
  6. struct treeNode
  7. {
  8. treeNode* left;
  9. treeNode* right;
  10. int data;
  11. };
  12. ///////////////////////
  13. struct stackNode
  14. {
  15. stackNode* next;
  16. treeNode* node;
  17. };
  18. //////////////////////
  19. void push_tree(treeNode* &tree_top, int x)
  20. {
  21. treeNode *p, *m;
  22. bool b;
  23. if (tree_top == NULL)
  24. {
  25. m = new treeNode;
  26. m->data = x;
  27. m->left = NULL;
  28. m->right = NULL;
  29. tree_top = m;//root
  30. }
  31. else
  32. {
  33. m = tree_top;
  34. b = true;
  35. while (b)
  36. {
  37. if (x >= m->data)
  38. {
  39. if (m->right)
  40. m = m->right;
  41. else
  42. {
  43. p = new treeNode;
  44. p->data = x;
  45. p->left = NULL;
  46. p->right = NULL;
  47. m->right = p;
  48. b = false;
  49. }
  50. }
  51. else
  52. {
  53. if (m->left) m = m->left;
  54. else
  55. {
  56. p = new treeNode;
  57. p->data = x;
  58. p->left = NULL;
  59. p->right = NULL;
  60. m->left = p;
  61. b = false;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. }
  68. /////////////////////////////////////////////////
  69. void push_stack(stackNode* &stack_top, treeNode* t)
  70. {
  71. stackNode* p;
  72. p = new stackNode;
  73. p->node = t;
  74. p->next = stack_top;
  75. stack_top = p;
  76. }
  77. //////////////////////////////////////////////////////////////
  78. void create_tree(ifstream &f, treeNode* &tree_top)
  79. {
  80. tree_top = NULL;
  81. int x;
  82. while (f >> x)
  83. {
  84. push_tree(tree_top, x);
  85. }
  86. }
  87. /////////////////////////
  88. void pop_stack(stackNode* &stack_top, treeNode* &t)
  89. //get node of tree from stack
  90. {
  91. stackNode *p;
  92. if (stack_top)
  93. {
  94. t = stack_top->node;
  95. p = stack_top->next;
  96. delete stack_top;
  97. stack_top = p;
  98. }
  99. }
  100. /////////////////////////////////////////////////////////////
  101. void print_node(int x)
  102. {
  103. cout << " " << x;
  104. }
  105. ////////////////////////////
  106. void inorder_tree(treeNode* tree_top)//печать обход в глубину (по порядку) с левым уклоном
  107. {
  108. stackNode* stack_top;
  109. treeNode* tree_p;
  110. tree_p = tree_top;
  111. stack_top = NULL;
  112. do
  113. {
  114. while (tree_p)
  115. {
  116. while (tree_p->left)
  117. {
  118. push_stack(stack_top, tree_p);
  119. tree_p = tree_p->left;
  120. }
  121. print_node(tree_p->data);
  122. tree_p = tree_p->right;
  123. }
  124.  
  125. if (stack_top)
  126. {
  127. pop_stack(stack_top, tree_p);
  128. print_node(tree_p->data);
  129. tree_p = tree_p->right;
  130. }
  131. } while (stack_top || tree_p);
  132. cout << endl;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment