二叉树算法

Posted PLDaily

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树算法相关的知识,希望对你有一定的参考价值。

<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>二叉树算法</title> 
<script type="text/javascript"> 
window.onload = function () {
function Node(data, left, right) {
	this.data = data;
	this.left = left;
	this.right = right;
}

Node.prototype.show = function() {
	return this.data;
}

//建立一个二叉树
function BST() {
	this.root = null;
}

//二叉树的插入操作
BST.prototype.insert = function(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;
				}
			}
		}
	}
}

//二叉树的历遍操作
BST.prototype.inOrder = function(node) {
	if(!(node === null)) {
		this.inOrder(node.left);
		console.log(node.show());
		this.inOrder(node.right);
	}
}//中序历遍

BST.prototype.preOrder = function(node) {
	if(!(node === null)) {
		console.log(node.show());
		this.inOrder(node.left);
		this.inOrder(node.right);
	}
}//先序历遍

BST.prototype.postOrder = function(node) {
	if(!(node === null)) {
		this.inOrder(node.left);
		this.inOrder(node.right);
		console.log(node.show());
	}
}//后序历遍

BST.prototype.getMin = function() {
	var current = this.root;
	while(!(current.left === null)) {
		current = current.left;
	}
	return current.data;
}//获取最小值

BST.prototype.getMax = function() {
	var current = this.root;
	while(!(current.right === null)) {
		current = current.right;
	}
	return current.data;
}//获取最大值

BST.prototype.find = function(data) {
	var current = this.root;
	while(current !== null) {
		if(current.data === data) {
			return current.data;
		}else if(data < current.data) {
			current = current.left;
		}else {
			current = current.right;
		}
	}
	return null;
}//查找节点

BST.prototype.getSmallest = function(node) {
	if (node.left == null) {
	  return node;
	}
	else {
	  return this.getSmallest(node.left);
	}
}

//删除一个节点,需要传入一个root节点(根节点)
BST.prototype.remove = function(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 = this.getSmallest(node.right); 
		node.data = tempNode.data;
		node.right = remove(node.right, tempNode.data);
		return node;
	}else if(data < node.data) {
		node.left = this.remove(node.left, data);
		return node;
	}else {
		node.right = this.remove(node.right, data);
		return node;
	}
}
var num = new BST();
num.insert(23);
num.insert(45);
num.insert(16);
num.insert(37);
num.insert(3);
num.insert(99);
num.insert(22);
//console.log(num.root);
//num.inOrder(num.root);
//console.log(num.getMin());
//console.log(num.getMax());
num.inOrder(num.root);
console.log(num.remove(num.root,37));
num.inOrder(num.root);
}
</script> 
</head> 
<body> 
</body> 
</html>

  

以上是关于二叉树算法的主要内容,如果未能解决你的问题,请参考以下文章

讲透学烂二叉树:二叉树的遍历图解算法步骤及JS代码

算法——建立线索二叉树

二叉树遍历算法

数据结构与算法二叉树——二叉树的最小深度

代码随想录算法训练营第15天 | ● 层序遍历 10 ● 226.翻转二叉树 ● 101.对称二叉树 2

c++如何用非递归的算法去创建二叉树,有没有分层建立二叉树的方法