Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. class Node {
  12. constructor(data) {
  13. this.data = data;
  14. this.left = null;
  15. this.right = null;
  16. }
  17. }
  18.  
  19. class BST {
  20. constructor() {
  21. this.root = null;
  22. }
  23.  
  24. insert(data) {
  25. const newNode = new Node(data);
  26. if(this.root === null) {
  27. this.root = newNode;
  28. } else {
  29. this._insertNode(this.root, newNode);
  30. }
  31. }
  32.  
  33. _insertNode(node, newNode) {
  34. // console.log('node', node);
  35. if(node.data > newNode.data) {
  36. if(node.left !== null) {
  37. return this._insertNode(node.left, newNode);
  38. } else {
  39. node.left = newNode
  40. }
  41. } else {
  42. if (node.right !== null) {
  43. return this._insertNode(node.right, newNode)
  44. } else {
  45. node.right = newNode
  46. }
  47. }
  48. }
  49.  
  50. search(node, data) {
  51. if(node === null) {
  52. return 0;
  53. }
  54. if (node.data === data) {
  55. return 1;
  56. }
  57. if(node.data > data) {
  58. return this.search(node.left, data);
  59. } else {
  60. return this.search(node.right, data);
  61. }
  62. }
  63. }
  64.  
  65. function main(value){
  66. const bst1 = new BST();
  67. bst1.insert(1);
  68. bst1.insert(3);
  69. bst1.insert(2);
  70. const bst2 = new BST();
  71. bst2.insert(3);
  72. bst2.insert(2);
  73. bst2.insert(1);
  74. const bst3 = new BST();
  75. bst3.insert(3);
  76. bst3.insert(1);
  77. bst3.insert(2);
  78. const bst4 = new BST();
  79. bst4.insert(2);
  80. bst4.insert(1);
  81. bst4.insert(3);
  82. const bst5 = new BST();
  83. bst5.insert(1);
  84. bst5.insert(2);
  85. bst5.insert(3);
  86. const result = bst1.search(bst1.root, value) +
  87. bst2.search(bst2.root, value) +
  88. bst3.search(bst3.root, value) +
  89. bst4.search(bst4.root, value) +
  90. bst5.search(bst5.root, value);
  91. return result;
  92. }
  93.  
  94. console.log(main(3));
  95. </script>
  96.  
  97.  
  98.  
  99. <script id="jsbin-source-javascript" type="text/javascript">class Node {
  100. constructor(data) {
  101. this.data = data;
  102. this.left = null;
  103. this.right = null;
  104. }
  105. }
  106.  
  107. class BST {
  108. constructor() {
  109. this.root = null;
  110. }
  111.  
  112. insert(data) {
  113. const newNode = new Node(data);
  114. if(this.root === null) {
  115. this.root = newNode;
  116. } else {
  117. this._insertNode(this.root, newNode);
  118. }
  119. }
  120.  
  121. _insertNode(node, newNode) {
  122. // console.log('node', node);
  123. if(node.data > newNode.data) {
  124. if(node.left !== null) {
  125. return this._insertNode(node.left, newNode);
  126. } else {
  127. node.left = newNode
  128. }
  129. } else {
  130. if (node.right !== null) {
  131. return this._insertNode(node.right, newNode)
  132. } else {
  133. node.right = newNode
  134. }
  135. }
  136. }
  137.  
  138. search(node, data) {
  139. if(node === null) {
  140. return 0;
  141. }
  142. if (node.data === data) {
  143. return 1;
  144. }
  145. if(node.data > data) {
  146. return this.search(node.left, data);
  147. } else {
  148. return this.search(node.right, data);
  149. }
  150. }
  151. }
  152.  
  153. function main(value){
  154. const bst1 = new BST();
  155. bst1.insert(1);
  156. bst1.insert(3);
  157. bst1.insert(2);
  158. const bst2 = new BST();
  159. bst2.insert(3);
  160. bst2.insert(2);
  161. bst2.insert(1);
  162. const bst3 = new BST();
  163. bst3.insert(3);
  164. bst3.insert(1);
  165. bst3.insert(2);
  166. const bst4 = new BST();
  167. bst4.insert(2);
  168. bst4.insert(1);
  169. bst4.insert(3);
  170. const bst5 = new BST();
  171. bst5.insert(1);
  172. bst5.insert(2);
  173. bst5.insert(3);
  174. const result = bst1.search(bst1.root, value) +
  175. bst2.search(bst2.root, value) +
  176. bst3.search(bst3.root, value) +
  177. bst4.search(bst4.root, value) +
  178. bst5.search(bst5.root, value);
  179. return result;
  180. }
  181.  
  182. console.log(main(3));
  183. </script></body>
  184. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement