Guest User

Untitled

a guest
May 21st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. // noprotect
  2.  
  3. /*
  4. Tree constructor definition
  5. */
  6. let Node = function() {
  7. //key of the node
  8. this.key = null;
  9. //value of the node
  10. this.value = null;
  11. //the index of the parent node in the nodeCache
  12. this.parent = null;
  13. //the index of the left node in the nodeCache
  14. this.left = null;
  15. //the index of the right node in the nodeCache
  16. this.right = null;
  17. //its own index in the nodeCache
  18. this.index = null;
  19. };
  20.  
  21. Node.prototype.setLeftNode = function(node) {
  22. this.left = node.getIndex();
  23. };
  24.  
  25. Node.prototype.setRightNode = function(node) {
  26. this.right = node.getIndex();
  27. };
  28.  
  29. Node.prototype.getLeftNode = function(node) {
  30. return this.left;
  31. };
  32.  
  33. Node.prototype.getRightNode = function(node) {
  34. return this.right;
  35. };
  36.  
  37. Node.prototype.setValue = function(value) {
  38. this.value = value;
  39. };
  40.  
  41. Node.prototype.getValue = function() {
  42. return this.value;
  43. };
  44.  
  45. Node.prototype.setParentNode = function(node) {
  46. if (node) {
  47. this.parent = node.getIndex();
  48. }
  49. };
  50.  
  51. Node.prototype.getParentNode = function() {
  52. return this.parent;
  53. };
  54.  
  55. Node.prototype.setIndex = function(index) {
  56. this.index = index;
  57. };
  58.  
  59. Node.prototype.getIndex = function() {
  60. return this.index;
  61. };
  62.  
  63. Node.prototype.setKey = function(key) {
  64. this.key = key;
  65. };
  66.  
  67. Node.prototype.getKey = function() {
  68. return this.key;
  69. };
  70. /*
  71. Binary search tree data strucutre implementation
  72. */
  73. let BsTree = function(rootElm) {
  74. let nodeCache = [];
  75. let depth = 0;
  76. let self = this;
  77. let root = null;
  78. let init = function(rootElm) {
  79. root = new Node();
  80. root.setParentNode(null);
  81. root.setKey(rootElm.key);
  82. root.setValue(rootElm.value);
  83. nodeCache.push(root);
  84. root.setIndex(0);
  85. };
  86.  
  87. let _appendNode = function(node, nodeToAppend) {
  88. let value = node.getValue();
  89. let current = nodeToAppend.getValue();
  90. if (current >= value) {
  91. let right = node.getRightNode();
  92. if (right !== null) {
  93. _appendNode(nodeCache[right], nodeToAppend);
  94. } else {
  95. nodeToAppend.setParentNode(node);
  96. node.setRightNode(nodeToAppend);
  97. node.rightNode = nodeToAppend;
  98. //hack to get depth of the tree
  99. if (node.getLeftNode() === null) {
  100. depth++;
  101. }
  102. }
  103. } else if (current < value) {
  104. let left = node.getLeftNode();
  105. if (left !== null) {
  106. _appendNode(nodeCache[left], nodeToAppend);
  107. } else {
  108. nodeToAppend.setParentNode(node);
  109. node.setLeftNode(nodeToAppend);
  110. node.leftNode = nodeToAppend;
  111. //hack to get depth of the tree
  112. if (node.getRightNode() === null) {
  113. depth++;
  114. }
  115. }
  116. }
  117. };
  118.  
  119. let _getValue = function(node, givenValue) {
  120. if (node) {
  121. let key = node.getKey();
  122. let value = node.getValue();
  123. let leftNode = nodeCache[node.getLeftNode()];
  124. let rightNode = nodeCache[node.getRightNode()];
  125. if (givenValue === value) {
  126. return node;
  127. } else if (givenValue > value) {
  128. return _getValue(rightNode, givenValue);
  129. } else if (givenValue < value) {
  130. return _getValue(leftNode, givenValue);
  131. }
  132. } else {
  133. return null;
  134. }
  135. };
  136.  
  137. this.createNode = function(obj) {
  138. let node = new Node();
  139. node.setKey(obj.key);
  140. node.setValue(obj.value);
  141. return node;
  142. };
  143.  
  144. this.appendNode = function(data) {
  145. let node = this.createNode(data);
  146. node.setIndex(nodeCache.length);
  147. nodeCache.push(node);
  148. _appendNode(root, node);
  149. };
  150.  
  151. this.createTree = function(data) {
  152. for(let i = 1; i < data.length; i++) {
  153. let obj = data[i];
  154. let node = new Node();
  155. node.setKey(obj.key);
  156. node.setValue(obj.value);
  157. node.setIndex(nodeCache.length);
  158. nodeCache.push(node);
  159. _appendNode(root, node);
  160. };
  161. };
  162.  
  163. this.getNode = function(value) {
  164. return _getValue(root, value);
  165. };
  166.  
  167. //TODO
  168. this.deleteNode = function(value) {
  169.  
  170. };
  171.  
  172. this.getNodeCache = function() {
  173. return nodeCache;
  174. };
  175.  
  176. this.getDepth = function() {
  177. return depth;
  178. };
  179.  
  180. init(rootElm);
  181. };
  182.  
  183.  
  184. /*
  185. create a sample data which can be
  186. used to construct a binary tree DS
  187. */
  188. let setSampleData = function(noOfExamples) {
  189. const sampleDataset = [];
  190. for(let i = 0; i < noOfExamples; i++) {
  191. sampleDataset.push({
  192. // here key contains the metadata
  193. key: Math.random(),
  194. value: Math.random()
  195. });
  196. }
  197. return sampleDataset;
  198. };
  199.  
  200. //create sample data set
  201. const data = setSampleData(5);
  202. let bsTree = new BsTree(data[0]);
  203. const start = performance.now();
  204. bsTree.createTree(data);
  205. const end = performance.now();
  206.  
  207. const nodeCache = bsTree.getNodeCache();
  208. const depth = bsTree.getDepth();
  209. // since the tree is constructed via `value`
  210. const node = bsTree.getNode(nodeCache[2].getValue());
  211. console.log(nodeCache);
  212. console.log(node);
  213. console.log(depth);
  214. console.log("time taken: " + (end - start));
Add Comment
Please, Sign In to add comment