數據結構之二叉搜索樹(BST)--JavaScript實現

原理:node

叉排序樹的查找過程和次優二叉樹相似,一般採起二叉鏈表做爲二叉排序樹存儲結構中序遍歷二叉排序樹可獲得一個關鍵字的有序序列,一個無序序列能夠經過構造一棵二叉排序樹變成一個有序序列,構造樹的過程即爲對無序序列進行排序的過程。每次插入的新的結點都是二叉排序樹上新的葉子結點,在進行插入操做時,沒必要移動其它結點,只需改動某個結點的指針,由空變爲非空便可。搜索,插入,刪除的複雜度等於樹高,O(log(n)).post

JavaScript實現:this

var BinarySearchTree = function(){ this.root = null; } BinarySearchTree.prototype = { insert: function(key){//插入 var newNode = new this.Node(key); if(this.root === null){ this.root = newNode; }else{ this.insertNode(this.root, newNode) } console.log(this.root) }, inOrderTraverse: function(callback){//中序查找 this.inOrderTraverseNode(this.root, callback); }, preOrderTraverse: function(callback){//先序查找 this.preOrderTraverseNode(this.root, callback); }, postOrderTraverse: function(callback){//後序查找 this.postOrderTraverseNode(this.root, callback); }, min: function(){//最小值 return this.minNode(this.root) }, max: function(){//最大值 return this.maxNode(this.root) }, search: function(key){//查找 this.searchNode(this.root, key) }, remove: function(key){//移除樹節點 this.removeNode(this.root, key) }, Node: function(key){ this.key = key; this.left = null; this.right = null; }, insertNode: function(node, newNode){ if(newNode.key < node.key){ 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) } } }, inOrderTraverseNode: function(node, callback){ if(node !== null){ this.inOrderTraverseNode(node.left, callback); callback(node.key); this.inOrderTraverseNode(node.right, callback); } }, preOrderTraverseNode: function(node, callback){ if(node !== null){ callback(node.key); this.preOrderTraverseNode(node.left, callback); this.preOrderTraverseNode(node.right, callback); } }, postOrderTraverseNode: function(node, callback){ if(node !== null){ this.postOrderTraverseNode(node.left, callback); this.postOrderTraverseNode(node.right, callback); callback(node.key); } }, minNode: function(node){ if(node){ while(node && node.left !== null){ node = node.left; } return node.key; } return null; }, maxNode: function(node){ if(node){ while(node && node.right !== null){ node = node.right; } return node.key; } return null; }, searchNode: function(node, key){ if(node === null) return false; if(key < node.key){ return this.searchNode(node.left, key); }else if(key > node.key){ return this.searchNode(node.right, key); }else{ return true; } }, removeNode(node, key){ if(node === null) return null; if(key < node.key){ node.left = this.removeNode(node.left, key); return node; }else if(key > node.key){ node.right = this.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 = this.findMinNode(node.right); node.key = aux.key; node.right = this.removeNode(node.right, aux.key); return node; } }, findMinNode: function(node){ if(node){ while(node && node.left !== null){ node = node.left; } return node.key; } return null; } } 
相關文章
相關標籤/搜索