js实现完全排序二叉树、二叉搜索树

1,237 阅读3分钟

完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。
对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度
为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。

二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)
它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,
则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,
则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。

排序二叉树的实现考虑加入null的情况。

 //创建二叉树
function Node(data,left,right){
    this.val = data;
    this.left = left;
    this.right = right;
}
 Node.prototype.show = function(){
    return this.val;
}

function BST(){
    this.root = null;
}

//查找二叉树
BST.prototype.insert = function(data){
	if(data === null){
		return false
	}
    var node = new Node(data,null,null);
    if(this.root == null){
        this.root = node;
    }else{
        var current = this.root;
        var parent;
        while(true){
            parent = current;
            if(data < current.val){
                current = current.left;
                if(current == null){
                    parent.left = node;
                    break;
                }
            }else{
                current = current.right;
                if(current == null){
                    parent.right = node;
                    break;
                }
            }
        }
    }
}

//迭代方式创建普通给定数组顺序二叉树
BST.prototype.create = function(charecters){
	var len = charecters.length ;  //数组的长度
    var nodes = new Array(); //创建一个临时数组,用于存放二叉树节点
    //循环创建二叉树节点存放到数组中
    for (var i = 0 ; i < len ; i++) {
    	if(charecters[i]){
    		var node = new Node(charecters[i],null,null);
    		nodes.push(node);
    	}else{
    		nodes.push(null)
    	}
    }
	var index = 0;//索引从0开始
	this.root = nodes[0];
	// [ 5 , 1 , 4 , null , null , 3 , 6 , 6 , null , 7]
	//1.     ^ 5 ^
	//2.              ^   1   ^
	//3.                           ^ 4 ^
	//4.                                   ^ 3 ^
	//count计算有null占位的情况
	var count = 0 
	//循环建立二叉树子节点的引用
    while(index < len) {
    	
        if(nodes[index]){
        	var leftIndex = 2*(index-count)+1,              //当前节点左孩子索引
        		rightIndex = 2*(index-count)+2;             //当前节点右孩子索引
        	//给当前节点添加左孩子
        	nodes[index].left = nodes[leftIndex];
        	//给当前节点添加右孩子
        	nodes[index].right = nodes[rightIndex];
        }else{
        	count++;
        }
        index++;
    }
}

//递归方式创建普通给定数组顺序二叉树
BST.prototype.createRecurrence = function(charecters){
    this.root = new Node(charecters[0],null,null);
	var innerfun = function(nodes,i){
		var leftIndex = 2*i+1,
			rightIndex = 2*i+2;
		if(leftIndex < charecters.length){
			var node = new Node(charecters[leftIndex],null,null);
			nodes.left = node;
			innerfun(node,leftIndex);
		}
		if(rightIndex < charecters.length){
			var node = new Node(charecters[rightIndex],null,null);
			nodes.right = node;
			innerfun(node,rightIndex);
		}
	}
	innerfun(this.root,0)
}

//中序遍历
BST.prototype.inOrder = function(node){
    if(node){
        this.inOrder(node.left);
        console.log(node.show() + " ");
        this.inOrder(node.right);
    }
}

//堆栈  后进先出  将当前结点的孩子全部遍历结束,在遍历同一级的节点  深度优先搜索
BST.prototype.deepTraversal = function(node){
	var list = []
	var stack = []
	stack.push(node)
	while(stack.length !== 0){
		var item = stack.pop()
		list.push(item.val)
		if(item.right){
			stack.push(item.right)
		}
		if(item.left){
			stack.push(item.left)
		}
	}
	return list
}

//队列   先进先出  根  第一层子节点  第二层子节点  左到右依次遍历  广度优先搜索
BST.prototype.wideTraversal = function(node){
	var list = []
	var stack = []
	stack.push(node)
	while(stack.length !== 0){
		var item = stack.shift()
		list.push(item.val)
		if(item.left){
			stack.push(item.left)
		}
		if(item.right){
			stack.push(item.right)
		}
	}
	return list
}

调用方式1:迭代生成
var bst  = new BST();
var nums = [5,1,4,null,null,3,6,6,null,7]
bst.create(nums);
console.log(bst)

调用方式2:递归生成
var bst  = new BST();
var nums = [5,1,4,null,null,3,6,6,null,7]
bst.createRecurrence(nums);
console.log(bst)

调用方式3:
var bst  = new BST();
var nums = [5,1,4,null,null,3,6,6,null,7]
for(var i = 0;i < nums.length;i ++){
  bst.insert(nums[i]);
}
console.log(bst)