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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
function BinarySearchTree() { var Node = function(key) { this.key = key; this.left = null; this.right = null; };
var root = null;
this.insert = function(key) { var newNode = new Node(key);
if (root === null) { root = newNode; } else { insertNode(root, newNode); } };
function insertNode(node, newNode) { if (newNode.key < node.key) { if (node.left === null) { node.left = newNode; } else { insertNode(node.left, newNode); } } else { if (node.right === null) { node.right = newNode; } else { insertNode(node.right, newNode); } } }
this.inOrderTraverse = function(cb) { inOrderTraverseNode(root, cb); };
function inOrderTraverseNode(node, cb) { if (node !== null) { inOrderTraverseNode(node.left, cb); cb(node); inOrderTraverseNode(node.right, cb); } }
this.preOrderTraverse = function(cb) { preOrderTraverseNode(root, cb); };
function preOrderTraverseNode(node, cb) { if (node !== null) { cb(node); preOrderTraverseNode(node.left, cb); preOrderTraverseNode(node.right, cb); } }
this.postOrderTraverse = function(cb) { postOrderTraverseNode(root, cb); };
function postOrderTraverseNode(node, cb) { if (node !== null) { postOrderTraverseNode(node.left, cb); postOrderTraverseNode(node.right, cb); cb(node); } }
this.min = function() { return findMinNode(root); };
function findMinNode(node) { var n = node; while(n && n.left) { n = n.left; }
return n; }
this.remove = function(key) { root = removeNode(root, key); }
function removeNode(node, key) { if (node === null) { return null; }
if (key < node.key) { node.left = removeNode(node.left, key); return node; } else if (key > node.key) { node.right = removeNode(node.right, key); return node; } else { if (node.left === null && node.right === null) { node = null; return node; } else if (node.left === null) { node = node.right; return node; } else if (node.right === null) { node = node.left; return node; }
var aux = findMinNode(node.right); node.key = aux.key; node.right = removeNode(node.right, aux.key); return node; } } }
var bst = new BinarySearchTree();
bst.insert(17); bst.insert(1); bst.insert(2); bst.insert(1); bst.insert(10); bst.insert(4); bst.insert(15); bst.insert(6);
console.log('\n中序遍历二叉树 =>\n'); bst.inOrderTraverse(function(node) { console.log(node.key); });
console.log('\n先序遍历二叉树 =>\n'); bst.preOrderTraverse(function(node) { console.log(node.key); });
console.log('\n后序遍历二叉树 =>\n'); bst.postOrderTraverse(function(node) { console.log(node.key); });
console.log('\nmin node =>', bst.min().key);
bst.remove(2); console.log('\n先序遍历二叉树 =>\n'); bst.preOrderTraverse(function(node) { console.log(node.key); });
|