Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int op_select = 0;
  5. struct NodeT
  6. {
  7. int data;
  8. NodeT* left;
  9. NodeT* right;
  10. int size;
  11.  
  12. };
  13.  
  14. int size(NodeT* p)
  15. {
  16. if (p == NULL)
  17. return 0;
  18. else
  19. return(size(p->left) + 1 + size(p->right));
  20. }
  21. NodeT* newNode(int data)
  22. {
  23. NodeT* p= new NodeT();
  24. p->data = data;
  25. p->left = NULL;
  26. p->right = NULL;
  27. return p;
  28. }
  29. NodeT* buildPBT(int a[], int start, int end)
  30. {
  31. if (end >= start)
  32. {
  33. int mid = (start + end + 1) / 2;
  34. NodeT* root = newNode(a[mid]);
  35. root->left = buildPBT(a, start, mid - 1);
  36. root->right = buildPBT(a, mid + 1, end);
  37. return root;
  38. }
  39. return NULL;
  40. }
  41.  
  42. void preOrder(NodeT* p)
  43. {
  44. if ( p== NULL)
  45. return;
  46. cout << p->data << " ";
  47. preOrder(p->left);
  48. preOrder(p->right);
  49. }
  50. void printInorder(NodeT* p)
  51. {
  52. if (p == NULL)
  53. return;
  54. printInorder(p->left);
  55. cout << p->data << " ";
  56. printInorder(p->right);
  57. }
  58. NodeT* OS_SELECT(NodeT* tree, int index)
  59. {
  60. int r = size(tree->left)+ 1;
  61. op_select++;
  62. if (index == r)
  63. {
  64. return tree;
  65. op_select++;
  66. }
  67. else if (index < r)
  68. {
  69. return OS_SELECT(tree->left, index);
  70. op_select++;
  71. }
  72. else
  73. {
  74. return OS_SELECT(tree->right, index);
  75. op_select++;
  76. }
  77. }
  78. NodeT* minValueNode(NodeT* node)
  79. {
  80. NodeT* current = node;
  81. /* loop down to find the leftmost leaf */
  82. while (current && current->left != NULL)
  83. current = current->left;
  84. return current;
  85. }
  86. NodeT* OS_DELETE(NodeT* tree, int index)
  87. {
  88. NodeT* p = OS_SELECT(tree, index);
  89. if (tree == NULL) return tree;
  90. // If the key to be deleted is smaller than the root's key,
  91. // then it lies in left subtree
  92. if (p->data < tree->data)
  93. p->left = OS_DELETE(p->left, index);
  94. // If the key to be deleted is greater than the root's key,
  95. // then it lies in right subtree
  96. else if (p->data > tree->data)
  97. p->right = OS_DELETE(p->right, index);
  98. // if key is same as root's key, then This is the node
  99. // to be deleted
  100. else
  101. {
  102. // node with only one child or no child
  103. if (p->left == NULL)
  104. {
  105. NodeT* temp = p->right;
  106. free(p);
  107. return temp;
  108. }
  109. else if (p->right == NULL)
  110. {
  111. NodeT* temp = p->left;
  112. free(p);
  113. return temp;
  114. }
  115. // node with two children: Get the inorder successor (smallest
  116. // in the right subtree)
  117. NodeT* temp = minValueNode(p->right);
  118. // Copy the inorder successor's content to this node
  119. p->data = temp->data;
  120. // Delete the inorder successor
  121. p->right = OS_DELETE(p->right, temp->data);
  122. }
  123. return p;
  124. }
  125. void demoBuildTree()
  126. {
  127. int a[11] = { 1,2,4,6,7,9,11,15,17,18,23 };
  128. cout << "Convert List to PBT" << "\n";
  129. NodeT* root = buildPBT(a, 0, 10);
  130. printInorder(root);
  131. cout << "\n";
  132. }
  133. void demoOS_Select()
  134. {
  135. int a[11] = { 1,2,4,6,7,9,11,15,17,18,23 };
  136. NodeT* root = buildPBT(a, 0, 10);
  137. printInorder(root);
  138. cout << OS_SELECT(root, 9)->data;
  139. }
  140. int main()
  141. {
  142. demoBuildTree();
  143. // demoOS_Select();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement