【DS】C10 -- 二叉树和二叉查找树

192 阅读2分钟

C10 -- 二叉树和二叉查找树

树是一种非线性的数据结构,以分层的方式存储数据。

树被用来存储具有层级关系的数据,比如文件系统中的文件;树还被用来存储有序列表。

树对其查找元素非常快,添加或删除元素也非常快。

根节点是第0层。树的层数是树的深度。

1.实现二叉查找树

//定义Node
function Node(data,left,right){
	this.data = data;
	this.left = left;
	this.right = right;
	this.show = show;
}

function show(){
	return this.data;
}

//二叉查找树BST
function BST(){
	this.root = null;
	this.insert = insert;
    this.inOrder = inOrder;
}

function insert(data){
	var n = new Node(data,null,null);
	if(this.root == null){
		this.root = n;
	}else{
		var current = this.root;
		var parent;
		while(true){
			parent = current;
			if(data<current.data){
				current = current.left;
				if(current == null){
					parent.left = n;
					break;
				}
			}else{
				current = current.right;
				if(current == null){
					parent.right = n;
					break;
				}
			}
		}
	}
}

2.遍历二叉查找树

//递归实现中序遍历
function inOrder(node){
	if(!(node == null)){
		inOrder(node.left);
		putstr(node.show()+" ");
		inOrder(node.right);
	}
}
//先序遍历
function preOrder(node){
	if(!(node == null)){
		putstr(node.show()+" ");
		preOrder(node.left);
		preOrder(node.right);	
	}
}

//后序遍历
function postOrder(node){
	if(!(node == null)){		
		postOrder(node.left);
		postOrder(node.right);
        putstr(node.show()+" ");
	}
}

3.在二叉查找树上进行查找

1.查找最大值和最小值

最小值:遍历左子树直到找到最后一个节点

最大值:遍历右子树直到找到最后一个节点

function getMin(){
	var current = this.root;
	while(!(current.left==null)){
		current = current.left;
	}
	return current.data;
}

function getMax(){
	var current = this.root;
	while(!(current.right == null)){
		current = current.right;
	}
	return current.data;
}
2.查找给定值
function find(data){
	var current = this.root;
	while(current != null){
		if(current.data == data){
			return current;
		}else if(data<current.data){
			current = current.left;
		}else{
			current = current.right;
		}
	}
	return null;
}
3.从二叉查找树上删除节点
function remove(data){
	root = removeNode(this.root,data)
}
function removeNode(node,data){
	if(node == null){
		return null;
	}
	if(data == node.data){
		if(node.left == null && node.right == null){
			return null;
		}
		if(node.left == null){
			return node.right;
		}
		if(node.right == null){
			return node.left;
		}
		var tempNode = getSmallest(node.right);
		node.data = tempNode.data;
		node.right = removeNode(node.right,tempNode.data);
		return node;
	}else if(data< node.data){
		node.left = removeNode(node.left,data);
		return node;
	}else{
		node.right = removeNode(node.right,data);
		return node;
	}
}
计数
//记录一组数据集中不同成绩出现的次数
function Node(data,left,right){
	this.data = data;
	this.left = left;
	this.right = right;
	this.show = show;
	this.count = 1;
}

function update(data){
	var grade = this.find(data);
	grade.count++;
	return grade;
}

function prArray(arr){
	putStr(arr[0].toString()+' ');
	for(var i = 1;i<arr.length;++i){
		putStr(arr[i].toString()+' ');
		if(i%10 == 0){
			putstr("\n");
		}
	}
}

//随机产生成绩
function genArray(length){
	var arr = [];
	for (var i= 0;i<length;++i){
		arr[i] = Math.floor(Math.random()*101);
	}
	return Arr;
}