function BinaryTree(){ var Node = function(key){ this.key = key; //值 this.left = null; //左箭头 this.right = null; //右箭头 } //根节点 var root = null; var insertNode = function(oldNode,newNode){ if(newNode.key < oldNode.key){ if(oldNode.left === null){ oldNode.left = newNode }else{ insertNode(oldNode.left,newNode)//递归查找 } }else{ if(oldNode.right === null){ oldNode.right = newNode }else{ insertNode(oldNode.right,newNode); } } } //插入节点 this.insert = function(key) { var newNode = new Node(key); if(root === null){ root = newNode; }else{ insertNode(root,newNode) } } //中序排列 this.inOrderTraverse = function(callback){ inOrderTraverseNode(root,callback) } //中序排序辅助函数 var inOrderTraverseNode = function(node,callback){ if(node !== null){ inOrderTraverseNode(node.left,callback);//遍历左节点 callback(node.key);//遍历根节点,中节点 inOrderTraverseNode(node.right,callback);//遍历右节点 } } //先序遍历,先访问节点本身在遍历左节点,最后遍历右节点 this.preOrderTraverse = function(callback){ preOrderTraverseNode(root,callback); } var preOrderTraverseNode = (node,callback)=>{ if(node !== null){ callback(node.key); preOrderTraverseNode(node.left,callback); preOrderTraverseNode(node.right,callback); } } //后序遍历,先访问节点的后代节点,再访问节点本身 this.postOrderTraverse = function(callback){ postOrderTraverseNode(root,callback); } var postOrderTraverseNode = (node,callback) =>{ if(node !== null){ postOrderTraverseNode(node.left,callback); postOrderTraverseNode(node.right,callback); callback(node.key); } } //搜索最大值,最小值 this.min = function(){ return minNode(root); } var minNode = (node)=>{ if(node){ while(node && node.left !== null){ node = node.left; } return node.key } return null; } this.max = function(){ return maxNode(root); } var maxNode = (node) => { if(node){ while(node && node.right !== null){ node = node.right; } return node.key } return null; } //搜索一个特定的值 this.search = function(key){ return searchNode(root,key); } var searchNode = (node,key) =>{ if(node === null){ return false; } if(key < node.key){ return searchNode(node.left,key) } else if( key > node.key){ return searchNode(node.right,key); }else{ return true; } } //移除一个节点 this.remove = function(key){ return removeNode(root,key); } var removeNode = (node,key) =>{ if(node === null){ return false } 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; } //第二种情况 一个只有一个子节点的节点 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 findMinNode = (node)=>{ if(node){ while(node && node.left !== null){ node = node.left; } return node } return null; } } let node = [8,3,10,1,6,14,4,7,13]; var binaryTree = new BinaryTree(); node.forEach((key)=>{ binaryTree.insert(key); }) var printNode = (val) => { console.log(val) } binaryTree.inOrderTraverse(printNode) //中序遍历 binaryTree.preOrderTraverse(printNode) //先序遍历 binaryTree.postOrderTraverse(printNode) //后序遍历 console.log(binaryTree.min() + ': min') console.log(binaryTree.max() + ': max') console.log(binaryTree.search(8) + ': search') console.log(binaryTree.remove(8) )