|
| 1 | +/*Binary Search Tree!! |
| 2 | +* |
| 3 | +* Nodes that will go on the Binary Tree. |
| 4 | +* They consist of the data in them, the node to the left, the node |
| 5 | +* to the right, and the parent from which they came from. |
| 6 | +* |
| 7 | +* A binary tree is a data structure in which an element |
| 8 | +* has two successors(children). The left child is usually |
| 9 | +* smaller than the parent, and the right child is usually |
| 10 | +* bigger. |
| 11 | +*/ |
| 12 | + |
| 13 | +// Node in the tree |
| 14 | +function Node(val) { |
| 15 | + this.value = val; |
| 16 | + this.left = null; |
| 17 | + this.right = null; |
| 18 | +} |
| 19 | + |
| 20 | +// Search the tree for a value |
| 21 | +Node.prototype.search = function(val) { |
| 22 | + if (this.value == val) { |
| 23 | + return this; |
| 24 | + } else if (val < this.value && this.left != null) { |
| 25 | + return this.left.search(val); |
| 26 | + } else if (val > this.value && this.right != null) { |
| 27 | + return this.right.search(val); |
| 28 | + } |
| 29 | + return null; |
| 30 | +} |
| 31 | + |
| 32 | +// Visit a node |
| 33 | +Node.prototype.visit = function() { |
| 34 | + // Recursively go left |
| 35 | + if (this.left != null) { |
| 36 | + this.left.visit(); |
| 37 | + } |
| 38 | + // Print out value |
| 39 | + console.log(this.value); |
| 40 | + // Recursively go right |
| 41 | + if (this.right != null) { |
| 42 | + this.right.visit(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Add a node |
| 47 | +Node.prototype.addNode = function(n) { |
| 48 | + if (n.value < this.value) { |
| 49 | + if (this.left == null) { |
| 50 | + this.left = n; |
| 51 | + } else { |
| 52 | + this.left.addNode(n) |
| 53 | + } |
| 54 | + } else if (n.value > this.value) { |
| 55 | + if (this.right == null) { |
| 56 | + this.right = n; |
| 57 | + } else { |
| 58 | + this.right.addNode(n); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function Tree() { |
| 64 | + // Just store the root |
| 65 | + this.root = null; |
| 66 | +} |
| 67 | + |
| 68 | +// Inorder traversal |
| 69 | +Tree.prototype.traverse = function() { |
| 70 | + this.root.visit(); |
| 71 | +} |
| 72 | + |
| 73 | +// Start by searching the root |
| 74 | +Tree.prototype.search = function(val) { |
| 75 | + var found = this.root.search(val); |
| 76 | + if(found === null) |
| 77 | + { |
| 78 | + console.log(val + " not found"); |
| 79 | + } |
| 80 | + else{ |
| 81 | + console.log("Found:"+found.value); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Add a new value to the tree |
| 86 | +Tree.prototype.addValue = function(val) { |
| 87 | + var n = new Node(val); |
| 88 | + if (this.root == null) { |
| 89 | + this.root = n; |
| 90 | + } else { |
| 91 | + this.root.addNode(n); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +//Implementation of BST |
| 96 | +var bst = new Tree(); |
| 97 | +bst.addValue(6); |
| 98 | +bst.addValue(3); |
| 99 | +bst.addValue(9); |
| 100 | +bst.addValue(2); |
| 101 | +bst.addValue(8); |
| 102 | +bst.addValue(4); |
| 103 | +bst.traverse(); |
| 104 | +bst.search(8); |
0 commit comments