Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<queue>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. class Node {
  8. public:
  9. int data;
  10. Node * left;
  11. Node * right;
  12. Node * parent;
  13. bool visited;
  14. Node();
  15. ~Node();
  16. };
  17.  
  18. Node::Node() {
  19. Node * n;
  20. n = new Node;
  21. n->data = 0;
  22. n->left = NULL;
  23. n->right = NULL;
  24. bool visited = false;
  25. }
  26.  
  27. Node :: ~Node() {
  28. left = NULL;
  29. right = NULL;
  30. parent = NULL;
  31. }
  32.  
  33. class Tree {
  34. public:
  35. Tree();
  36. ~Tree();
  37. int temp;
  38. void Aad(Node * root, Node * n);
  39. int Maximum(Node * root);
  40. void height(Node * root);
  41. void distruct(Node * root);
  42. void Print(Node * root, Node * P);
  43. void Traverse(Node * root);
  44. };
  45.  
  46. Tree::Tree(){
  47. Node * root;
  48.  
  49. }
  50.  
  51. Tree :: ~Tree() {
  52. }
  53.  
  54. void Tree::Aad(Node *root, Node *n) {
  55. if (n->data > root->data) {
  56. if (root->right == NULL)
  57. root->right = n;
  58. else
  59. Aad(root->right, n);
  60. }
  61. else {
  62. if (root->left == NULL)
  63. root->left = n;
  64. else
  65. Aad(root->left, n);
  66. }
  67. }
  68.  
  69. void Tree::height(Node * root) {
  70. int count = 0;
  71. int count1 = 0;
  72. Node *P = root;
  73. Node *P1 = root->right;
  74. while (P1->right != NULL) {
  75. count++;
  76. P = P1;
  77. P1 = P1->right;
  78. }
  79. P = root;
  80. P1 = root->left;
  81. while (P1->left != NULL) {
  82. count++;
  83. P = P1;
  84. P1 = P1->left;
  85. }
  86. if (count > count1)
  87. cout << count << endl;
  88. else
  89. cout << count1 << endl;
  90. }
  91.  
  92. int Tree::Maximum(Node *root) {
  93. if (root->right == NULL)
  94. return (root->data);
  95. else
  96. Maximum(root->right);
  97. }
  98.  
  99. void Tree::distruct(Node * root) {
  100. if (root->right == NULL)
  101. return;
  102. else {
  103. distruct(root->right);
  104. delete root->right;
  105. }
  106. if (root->left == NULL)
  107. return;
  108. else {
  109. distruct(root->left);
  110. delete root->left;
  111. }
  112. }
  113.  
  114. void Tree::Print(Node *root, Node *P) {
  115. if (P->data > root->data) {
  116. Print(root->right, P);
  117. cout << root->data << endl;
  118. }
  119. else if (P->data < root->data) {
  120. Print(root->left, P);
  121. cout << root->data;
  122.  
  123. }
  124. else
  125. cout << root->data << endl;
  126. }
  127.  
  128. void Tree::Traverse(Node * root) {
  129. if (root == NULL)
  130. cout << "The Tree is empty" << endl;
  131. queue<Node *> Q;
  132. Q.push(root);
  133. while (Q.empty() != true) {
  134. cout << Q.front();
  135. if (Q.front()->left != NULL)
  136. Q.push(Q.front()->left);
  137. if (Q.front()->right != NULL)
  138. Q.push(Q.front()->right);
  139. Q.front()->visited = true;
  140. Q.pop();
  141. }
  142. }
  143.  
  144.  
  145.  
  146. int main()
  147. {
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement