Guest User

Untitled

a guest
Jul 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. /* Binary Search Tree*/
  2. /* Rules:
  3. 1. Values from the left node are smaller than the node's own value
  4. 2. Values from the right node are larger than the node's own value
  5. // Example
  6. // [] Node
  7. // () leaf
  8. [7]
  9.  / \
  10. [4] [9]
  11. / \ / \
  12. [3] [5] () [10]
  13. / \ / \ / \
  14. () () () () () ()
  15. */
  16. module type Ord = {
  17. type t;
  18. let eq: (t, t) => bool;
  19. let lt: (t, t) => bool;
  20. let leq: (t, t) => bool;
  21. };
  22.  
  23. module Integer: Ord with type t = int = {
  24. type t = int;
  25. let eq = (===);
  26. let lt = (<);
  27. let leq = (<=);
  28. };
  29.  
  30. module type Print = {type t; let toString: t => string;};
  31.  
  32. module PrintInteger: Print with type t = Integer.t = {
  33. type t = int;
  34. let toString = a => string_of_int(a);
  35. };
  36.  
  37. module type BinarySearchTree = {
  38. type t;
  39. type tree('t) =
  40. | Empty
  41. | Node(tree(t), t, tree(t)); /* Node(left Node, value, right Node)*/
  42. let contains: (tree(t), t) => bool;
  43. let insert: (tree(t), t) => tree(t);
  44. let remove: (tree(t), t) => tree(t);
  45. let minNode: tree(t) => tree(t);
  46. let print: tree(t) => string;
  47. };
  48.  
  49. type create = string => string;
  50.  
  51. module MakeBinarySearchTree =
  52. (Elem: Ord, Print: Print with type t = Elem.t)
  53. : (BinarySearchTree with type t := Elem.t) => {
  54. type t = Elem.t;
  55. type tree('t) =
  56. | Empty
  57. | Node(tree(t), t, tree(t));
  58. let rec insert = (node, value) =>
  59. switch (node) {
  60. | Empty => Node(Empty, value, Empty)
  61. | Node(leftNode, currentValue, rightNode) =>
  62. if (Elem.lt(currentValue, value)) {
  63. /*Right side*/
  64. Node(leftNode, currentValue, insert(rightNode, value));
  65. } else {
  66. /*Left side*/
  67. Node(insert(leftNode, value), currentValue, rightNode);
  68. }
  69. };
  70. let rec contains = (node, value) =>
  71. switch (node) {
  72. | Empty => false
  73. | Node(leftNode, currentValue, rightNode) =>
  74. if (Elem.lt(value, currentValue)) {
  75. /* Check left side*/
  76. contains(leftNode, value);
  77. } else if (Elem.lt(currentValue, value)) {
  78. /* Check right side*/
  79. contains(rightNode, value);
  80. } else {
  81. /* value is found */
  82. Elem.eq(value, currentValue);
  83. }
  84. };
  85. let rec minNode = node =>
  86. switch (node) {
  87. | Empty => Empty
  88. | Node(leftNode, _, _) =>
  89. /* keep searching of a left node*/
  90. if (leftNode == Empty) {
  91. node;
  92. } else {
  93. minNode(leftNode);
  94. }
  95. };
  96. let rec remove = (node, value) =>
  97. switch (node) {
  98. /* Do nothing */
  99. | Empty => Empty
  100. /* Only a value no children nodes */
  101. | Node(Empty, currentValue, Empty) =>
  102. if (Elem.eq(value, currentValue)) {
  103. Empty;
  104. } else {
  105. node;
  106. }
  107. /* Only left node */
  108. | Node(leftNode, currentValue, Empty) =>
  109. if (Elem.eq(value, currentValue)) {
  110. leftNode;
  111. } else {
  112. Node(remove(leftNode, value), currentValue, Empty);
  113. }
  114. /* Only right node */
  115. | Node(Empty, currentValue, rightNode) =>
  116. if (Elem.eq(value, currentValue)) {
  117. rightNode;
  118. } else {
  119. Node(Empty, currentValue, remove(rightNode, value));
  120. }
  121. /* Left and right node exist */
  122. | Node(leftNode, currentValue, rightNode) =>
  123. if (Elem.lt(value, currentValue)) {
  124. Node(remove(leftNode, value), currentValue, rightNode);
  125. } else if (Elem.lt(currentValue, value)) {
  126. Node(leftNode, currentValue, remove(rightNode, value));
  127. } else {
  128. switch (minNode(rightNode)) {
  129. | Empty => node
  130. | Node(_, minNodValue, _) =>
  131. Node(leftNode, minNodValue, remove(rightNode, minNodValue))
  132. };
  133. }
  134. };
  135. let rec printer = (node, level) =>
  136. switch (node) {
  137. | Empty => "empty"
  138. | Node(leftNode, currentValue, rightNode) =>
  139. " [ "
  140. ++ Print.toString(currentValue)
  141. ++ ", "
  142. ++ printer(leftNode, level + 1)
  143. ++ ", "
  144. ++ printer(rightNode, level + 1)
  145. ++ " ]"
  146. };
  147. let print = node => printer(node, 1);
  148. };
  149.  
  150. let run = () => {
  151. module Bst = MakeBinarySearchTree(Integer, PrintInteger);
  152. let t = Bst.insert(Empty, 6);
  153. let t = Bst.insert(t, 4);
  154. let t = Bst.insert(t, 10);
  155. let t = Bst.insert(t, 5);
  156. let t = Bst.insert(t, 7);
  157. Js.log(Bst.print(t));
  158. Js.log("contains 7?");
  159. Js.log(Bst.contains(t, 7));
  160. Js.log("contains 17?");
  161. Js.log(Bst.contains(t, 17));
  162. Js.log("contains 6?");
  163. Js.log(Bst.contains(t, 6));
  164. Js.log("contains 12?");
  165. Js.log(Bst.contains(t, 12));
  166. Js.log("contains 10?");
  167. Js.log(Bst.contains(t, 10));
  168. Js.log("remove 7");
  169. let t = Bst.remove(t, 7);
  170. Js.log("contains 7?");
  171. Js.log(Bst.contains(t, 7));
  172. Js.log(Bst.print(t));
  173. };
  174. run();
Add Comment
Please, Sign In to add comment