我们则先和根节点的值比较,如果小于当前跟节点则向左遍历,大于当前跟节点则向右遍历,如果等于则返回true, 这个时候我们查找一个不存在的值通过chrome最后可以看到root.node == null的情况,此时我们加个判断return false就可以了
有几种情况
实现逻辑叶子节点左右节点为空这种就是直接赋值为null就行了兄弟节点有左边或者右边有节点比如删除的为上图中的10,然后此时左子树为空,我们就此时将14的右子树赋值给根节点。兄弟节点左右都有节点比如上面的图我们删除节点3的值,然后我们需要从比他大的右子树找一个最小值,赋值给节点3,然后将找到的最小值删除callback 只是一个函数,作用:输出节点的值;
看指定的方法时将其方法中的debugger和调用打开在chrome运行即可,主要是看递归调用时root的作为参数传入的变化和递归执行顺序。
class BinaryNode { constructor(node) { this.node = node this.right = null this.left = null } } class BinaryTree { constructor() { this.root = null } insert(newNode) { // debugger var newNode = new BinaryNode(newNode) if (this.root) { this.insertNode(this.root, newNode) } else { this.root = newNode } } insertNode(node, newNode) { // debugger if (node.node > newNode.node) { if (node.left === null) { node.left = newNode } else { this.insertNode(node.left, newNode) } } else { if (node.right === null) { node.right = newNode } else { this.insertNode(node.right, newNode) } } } // 前序遍历 preOrderTraverse(callback) { debugger this.preOrderTraverseNode(this.root, callback); } preOrderTraverseNode(node, callback) { debugger if (node !== null) { callback(node.node) this.preOrderTraverseNode(node.left, callback) this.preOrderTraverseNode(node.right, callback) } } // 中序遍历 inOrderTraverse(callback) { // debugger this.inOrderTraverseNode(this.root, callback) } inOrderTraverseNode(node, callback) { // debugger if (node !== null) { this.inOrderTraverseNode(node.left, callback) callback(node.node) this.inOrderTraverseNode(node.right, callback) } } // 后序遍历 postOrderTraverse(callback) { // debugger this.postOrderTraverseNode(this.root, callback) } postOrderTraverseNode(node, callback) { // debugger if (node !== null) { this.postOrderTraverseNode(node.left, callback) this.postOrderTraverseNode(node.right, callback) callback(node.node) } } // 最小值 min() { // debugger return this.minNode(this.root) } minNode(root) { // debugger if (root.left !== null) { // debugger while(root && root.left) { root = root.left } return root.node } return root.node } // 最大值 max() { // debugger return this.maxNode(this.root) } maxNode(root) { // debugger if(root.right !== null) { while(root && root.right) { root = root.right } return root.node } return root.node } // 查找一个指定node search(node) { // debugger return this.searchNode(this.root, node) } searchNode(root, node) { // debugger if (root === null ) { return false } if (root.node > node) { return this.searchNode(root.left, node) } else if (root.node < node) { return this.searchNode(root.right, node) } else { return true } } remove(node) { debugger this.removeNode(this.root, node) } removeNode(root, node) { debugger if (root === null) { return null } if (root.node > node) { root.left = this.removeNode(root.left, node) return root } else if (root.node < node) { root.right = this.removeNode(root.right, node) return root } else { if (root.left === null && root.right === null) { root = null return root } if (root.left === null) { root = root.right return root } else if (root.right === null) { root = root.left return root } var aux = this.findMinNode(root.right) root.node = aux.node root.right = this.removeNode(root.right, aux.node) return root } } findMinNode(root) { if (root) { while(root && root.left !== null) { root = root.left } return root } return null } } var nodes = [8, 3,10, 1, 6, 14, 4, 7, 13] var binaryTree = new BinaryTree() nodes.forEach(node => { binaryTree.insert(node) }) console.log(binaryTree.root) var callback = function(node) { console.log(node) } // 如果需要哪个代码则打开注释和方法中的debugger即可,打开一个要把不需要看的注释, // 先序遍历 binaryTree.preOrderTraverse(callback) //例如这条打开,这个方法中已经打好的debugger打开, 多看一遍函数的执行顺序和当前值,就好了。 // 中序遍历 // binaryTree.inOrderTraverse(callback) // 后序遍历 // binaryTree.postOrderTraverse(callback) console.log( // `最小值是:${binaryTree.min()}`, // `最大值是:${binaryTree.max()}`, ) console.log( // binaryTree.search(1) ? "search num 1 is fount" : "search num 1 is not found", // binaryTree.search(15) ? "search num 15 is fount" : "search num 15 is not found", ) // binaryTree.remove(1) // binaryTree.remove(3) // binaryTree.remove(10) // console.log(binaryTree.root)