Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct Node
  5. {
  6. int data;
  7. Node* left;
  8. Node* right;
  9.  
  10. };
  11. using namespace std;
  12. class Tree
  13. {
  14. private:
  15. Node *head =new Node;
  16. Node *rood;
  17. int n = 0;
  18.  
  19. public:
  20.  
  21. void add()
  22. {
  23.  
  24. cout<<"введите корень(0)"<<endl;
  25. cin>>(head->data);
  26. head->left = NULL;
  27. head->right = NULL;
  28. cout<<"вводите вершины('0' означает NULL)"<<endl;
  29. add(head);
  30. }
  31. void add(Node *uz)
  32. {
  33. int gl,gr;
  34. Node* tmpl = new Node;
  35. cin>>gl;
  36.  
  37. if ((gl)!=0)
  38. {
  39. tmpl->data = gl;
  40. tmpl->left = NULL;
  41. tmpl->right = NULL;
  42. uz->left = tmpl;
  43. n++;
  44.  
  45. add(tmpl);
  46. }
  47. else{
  48. tmpl = NULL;
  49. uz->left = tmpl;
  50. }
  51.  
  52. Node* tmpr = new Node;
  53. cin>>gr;
  54.  
  55. if (gr!=0)
  56. {
  57. tmpr->data = gr;
  58. tmpr->left = NULL;
  59. tmpr->right = NULL;
  60. uz->right = tmpr;
  61. add(tmpr);
  62. }
  63. else{
  64. tmpr= NULL;
  65. uz->right = tmpr;
  66. }
  67. }
  68.  
  69.  
  70.  
  71. void print()
  72. {
  73. print(head,5);
  74. }
  75. void print(Node *p,int level)
  76. {
  77. if(p)
  78. {
  79. print(p->right,level + 1);
  80. for(int i = 0;i< level;i++) cout<<" ";
  81. cout << p->data << endl;
  82. print(p->left,level + 1);
  83. }
  84.  
  85. }
  86. };
  87.  
  88.  
  89. int main()
  90. {
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement