zoukankan      html  css  js  c++  java
  • 排序二叉树

    /**较高的查找效率,而且是链式存储,易插入删除*/

    public class BinarySortTree
    {
    /**
    *根节点
    */
    public var treeRoot:Node;

    public function BinarySortTree(_root:Node = null)
    {
    if (_root){
    treeRoot = _root;
    }
    }

    /**
    *插入数组
    */
    public function insert(arr:Array):void{
    for each(var item:int in arr){
    insertItem(item);
    }
    }

    /**
    *插入单个元素
    */
    public function insertItem(item:int):void{
    if(treeRoot){
    var tempTree:BinarySortTree;
    if (item < treeRoot.value){
    tempTree = new BinarySortTree(treeRoot.left);
    tempTree.insertItem(item);
    if(!treeRoot.left){
    this.treeRoot.left = tempTree.treeRoot;
    }
    }else{
    tempTree = new BinarySortTree(treeRoot.right);
    tempTree.insertItem(item);
    if(!treeRoot.right){
    this.treeRoot.right = tempTree.treeRoot;
    }
    }
    }else{
    this.treeRoot = new Node(item);
    }
    }

    /**
    *中序遍历所有节点 ,得到从小到大序列
    */
    public function printAllNodes():void{
    if(treeRoot){
    var tempTree:BinarySortTree;
    tempTree = new BinarySortTree(treeRoot.left);
    tempTree.printAllNodes();

    trace(treeRoot.value);

    tempTree = new BinarySortTree(treeRoot.right);
    tempTree.printAllNodes();
    }
    }
    }
    }

    class Node
    {
    public var value:int;

    public var left:Node;

    public var right:Node;

    public function Node(value:int)
    {
    this.value = value;
    }
    }

    =============================

    /**演示*/

    public function Main()
    {
    var tree:BinarySortTree = new BinarySortTree;
    tree.insert([2,4,3,5,7,11,9,22,1]);
    tree.printAllNodes();
    }

  • 相关阅读:
    位运算 & 网络序字节序
    TFTP & commons-net-3.3.jar
    存储过程
    poj1185-炮兵阵地(状态压缩dp)
    hdu4570-区间dp
    codevs1026-dp(记忆化搜索)
    hdu1494 跑跑卡丁车(动态规划)
    hdu5094-Maze
    hdu4403- A very hard Aoshu problem(搜索)
    hdu2510-符号三角形(dfs+打表)
  • 原文地址:https://www.cnblogs.com/maomaolw3/p/4465316.html
Copyright © 2011-2022 走看看