PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)…

PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)…

大家好,又见面了,我是全栈君。

点击上方“码农编程进阶笔记”,选择“置顶或者星标

优质文章第一时间送达!

前言:

深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次。要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历、中序遍历、后序遍历。具体说明如下:

前序遍历:根节点->左子树->右子树

中序遍历:左子树->根节点->右子树

后序遍历:左子树->右子树->根节点

广度优先遍历:又叫层次遍历,从上往下对每一层依次访问,在每一层中,从左往右(也可以从右往左)访问结点,访问完一层就进入下一层,直到没有结点可以访问为止。

例如对于一下这棵树:

PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)...

深度优先遍历:

前序遍历:10 8 7 9 12 11 13

中序遍历:7 8 9 10 11 12 13

后序遍历:7 9 8 11 13 12 10

广度优先遍历:

层次遍历:10 8 12 7 9 11 13

二叉树的深度优先遍历的非递归的通用做法是采用栈,广度优先遍历的非递归的通用做法是采用队列。

深度优先遍历:

1、前序遍历:

/**
     * 前序遍历(递归方法)
     */
    private function pre_order1($root)
{
        if (!is_null($root)) {
            //这里用到常量__FUNCTION__,获取当前函数名,好处是假如修改函数名的时候,里面的实现不用修改
            $function = __FUNCTION__;
            echo $root->key . " ";
            $this->$function($root->left);
            $this->$function($root->right);
        }
    }




    /**
     * 前序遍历(非递归方法)
     * 因为当遍历过根节点之后还要回来,所以必须将其存起来。考虑到后进先出的特点,选用栈存储。
     */
    private function pre_order2($root)
{




        //        $stack = new splstack();
        //        $stack->push($root);
        //        while(!$stack->isEmpty()){
        //            $node = $stack->pop();
        //            echo $node->key.' ';
        //            if(!is_null($node->right)){
        //                $stack->push($node->right);
        //            }
        //            if(!is_null($node->left)){
        //                $stack->push($node->left);
        //            }
        //        }








        if (is_null($root)) {
            return;
        }
        $stack = new splstack();
        $node = $root;
        while (!is_null($node) || !$stack->isEmpty()) {
            while (!is_null($node)) {
                //只要结点不为空就应该入栈保存,与其左右结点无关
                $stack->push($node);
                echo $node->key . ' ';
                $node = $node->left;
            }
            $node = $stack->pop();
            $node = $node->right;
        }
    }








    //前序遍历
    public function PreOrder()
{
        // 所在对象中的tree属性保存了一个树的引用
        //     $this->pre_order1($this->tree->root);
        $this->pre_order2($this->tree->root);
    }

说明:1、我将所有的遍历方法都封装在一个类 traverse 里面了。2、pre_order2方法中,在使用栈的过程中,我使用的是PHP标准库SPL提供的splstack,如果你们习惯使用数组的话,可以使用 array_push() 和array_pop() 模拟实现。

2、中序遍历:

/**
     * 中序遍历(递归方法)
     */
    private function mid_order1($root)
{
        if (!is_null($root)) {
            $function = __FUNCTION__;
            $this->$function($root->left);
            echo $root->key . " ";
            $this->$function($root->right);
        }
    }




    /**
     * 中序遍历(非递归方法)
     * 因为当遍历过根节点之后还要回来,所以必须将其存起来。考虑到后进先出的特点,选用栈存储。
     */
    private function mid_order2($root)
{
        if (is_null($root)) {
            return;
        }




        $stack = new splstack();
        $node = $root;
        while (!is_null($node) || !$stack->isEmpty()) {
            while (!is_null($node)) {
                $stack->push($node);
                $node = $node->left;
            }
            $node = $stack->pop();
            echo $node->key . ' ';
            $node = $node->right;
        }
    }




    //中序遍历
    public function MidOrder()
{
        //        $this->mid_order1($this->tree->root);
        $this->mid_order2($this->tree->root);
    }




3、后序遍历:

/**
     * 后序遍历(递归方法)
     */
    private function post_order1($root)
{
        if (!is_null($root)) {
            $function = __FUNCTION__;
            $this->$function($root->left);
            $this->$function($root->right);
            echo $root->key . " ";
        }
    }




    /**
     * 后序遍历(非递归方法)
     * 因为当遍历过根节点之后还要回来,所以必须将其存起来。考虑到后进先出的特点,选用栈存储。
     * 由于在访问了左子节点后怎么跳到右子节点是难点,这里使用一个标识lastVisited来标识上一次访问的结点
     */
    private function post_order2($root)
{
        if (is_null($root)) {
            return;
        }




        $node = $root;
        $stack = new splstack();
        //保存上一次访问的结点引用
        $lastVisited = NULL;
        $stack->push($node);
        while(!$stack->isEmpty()){
            $node = $stack->top();//获取栈顶元素但不弹出
            if(($node->left == NULL && $node->right == NULL) || ($node->right == NULL && $lastVisited == $node->left) || ($lastVisited == $node->right)){
                echo $node->key.' ';
                $lastVisited = $node;
                $stack->pop();
            }else{
                if($node->right){
                    $stack->push($node->right);
                }
                if($node->left){
                    $stack->push($node->left);
                }
            }
        }
    }




    //后序遍历
    public function PostOrder()
{
        //        $this->post_order1($this->tree->root);
        $this->post_order2($this->tree->root);
    }

广度优先遍历:

