• 二叉树算法


    <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>
    

      

  • 相关阅读:
    yeoman+grunt/gulp+bower构建angular项目
    eclipse配置svn 并从svn下载maven项目 配置tomcat启动maven项目(二)
    eclipse配置svn 并从svn下载maven项目 配置tomcat启动maven项目
    mysql Oracle常用使用区别
    海思平台交叉编译curl支持SSL功能
    Gitlab 自动构建心得
    openssl windows平台编译库
    更换树莓派源成国内源
    使用ntp协议同步本地时间(C语言)
    网络分析纪录
  • 原文地址:https://www.cnblogs.com/pcd12321/p/5303308.html
Copyright © 2020-2023  润新知