• 二叉树算法


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

      

  • 相关阅读:
    [转]Worksheet.Change Event (Excel)
    [转]EXCEL如何使用动态公式
    [转]用NPOI操作EXCEL--数据有效性
    [转]How to insert a row between two rows in an existing excel with HSSF (Apache POI)
    [转]Excel
    [转]asp.net解决高并发的方案.
    [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
    Programming ActionScript 3.0 for Flash
    页游 《大皇帝》
    [转]Flash ActionScript2.0面向对象游戏开发-推箱子
  • 原文地址:https://www.cnblogs.com/pcd12321/p/5303308.html
Copyright © 2020-2023  润新知