|
| 1 | +class Node { |
| 2 | + constructor(value){ |
| 3 | + this.value = value; |
| 4 | + this.left = null; |
| 5 | + this.right = null; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +class BinarySearchTree { |
| 10 | + constructor(){ |
| 11 | + this.root = null; |
| 12 | + } |
| 13 | + insert(value){ |
| 14 | + var newNode = new Node(value); |
| 15 | + if(this.root === null){ |
| 16 | + this.root = newNode; |
| 17 | + return this; |
| 18 | + } |
| 19 | + var current = this.root; |
| 20 | + while(true){ |
| 21 | + if(value === current.value) return undefined; |
| 22 | + if(value < current.value){ |
| 23 | + if(current.left === null){ |
| 24 | + current.left = newNode; |
| 25 | + return this; |
| 26 | + } |
| 27 | + current = current.left; |
| 28 | + } else { |
| 29 | + if(current.right === null){ |
| 30 | + current.right = newNode; |
| 31 | + return this; |
| 32 | + } |
| 33 | + current = current.right; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + find(value){ |
| 38 | + if(this.root === null) return false; |
| 39 | + var current = this.root, |
| 40 | + found = false; |
| 41 | + while(current && !found){ |
| 42 | + if(value < current.value){ |
| 43 | + current = current.left; |
| 44 | + } else if(value > current.value){ |
| 45 | + current = current.right; |
| 46 | + } else { |
| 47 | + found = true; |
| 48 | + } |
| 49 | + } |
| 50 | + if(!found) return undefined; |
| 51 | + return current; |
| 52 | + } |
| 53 | + contains(value){ |
| 54 | + if(this.root === null) return false; |
| 55 | + var current = this.root, |
| 56 | + found = false; |
| 57 | + while(current && !found){ |
| 58 | + if(value < current.value){ |
| 59 | + current = current.left; |
| 60 | + } else if(value > current.value){ |
| 61 | + current = current.right; |
| 62 | + } else { |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + return false; |
| 67 | + } |
| 68 | + BFS(){ |
| 69 | + var node = this.root, |
| 70 | + data = [], |
| 71 | + queue = []; |
| 72 | + queue.push(node); |
| 73 | + |
| 74 | + while(queue.length){ |
| 75 | + node = queue.shift(); |
| 76 | + data.push(node.value); |
| 77 | + if(node.left) queue.push(node.left); |
| 78 | + if(node.right) queue.push(node.right); |
| 79 | + } |
| 80 | + return data; |
| 81 | + } |
| 82 | + DFSPreOrder(){ |
| 83 | + var data = []; |
| 84 | + function traverse(node){ |
| 85 | + data.push(node.value); |
| 86 | + if(node.left) traverse(node.left); |
| 87 | + if(node.right) traverse(node.right); |
| 88 | + } |
| 89 | + traverse(this.root); |
| 90 | + return data; |
| 91 | + } |
| 92 | + DFSPostOrder(){ |
| 93 | + var data = []; |
| 94 | + function traverse(node){ |
| 95 | + if(node.left) traverse(node.left); |
| 96 | + if(node.right) traverse(node.right); |
| 97 | + data.push(node.value); |
| 98 | + } |
| 99 | + traverse(this.root); |
| 100 | + return data; |
| 101 | + } |
| 102 | + DFSInOrder(){ |
| 103 | + var data = []; |
| 104 | + function traverse(node){ |
| 105 | + if(node.left) traverse(node.left); |
| 106 | + data.push(node.value); |
| 107 | + if(node.right) traverse(node.right); |
| 108 | + } |
| 109 | + traverse(this.root); |
| 110 | + return data; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +var tree = new BinarySearchTree(); |
| 116 | +tree.insert(10); |
| 117 | +tree.insert(6); |
| 118 | +tree.insert(15); |
| 119 | +tree.insert(3); |
| 120 | +tree.insert(8); |
| 121 | +tree.insert(20); |
| 122 | +tree.DFSPreOrder(); |
| 123 | +tree.DFSPostOrder(); |
| 124 | +tree.DFSInOrder(); |
| 125 | + |
| 126 | + |
| 127 | + |
0 commit comments