Guest User

Untitled

a guest
Jun 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. #include <vector>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <stdexcept>
  8.  
  9. struct Node {
  10. int val;
  11. Node * left;
  12. Node * right;
  13. Node(int x): val(x), left(NULL), right(NULL) {}
  14. };
  15.  
  16. /* For testing purposes */
  17. void preOrder(Node * root) {
  18. if (root == NULL) {
  19. return;
  20. }
  21. cout << root->val << " ";
  22. preOrder(root->left);
  23. preOrder(root->right);
  24. }
  25.  
  26. Node* insert(Node * root, int value) {
  27. // Assumption: If node values are equal, push it to the left side of the tree
  28. if (root == NULL) {
  29. return new Node(value);
  30. }
  31. if (root->val < value) {
  32. root->right = insert(root->right, value);
  33. } else {
  34. root->left = insert(root->left, value);
  35. }
  36. return root;
  37. }
  38.  
  39. Node* createBST(const vector < int > & values) {
  40. if (values.size() == 0) {
  41. return NULL;
  42. }
  43. Node * root = new Node(values[0]);
  44. //Insert the rest of the values
  45. for (int i = 1; i < values.size(); ++i) {
  46. insert(root, values[i]);
  47. }
  48. return root;
  49. }
  50.  
  51. int findDistanceToValue(Node * root, int value) {
  52. if (root->val == value) {
  53. return 0;
  54. }
  55. if (root->val > value) {
  56. return 1 + findDistanceToValue(root->left, value);
  57. } else {
  58. return 1 + findDistanceToValue(root->right, value);
  59. }
  60.  
  61. }
  62.  
  63. int findDistanceBetweenTwoValues(Node * root, int key_1, int key_2) {
  64. if (key_1 > root->val && key_2 > root->val) {
  65. return findDistanceBetweenTwoValues(root->right, key_1, key_2);
  66. }
  67. if (root->val > key_1 && root->val > key_2) {
  68. return findDistanceBetweenTwoValues(root->left, key_1, key_2);
  69. }
  70. //Else we have our least common ancestor at the root, so add up distance to each value
  71. return findDistanceToValue(root, key_1) + findDistanceToValue(root,
  72. key_2);
  73.  
  74. }
  75. //Notes:
  76. //I assumed "in order" in the problem spec to mean that we can't sort the array and that
  77. //we should try to create the BST in the order that the values were given
  78. //Thus, I set root as the first element in the array in createBST() and continue inserting elements.
  79. int main(int argc, char ** argv) {
  80.  
  81. //Process input
  82. vector<string>inputs;
  83. string line; //temporary buffer
  84. while (std::getline(std::cin, line)) {
  85. inputs.push_back(line);
  86. }
  87. if (inputs.size() != 3) {
  88. throw std::runtime_error("Input is not valid, must be 3 lines of integers \n");
  89. }
  90.  
  91. /* Store keys we are searching for */
  92. vector<int> keys_to_find;
  93. keys_to_find.push_back(stoi(inputs[0]));
  94. keys_to_find.push_back(stoi(inputs[1]));
  95. int found_entered_keys = 0;
  96.  
  97. vector<int> values;
  98.  
  99. stringstream ss(inputs[2]); // Insert the string of values for the tree into a stream
  100. line = "";
  101. while (ss >> line){
  102. int new_key = stoi(line);
  103. values.push_back(new_key);
  104. if(new_key == keys_to_find[0] || new_key == keys_to_find[1]) found_entered_keys += 1; //Ensure we find both keys we are searching for
  105. }
  106.  
  107. if(found_entered_keys < 2){
  108. // throw std::runtime_error("Entered Keys are not in BST \n");
  109. cout << "Not found!\n";
  110. return 0;
  111. }
  112.  
  113. Node * root = createBST(values);
  114. cout << "The distance between the two nodes: " << (findDistanceBetweenTwoValues(root, keys_to_find[0], keys_to_find[1])) << "\n";
  115. }
Add Comment
Please, Sign In to add comment