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();
    }

  • 相关阅读:
    vue $refs的静态绑定使用与动态绑定使用
    net core中Vue.component单独一个文件不运行,不报错的处理
    C语言之指针基础
    C语言之指针函数
    指针强化
    C语言之指针数组
    C语言之数组
    C语言之数据类型
    C语言之内存管理
    C语言之流程控制
  • 原文地址:https://www.cnblogs.com/maomaolw3/p/4465316.html
Copyright © 2011-2022 走看看