Guest User

Untitled

a guest
Jul 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <boost/range/irange.hpp>
  4. #include <boost/variant.hpp>
  5. #include <random>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. using boost::irange;
  10. using boost::variant;
  11.  
  12. struct Node
  13. {
  14. Node(Node* l, Node* r, int v):left(l), right(r), val(v)
  15. {
  16.  
  17. }
  18. Node* left;
  19. Node* right;
  20. int val;
  21. };
  22.  
  23. typedef variant<Node*, int> stkElemT;
  24. typedef stack<stkElemT> bstStkT;
  25.  
  26. //from variant extract the pointer if valid otherwise NULL
  27. struct stkElemVisitorNode : public boost::static_visitor<Node*>
  28. {
  29. Node* operator()(const int& val) const { return NULL; }
  30. Node* operator()(Node*& ptr) const { return ptr; }
  31. };
  32.  
  33. //from variant extract the integer value if valid otherwise -1
  34. struct stkElemVisitorInt : public boost::static_visitor<int>
  35. {
  36. int operator()(const int& val) const { return val; }
  37. int operator()(Node*& ptr) const { return -1; }
  38. };
  39.  
  40. //expand left most path of top node.
  41. void fillPathStkRecurse(bstStkT& bstStk)
  42. {
  43. stkElemT topE = bstStk.top();
  44.  
  45. Node* topN = boost::apply_visitor(stkElemVisitorNode(), topE);
  46. if(topN != NULL) //
  47. {
  48. bstStk.pop();
  49. if (topN->right)
  50. bstStk.push(topN->right);
  51. bstStk.push(topN->val);
  52. if (topN->left)
  53. {
  54. bstStk.push(topN->left);
  55. }
  56. fillPathStkRecurse(bstStk);
  57. }
  58. else{
  59. return; //top node is not a pointer but value
  60. }
  61. }
  62.  
  63. int getTopVal(const bstStkT& bstStk)
  64. {
  65. assert(!bstStk.empty());
  66. stkElemT topE = bstStk.top();
  67. int val = boost::apply_visitor(stkElemVisitorInt(), topE);
  68. return val;
  69. }
  70.  
  71. void incrBstStk(bstStkT& bstStk)
  72. {
  73. if(bstStk.empty()) return;
  74. int topVal = getTopVal(bstStk);
  75. assert(topVal != -1);
  76. bstStk.pop();
  77. if(!bstStk.empty())
  78. fillPathStkRecurse(bstStk); //expand till child node
  79. return;
  80. }
  81.  
  82.  
  83.  
  84. Node* create_tree(vector<int>& vals, int start, int end) //end excluded
  85. {
  86. if(end==start)
  87. return new Node(NULL, NULL, vals[start]);
  88.  
  89.  
  90. if(end == start + 1)
  91. Node* curr = new Node(NULL, NULL, vals[start]);
  92. curr->right = new Node(NULL, NULL, vals[start+1]);
  93. return curr;
  94.  
  95. int mid = floor((start + end)/2.0);
  96.  
  97. Node* left = create_tree(vals, start, mid-1);
  98. Node* right = create_tree(vals, mid+1, end);
  99. Node* curr = new Node(left, right, vals[mid]);
  100. return curr;
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. vector<int> merge_bst(Node* root1, Node* root2)
  108. {
  109. vector<int> res;
  110. bstStkT bstStk1;
  111. bstStk1.push(root1);
  112. fillPathStkRecurse(bstStk1);
  113.  
  114.  
  115. bstStkT bstStk2;
  116. bstStk2.push(root2);
  117. fillPathStkRecurse(bstStk2);
  118.  
  119. while(1)
  120. {
  121. //cout<<"stk sizes = "<<bstStk1.size()<<" "<<bstStk2.size()<<endl;
  122. if(bstStk1.empty() && bstStk2.empty())
  123. break;
  124.  
  125. int val1 = numeric_limits<int>::max();
  126. if(!bstStk1.empty())
  127. val1 = getTopVal(bstStk1);
  128.  
  129. int val2 = numeric_limits<int>::max();
  130. if(!bstStk2.empty())
  131. val2 = getTopVal(bstStk2);
  132.  
  133. if(val1 < val2)//consume bstStk1
  134. {
  135. res.push_back(val1);
  136. incrBstStk(bstStk1);
  137. }
  138. else
  139. {
  140. res.push_back(val2);
  141. incrBstStk(bstStk2);
  142. }
  143. }
  144. return res;
  145. }
  146.  
  147.  
  148. int main(int argc, char** argv)
  149. {
  150. std::mt19937 rng;
  151. rng.seed(std::random_device()());
  152. std::uniform_int_distribution<std::mt19937::result_type> uid5k(0, 1000); // distribution in range [1, 6]
  153.  
  154.  
  155. int n = 10000;
  156. for(auto k: irange(0, 10000))
  157. {
  158. vector<int> inVec1;
  159. for(auto i: irange(0, n))
  160. inVec1.push_back(uid5k(rng));
  161.  
  162. sort(inVec1.begin(), inVec1.end());
  163. Node* root1 = create_tree(inVec1, 0, n-1);
  164.  
  165. vector<int> inVec2;
  166. for(auto i: irange(0, n))
  167. inVec2.push_back(uid5k(rng));
  168. sort(inVec2.begin(), inVec2.end());
  169. Node* root2 = create_tree(inVec2, 0, n-1);
  170.  
  171. vector<int> merged_vec(inVec1.begin(), inVec1.end());
  172. merged_vec.insert(end(merged_vec), begin(inVec2), end(inVec2));
  173. sort(begin(merged_vec), end(merged_vec));
  174.  
  175. auto res = merge_bst(root1, root2);
  176. assert(res == merged_vec);
  177. }
  178.  
  179.  
  180. return 0;
  181. }
Add Comment
Please, Sign In to add comment