Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. // ConsoleApplication2.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include <algorithm>
  7. #include <list>
  8. #include <vector>
  9. using namespace std;
  10. struct Node{
  11. int value;
  12.  
  13. Node* left;
  14. Node* right;
  15.  
  16. Node(int num):value(num), left(NULL), right(NULL) {}
  17. };
  18. Node* Add(int num, Node* root){
  19. if(root == nullptr){
  20. root = new Node(num);
  21. }
  22. if(num < root->value){
  23. root->left = Add(num, root->left);
  24. }
  25. if(num > root->value) {
  26. root->right = Add(num, root->right);
  27. }
  28. return root;
  29. }
  30.  
  31. Node* search(int num, Node* root){
  32. if(num == root->value){
  33. return root;
  34. }
  35. if(num < root->value){
  36. return search(num, root->left);
  37. }
  38. if(num > root->value) {
  39. return search(num, root->right);
  40. }
  41. return nullptr;
  42. }
  43. int height(Node *root)
  44. {
  45. Node *tmp = root;
  46. int h1=0, h2=0;
  47. if(root==nullptr)
  48. return 0;
  49. if(root->left)
  50. h1=height(root->left);
  51. if(root->right)
  52. h2=height(root->right);
  53. return (max(h1,h2)+1);
  54. }
  55. void straight(Node *root)
  56. {
  57. if(root==nullptr) return;
  58.  
  59. cout <<root->value<< endl;
  60. straight(root->left);
  61. straight(root->right);
  62. }
  63. void por(vector <int> &TAGIR, int size)
  64. {
  65. for (int i=0;i<size;i++)
  66. {
  67. for (int j=1;j<(size);j++)
  68. {
  69. if (TAGIR[j]<TAGIR[j-1])
  70. {
  71. int b=TAGIR[j];
  72. TAGIR[j]=TAGIR[j-1];
  73.  
  74. TAGIR[j-1]=b;
  75. }
  76. }
  77. }
  78. for (int i=0;i<size;i++)
  79. {
  80. //if (TAGIR[i]==0) {continue;}
  81. cout <<TAGIR[i]<<endl;
  82. }
  83. return ;
  84.  
  85.  
  86. }
  87. int main(){
  88. Node* root = NULL;
  89. int n;
  90. int i=0;
  91. int size;
  92. cin >> size;
  93. cout<<endl;
  94. vector<int>TAGIR(500);
  95. while(true){
  96. cin >>n;
  97. if (n!=0){
  98. root = Add(n,root);
  99. TAGIR[i]=n;
  100. i++;
  101. }
  102. else break;
  103. }
  104. int h=height(root);
  105. cout << endl;
  106. cout << h << endl;
  107. cout << endl;
  108. straight(root);
  109. cout << endl;
  110. por(TAGIR,size);
  111. system("pause");
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement