1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| const Compare = { less: -1, bigger: 1, equ: 0, }; class Node { constructor(key) { this.key = key; this.left = null; this.right = null; } } class BinarySearchTree { constructor() { this.root = null; } compareFn(a, b) { if (a === b) { return Compare.equ; } return a < b ? Compare.less : Compare.bigger; } insert(key) { if (this.root == null) { this.root = new Node(key); } else { this.insertNode(this.root, key); } } insertNode(node, key) { if (this.compareFn(key, node.key) === Compare.less) { if (node.left == null) { node.left = new Node(key); } else { this.insertNode(node.left, key); } } else if (this.compareFn(key, node.key) === Compare.bigger) { if (node.right == null) { node.right = new Node(key); } else { this.insertNode(node.right, key); } } } inOrderMap(callback) { this.inOrderMapNode(this.root, callback); } inOrderMapNode(node, callback) { if (node != null) { this.inOrderMapNode(node.left, callback); callback(node.key); this.inOrderMapNode(node.right, callback); } } preOrderMap(callback) { this.preOrderMapNode(this.root, callback); } preOrderMapNode(node, callback) { if (node != null) { callback(node.key); this.preOrderMapNode(node.left, callback); this.preOrderMapNode(node.right, callback); } } postOrderMap(callback) { this.postOrderMapNode(this.root, callback); } postOrderMapNode(node, callback) { if (node != null) { this.postOrderMapNode(node.left, callback); this.postOrderMapNode(node.right, callback); callback(node.key); } } min() { return this.minNode(this.root); } minNode(node) { let current = node; while (current != null && current.left != null) { current = current.left; } return current; } max() { return this.maxNode(this.root); } maxNode(node) { let current = node; while (current != null && current.right != null) { current = current.right; } return current; } search(key) { return this.searchNode(this.root, key); } searchNode(node, key) { if (node == null) { return false; } else if (this.compareFn(key, node.key) === Compare.less) { return this.searchNode(node.left, key); } else if (this.compareFn(key, node.key) === Compare.bigger) { return this.searchNode(node.right, key); } else if (this.compareFn(key, node.key) === Compare.equ) { return true; } } remove(key) { this.root = this.removeNode(this.root, key); } removeNode(node, key) { if (node == null) { return null; } else if (this.compareFn(key, node.key) === Compare.less) { node.left = this.removeNode(node.left, key); } else if (this.compareFn(key, node.key) === Compare.bigger) { node.right = this.removeNode(node.right, key); } else if (this.compareFn(key, node.key) === Compare.equ) { if (node.left == null && node.right == null) { node = null; } else if (node.left == null) { node = node.right; } else if (node.right == null) { node = node.left; } else { const target = this.minNode(node.right); node.key = target.key; node.right = this.removeNode(node.right, target.key); } } return node; } }
|