Advertisement
wurdalak007

Untitled

Nov 7th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <ctime>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. template<class T>
  9. int depth_of_tree( T* root ){
  10. if( root == 0 )
  11. return 0;
  12.  
  13. int left = 0;
  14. int right = 0;
  15.  
  16. if( root->left != nullptr ) {
  17. left = depth_of_tree(root->left);
  18. } else {
  19. left = -1;
  20. }
  21. if( root->right != nullptr ) {
  22. right = depth_of_tree(root->right);
  23. } else {
  24. right = -1;
  25. }
  26.  
  27. int max = left > right ? left : right;
  28.  
  29. return max + 1;
  30.  
  31. }
  32.  
  33. struct DecartTree{
  34. int key;
  35. int prior;
  36. DecartTree* left;
  37. DecartTree* right;
  38.  
  39. DecartTree():
  40. key(0),
  41. prior(0),
  42. left( nullptr ),
  43. right( nullptr )
  44. {}
  45.  
  46. };
  47.  
  48. struct NativeTree{
  49. int key;
  50. NativeTree* left;
  51. NativeTree* right;
  52.  
  53. void add( int data );
  54.  
  55. NativeTree():
  56. key(0),
  57. left( nullptr ),
  58. right( nullptr )
  59. {}
  60. };
  61.  
  62. template <class T>
  63. void BuildTree( T* root, int K ) {
  64.  
  65. if( root->key <= K ) {
  66. if(root->right == 0) {
  67. root->right = new T;
  68. root->right->key = K;
  69. } else {
  70. BuildTree( root->right, K );
  71. }
  72. } else {
  73. if( root->left == 0 ) {
  74. root->left = new T;
  75. root->left->key = K;
  76. } else {
  77. BuildTree( root->left, K );
  78. }
  79. }
  80. }
  81.  
  82. template <class T>
  83. void deletetree( T* root ) {
  84. if( root == nullptr )
  85. return;
  86. deletetree( root->left );
  87. deletetree( root->right );
  88. delete root;
  89. };
  90.  
  91. void split( DecartTree* root, DecartTree*& left, DecartTree*& right, int key){
  92.  
  93. if( root == nullptr ) {
  94. left = nullptr;
  95. right = nullptr;
  96. } else {
  97. if (root->key < key) {
  98. split(root->right, root->right, right, key);
  99. left = root;
  100. } else {
  101. split(root->left, left, root->left, key);
  102. right = root;
  103. }
  104. }
  105. }
  106.  
  107. void insert(DecartTree*& root, DecartTree* current)
  108. {
  109. if (root == nullptr)
  110. {
  111. root = current;
  112. return;
  113. }
  114.  
  115. if (root->prior > current->prior)
  116. {
  117. if (current->key < root->key) {
  118. insert(root->left, current);
  119. return;
  120. } else {
  121. insert(root->right, current);
  122. return;
  123. }
  124. }
  125.  
  126. split(root, current->left, current->right, current->key);
  127. root = current;
  128. }
  129.  
  130.  
  131. int main() {
  132.  
  133. int n = 0;
  134. int xkey = 0;
  135. int prior = 0;
  136. DecartTree* root = new DecartTree;
  137. NativeTree* tree = new NativeTree;
  138. DecartTree* current = new DecartTree;
  139. cin >> n;
  140.  
  141. cin >> xkey >> prior;
  142. tree->key = prior;
  143. root->key = xkey;
  144. root->prior = prior;
  145.  
  146. for (int i = 1; i < n; i++){
  147. cin >> xkey >> prior;
  148. current->key = xkey;
  149. current->prior = prior;
  150.  
  151. insert(root, current);
  152. BuildTree(tree, prior);
  153.  
  154. }
  155.  
  156. cout << depth_of_tree(tree) <<" "<< depth_of_tree(root);
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement