Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace BinarySearchTreeSuccessor_mockingInterview
  9. {
  10. /// <summary>
  11. /// binary search tree successor
  12. /// June 28, 2017
  13. /// </summary>
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. var root = new Node(30);
  19.  
  20. root.Left = new Node(19);
  21. root.Left.Parent = root;
  22.  
  23. root.Right = new Node(30);
  24. root.Right.Parent = root;
  25.  
  26. root.Left.Left = new Node(15);
  27. root.Left.Left.Parent = root.Left;
  28.  
  29. root.Left.Right = new Node(22);
  30. root.Left.Right.Parent = root.Left;
  31.  
  32. var node22 = root.Left.Right;
  33. node22.Left = new Node(21);
  34. node22.Left.Parent = node22;
  35.  
  36. node22.Right = new Node(24);
  37. node22.Right.Parent = node22;
  38.  
  39. var testcase1 = FindInOrderSuccessor(root.Left.Left);
  40. Debug.Assert(testcase1.Value == 19);
  41.  
  42. var testcase2 = FindInOrderSuccessor(root.Left);
  43. Debug.Assert(testcase2.Value == 21);
  44.  
  45. var testcase3 = FindInOrderSuccessor(node22.Right);
  46. Debug.Assert(testcase3.Value == 30);
  47. }
  48.  
  49. class Node
  50. {
  51. public int Value { get; set; }
  52. public Node Left { get; set; }
  53. public Node Right { get; set; }
  54. public Node Parent { get; set; }
  55.  
  56. public Node(int v)
  57. {
  58. Value = v;
  59. }
  60. }
  61.  
  62. // given 15, find 19
  63. // test case 2: given node 24
  64. // test case 3: given node 35
  65. // test case 4: given node 19
  66. // test case 5: given node 30
  67. // test case 6: given node 21
  68. // time complexity - BST O(logN) - O(N) - space - linked list O(N)
  69. static Node FindInOrderSuccessor(Node inputNode)
  70. {
  71. // your code goes here
  72. if (inputNode == null)
  73. {
  74. return null;
  75. }
  76.  
  77. // assume that node at least has one child
  78. bool hasRightChild = inputNode.Right != null; // false, false
  79.  
  80. if (hasRightChild)
  81. {
  82. return getMinimum(inputNode.Right); // ? node 22 , return node 21
  83. }
  84.  
  85. // now we can assume that the node does not have right child
  86. // find parent node which is bigger than givenNode
  87. var givenValue = inputNode.Value; // 15, 24, 35
  88. var iterate = inputNode;
  89. while (iterate.Parent != null && iterate.Parent.Value < givenValue) // 19 > 15, 22 < 24, 30 > 24
  90. {
  91. iterate = iterate.Parent; // node 22, node 19
  92. }
  93.  
  94. return iterate.Parent;
  95. }
  96.  
  97. /// <summary>
  98. ///
  99. /// </summary>
  100. /// <param name="root"></param>
  101. /// <returns></returns>
  102. private static Node getMinimum(Node root)
  103. {
  104. var minimum = root; // node 22
  105. while (minimum.Left != null)
  106. {
  107. minimum = minimum.Left; // node 21
  108. }
  109.  
  110. return minimum; // return node 21
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement