//对二叉树数据结构的学习,以及js语言完成简单功能实现
<!doctype html>
<html>
<head>
<title>二叉树</title>
</head>
<body>
<script type="text/javascript">
function BinaryTree(){
var Node =function(key){
this.key=key;
this.left=null;
this.right=null;
}
var root=null;
var insertNode=function(node,newNode){
if(node.key>newNode.key){
if(node.left===null){
node.left=newNode;
}else{
insertNode(node.left,newNode);
}
}else{
if(node.right===null){
node.right=newNode;
}else{
insertNode(node.right,newNode);
}
}
}
this.insert=function(key){
var node= new Node(key);
if(root===null){
root=node;
}else{
insertNode(root,node);
}
}
//中序
var inOrderNode=function(node,callback){
if(node!==null){
inOrderNode(node.left,callback);
callback(node);
inOrderNode(node.right,callback);
}
}
this.inOrder=function(callback){
inOrderNode(root,callback);
}
//先序
var preOrderNode=function(node,callback){
if(node!==null){
callback(node);
preOrderNode(node.left,callback);
preOrderNode(node.right,callback);
}
}
this.preOrder=function(callback){
preOrderNode(root,callback);
}
//后序
var postOrderNode=function(node,callback){
if(node!==null){
postOrderNode(node.left,callback);
postOrderNode(node.right,callback);
callback(node);
}
}
this.postOrder=function(callback){
postOrderNode(root,callback);
}
//查找最小值
var findMin=function(node){
if(node.left===null){
return node;
}else{
return findMin(node.left);
}
return null;
}
this.min=function(){
return findMin(root);
}
//查找最大值
var findMax=function(node){
if(node.right===null){
return node;
}else{
return findMax(node.right);
}
return null;
}
this.max=function(){
return findMax(root);
}
//查找最大值2
var findMax2=function(node){
if(node){
while(node && node.right!==null){
node=node.right;
}
return node;
}
return null;
}
this.max2=function(){
return findMax2(root);
}
//查找某个值的节点
var findKey=function(node,key){
if(node){
if(node.key===key){
return node;
}else if(node.key>key){
return findKey(node.left,key);
}else{
return findKey(node.right,key);
}
}
return null;
}
this.key=function(key){
return findKey(root,key);
}
//删除某个节点
var removeNode=function(node,key){
if(node){
if(node.key>key){
node.left=removeNode(node.left,key);
return node;
}else if(node.key<key){
node.right=removeNode(node.right,key);
return node;
}else{
if(node.left===null&&node.right===null){
node=null;
document.write("删除的叶子节点"+key+'<br/>');
return node;
}else if(node.left===null&&node.right!==null){
node=node.right;
document.write("删除仅有右子树的节点"+key+'<br/>');
return node;
}else if(node.right===null&&node.left!==null){
node=node.left;
document.write("删除仅有左子树的节点"+key+'<br/>');
return node;
}else{
//从右子树中找到最小的节点,将删除节点的值赋最小节点的值,同时删除最小节点
document.write("删除同时有左右子树的节点"+key+'<br/>');
var mnode=findMin(node.right);
node.key=mnode.key;
document.write("将右子树中的最小值"+mnode.key+'赋给被删除的节点'+'<br/>');
removeNode(node.right,mnode.key);
return node;
}
}
}else {
return null;
}
}
this.remove =function(key){
var rnode = findKey(root,key);
if(rnode===null){
document.write("二叉树中已经不存在该节点了!!");
}else{
removeNode(root,key);
}
}
}
var callback=function(node){
document.write(node.key+"<br/>");
}
var nodes=[8,3,10,1,6,14,4,7,13];
var binaryTree =new BinaryTree();
nodes.forEach(function(key){binaryTree.insert(key);})
binaryTree.inOrder(callback);
document.write("<br/>");
binaryTree.preOrder(callback);
document.write("<br/>");
binaryTree.postOrder(callback);
document.write("<br/>");
binaryTree.min();
document.write("<br/>");
binaryTree.max();
document.write("<br/>");
document.write(binaryTree.max2()+"<br/>");
document.write(binaryTree.key(3).key+"<br/>");
binaryTree.remove(3);
binaryTree.inOrder(callback);
</script>
</body>
</html>
二叉树的一系列操作(js)
最新推荐文章于 2020-07-28 19:19:44 发布