1、层次遍历:

  /**
     * 层次遍历(递归方法)
     * 由于是按层逐层遍历,因此传递树的层数
     */
    private function level_order1($root,$level){
        if($root == NULL || $level < 1){
            return;
        }
        if($level == 1){
            echo $root->key.' ';
            return;
        }
        if(!is_null($root->left)){
            $this->level_order1($root->left,$level - 1);
        }
        if(!is_null($root->right)){
            $this->level_order1($root->right,$level - 1);
        }
    }




    /**
     * 层次遍历(非递归方法)
     * 每一层从左向右输出
元素需要储存有先进先出的特性,所以选用队列存储。
     */
    private function level_order2($root){
        if(is_null($root)){
            return;
        }




        $node = $root;
        //利用队列实现
//        $queue = array();
//        array_push($queue,$node);
//
//        while(!is_null($node = array_shift($queue))){
//            echo $node->key.' ';
//            if(!is_null($node->left)){
//                array_push($queue,$node->left);
//            }
//            if(!is_null($node->right)){
//                array_push($queue,$node->right);
//            }
//        }




        $queue = new splqueue();
        $queue->enqueue($node);
        while(!$queue->isEmpty()){
            $node = $queue->dequeue();
            echo $node->key.' ';
            if (!is_null($node->left)) {
                $queue->enqueue($node->left);
            }
            if (!is_null($node->right)) {
                $queue->enqueue($node->right);
            }
        }
    }




    //层次遍历
    public function LevelOrder(){
//        $level = $this->getdepth($this->tree->root);
//        for($i = 1;$i <= $level;$i ++){
//            $this->level_order1($this->tree->root,$i);
//        }




        $this->level_order2($this->tree->root);
    }




    //获取树的层数
    private function getdepth($root){
        if(is_null($root)){
            return 0;
        }
        $left = getdepth($root -> left);
        $right = getdepth($root -> right);
        $depth = ($left > $right ? $left : $right) + 1;
        return $depth;
    }

说明:level_order2方法中,在使用队列的过程中,我使用的是PHP标准库SPL提供的splqueue,如果你们习惯使用数组的话,可以使用 array_push() 和array_shift() 模拟实现。

使用:

现在我们来看看客户端代码:

class Client
{
    public static function Main()
{
        try {
            //实现文件的自动加载
            function autoload($class)
{
                include strtolower($class) . '.php';
            }




            spl_autoload_register('autoload');




            $arr = array(10, 8, 12, 7, 9, 11, 13);
            $tree = new Bst();
//            $tree = new Avl();
//            $tree = new Rbt();




            $tree->init($arr);




            $traverse = new traverse($tree);
            $traverse->PreOrder();
//            $traverse->MidOrder();
//            $traverse->PostOrder();
//            $traverse->LevelOrder();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}




CLient::Main();

PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)...

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111998.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java拦截器放行_java拦截器放行某些请求

    java拦截器放行_java拦截器放行某些请求在java开发中,拦截器使用是很普遍的,最常用的就是登陆拦截了,然后并不是所有的请求我们都需要拦截,比如index页面的请求我们是不拦截的.通常情况下我们有两种方式:先贴出来springboot使用拦截器的case:1.自定义拦截器,实现HandlerInterceptor,也可以采用继承的方式(HandlerInterceptorAdapter),内容不重要,看过程publicclassL…

    2022年6月7日
    158
  • Java 输出 九九乘法表的代码

    Java 输出 九九乘法表的代码敲成功代码后,超级有成就感。publicclass九九乘法表{publicstaticvoidmain(String[]args){intj=1;for(inti=1;i<10;i++,j=1){for(j=1;j<i+1;j++){System.out.print(j+”×”+i+”=”+i*j+”\t”);

    2022年7月15日
    21
  • bigDecimal除法取整数「建议收藏」

    bigDecimal除法取整数「建议收藏」bigDecimal加减乘法都没问题,除法由于会有除不尽小数的情况,如果不限制小数位数的话会进入死循环报错:java.lang.ArithmeticException:Non-terminatingdecimalexpansion;noexactrepresentabledecimalresult。所以要设定小数位数:BigDecimala=BigDecimal.valueO…

    2022年6月26日
    106
  • UVA 11490 – Just Another Problem(数论)

    UVA 11490 – Just Another Problem(数论)

    2022年1月19日
    43
  • [分享]在线的代码片段测试工具 jsbin[通俗易懂]

    [分享]在线的代码片段测试工具 jsbin[通俗易懂]有些时候,我们往往有这样的需求:临时测试一个代码片段,不想打开编辑器来新建一个文件,测试完毕又删除想给别人分享一个代码,html文件,css文件,js文件,打个包?向别人展个某个效果,发个文件过去?把代码部署到自己服务器上面?针对这些需求,我们使用在线的代码片段测试工具,也许来得更加简单和方便了。针对前端的在线代码片段工具很多,比较常见的有jsbin和jsfiddle以及codepen.而我最喜欢的就是jsbin了,它有着更多的特性给我带来了极大的方便:任意控制要展示的窗口点击这些标

    2025年7月31日
    3
  • 什么是网站的灵魂_测试性能网站

    什么是网站的灵魂_测试性能网站前言在前一篇随笔《大型网站系统架构的演化》中,介绍了大型网站的演化过程,期间穿插了一些技术和手段,我们可以从中看出一个大型网站的轮廓,但想要掌握设计开发维护大型网站的技术,需要我们一步一步去研究实践

    2022年8月4日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号