zoukankan      html  css  js  c++  java
  • 二叉树算法

    <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获取渲染后的样式
    小程序-实现带字母导航的滚动列表
    带排序动画的横向条形图
    VUE模仿百度搜索框,按上下方向键及回车键实现搜索选中效果
    js------科学计数法转换为正常小数
    js------保留指定位数小数
    css布局------左边宽度不定,右边宽度自动填满剩余空间
    css布局------左右宽度固定,中间宽度自适应容器
    css布局------上下高度固定,中间高度自适应容器
    css布局------块元素水平垂直居中的四种方法
  • 原文地址:https://www.cnblogs.com/pcd12321/p/5303308.html
Copyright © 2011-2022 走看看