• 5.2 二叉树的创建和遍历


    这里创建的二叉树为结构为:

                     A
    B C
    # D E F # # # # # #

    实现的二叉树遍历为前序遍历,中序遍历和后序遍历

    具体代码为:

    <?php
    header("content-type:text/html;charset=utf-8");
    /**
     * 二叉树的基本操作
     *
     *包括
     * 1.初始化 __contruct()
     * 2.创建二叉树 createBinaryTree()
     * 3.前序遍历 preOrderTraverse()
     * 4.中序遍历 inOrderTraverse()
     * 5.后序遍历 postOrderTraverse()
     */
    class BTNode{
        public $data;
        public $lchild;
        public $rchild;
        public function __construct($data)
        {
            $this->data = $data;
            $this->lchild = null;
            $this->rchild = null;
        }
    
    
    }
    
    class Binary_tree{
        private $root;
        private $btdata;
    
        //初始化
        public function __construct($btdata = null)
        {
            $this->root = new BTNode(null);
            $this->btdata = $btdata;
        }
    
        //创建二叉树
        public function createBinaryTree(&$root = null){
    
            $elem = array_shift($this->btdata);
            if($elem==null){
                return '';
    
            }elseif ($elem=='#'){//终止结点
                $root = '#';
            //    return $root;
            }else{
                $root = new BTNode($elem);//隐含条件 $root->data = $elem;
    
                $this->createBinaryTree($root->lchild);
                $this->createBinaryTree($root->rchild);
                }
            return $root;
        }
    
        //前序遍历
        public function preOrderTraverse($tree,&$temp){
            if($tree != '#'){
    
                $temp[] = $tree->data;
                $this->preOrderTraverse($tree->lchild,$temp);
                $this->preOrderTraverse($tree->rchild,$temp);
            }else{
                return '';
            }
    
            return $temp;
        }
    
        //中序遍历
        public function inOrderTraverse($tree,&$temp){
            if($tree != '#'){
    
                $this->inOrderTraverse($tree->lchild,$temp);
                $temp[] = $tree->data;
                $this->inOrderTraverse($tree->rchild,$temp);
            }else{
                return '';
            }
       //     $temp = array_filter($temp);
            return $temp;
        }
    
        //后序遍历
        public function postOrderTraverse($tree,&$temp){
            if($tree !='#'){
    
                $this->postOrderTraverse($tree->lchild,$temp);
                $this->postOrderTraverse($tree->rchild,$temp);
                $temp[] = $tree->data;
            }else{
                return '';
            }
         //   $temp = array_filter($temp);
            return $temp;
        }
    
    }
    
    ?>

    实现上述函数:

    <?php
    header("content-type:text/html;charset=utf-8");
    include 'binary_tree.class.php';
    
    $data = array('A', 'B', '#', 'D', '#', '#', 'C','E', '#', '#','F', '#', '#');
    
    echo "初始化二叉树:";
    echo "</br>";
    $binary_tree = new Binary_tree($data);
    print_r($binary_tree);
    echo "</br>";
    echo "</br>";
    
    echo "前序创建一个二叉树:";
    echo "</br>";
    $tree = $binary_tree->createBinaryTree();
    print_r($tree);
    echo "</br>";
    echo "</br>";
    
    echo "二叉树的前序遍历:";
    echo "</br>";
    $preOrder = $binary_tree->preOrderTraverse($tree,$temp1);
    print_r($preOrder);
    echo "</br>";
    echo "</br>";
    
    echo "二叉树的中序遍历:";
    echo "</br>";
    $inOrder=$binary_tree->inOrderTraverse($tree,$temp2);
    print_r($inOrder);
    echo "</br>";
    echo "</br>";
    
    echo "二叉树的后序遍历:";
    echo "</br>";
    $postOrder=$binary_tree->postOrderTraverse($tree,$temp3);
    print_r($postOrder);
    ?>

    最后的输出结果:

  • 相关阅读:
    PetShop 4.0讨论专贴(Q&A)
    Google完成对Writely的整合
    Atlas学习手记(29):JavaScript面向对象的扩展(三):接口Interface
    Atlas学习手记(25):使用行为增强用户界面(五):AutoComplete Behavior
    Atlas学习手记(21):使用行为增强用户界面(一):Click Behavior
    Atlas学习手记系列
    Atlas学习手记(24):使用行为增强用户界面(四):Popup Behavior
    Atlas学习手记(27):JavaScript面向对象的扩展(一):命名空间Namespace
    同一个联盟,同一个梦想 —— 微软 .NET 俱乐部 2006 年在线发布会
    微软中文新闻组提供免费在线支持
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/9866535.html
Copyright © 2020-2023  润新知