zoukankan      html  css  js  c++  java
  • 编程31:分别用递归和非递归的方式遍历二叉树

    <?php
    header("content-type:text/html;charset=utf-8");
    /*
     *分别用递归和非递归的方式遍历二叉树 P88
     */
    
    class Node{
        public $value;
        public $left;
        public $right;
        public function __construct($value)
        {
            $this->value = $value;
        }
    
    }
    
    function preOrderUnRecure1($head){
        if($head == null){
            return ;
        }
    
        echo $head->value." ";
        preOrderUnRecure1($head->left);
        preOrderUnRecure1($head->right);
    }
    
    function preOrderUnRecure2($head){
        if($head == null){
            return ;
        }
    
        $stack = new SplStack();
        $stack->push($head);
        while (!$stack->isEmpty()){
            $cur = $stack->pop();//注意这里,下一次循环应该是从当前的弹出结点为起点,而不是head
            echo $cur->value." ";
    
            if($cur->right != null){
                $stack->push($cur->right);
            }
    
            if($cur->left != null){
                $stack->push($cur->left);
            }
        }
    }
    
    function inOrderUnRecure1($head){
        if($head == null){
            return ;
        }
    
        inOrderUnRecure1($head->left);
        echo $head->value." ";
        inOrderUnRecure1($head->right);
    }
    
    function inOrderUnRecure2($head){
        if($head == null){
            return false;
        }
    
        $stack = new SplStack();
    
        while(! $stack->isEmpty() || $head != null){
            if($head != null){
                $stack->push($head);
                $head = $head->left;
            }
            else{
                $head = $stack->pop();
                echo $head->value." ";
                $head = $head->right;
            }
        }
    }
    
    function posOrderUnRecure1($head){
        if($head == null){
            return ;
        }
    
        posOrderUnRecure1($head->left);
        posOrderUnRecure1($head->right);
        echo $head->value." ";
    
    }
    
    function posOrderUnRecure2($head){
        if($head == null){
            return ;
        }
    
        $stack1 = new SplStack();
        $stack2 = new SplStack();
        $stack1->push($head);
        while (! $stack1->isEmpty()){
            $head = $stack1->pop();
            $stack2->push($head);
            if($head->left != null){
                $stack1->push($head->left);
            }
            if($head->right != null){
                $stack1->push($head->right);
            }
        }
        while (! $stack2->isEmpty()){
            $head = $stack2->pop();
            echo $head->value." ";
    
        }
    }
    
    function posOrderUnRecure(&$head){
        if($head == null){
    
            return false;
        }
        $stack1 = new SplStack();
        $stack2 = new SplStack();
        $stack1->push($head);
    
        while (! $stack1->isEmpty()){
    
            $cur = $stack1->pop();
            $stack2->push($cur);
    
            if($cur->left != null){
                $stack1->push($cur->left);
    
            }
    
            if($cur->right != null){
                $stack1->push($cur->right);
    
            }
    
        }
        while(!$stack2->isEmpty()){
    
            echo ($stack2->pop()->value)." ";
        }
    }
    
    
    $head = new Node(5);
    $head->left = new Node(3);
    $head->right = new Node(8);
    $head->left->left = new Node(2);
    $head->left->right = new Node(4);
    $head->left->left->left = new Node(1);
    $head->right->left = new Node(7);
    $head->right->left->left = new Node(6);
    $head->right->right = new Node(10);
    $head->right->right->left = new Node(9);
    $head->right->right->right = new Node(11);
    
    preOrderUnRecure1($head); //结果: 5 3 2 1 4 8 7 6 10 9 11
    preOrderUnRecure2($head); //结果: 5 3 2 1 4 8 7 6 10 9 11
    
    inOrderUnRecure1($head);    //结果: 1 2 3 4 5 6 7 8 9 10 11
    inOrderUnRecure2($head);    //结果: 1 2 3 4 5 6 7 8 9 10 11
    
    posOrderUnRecure1($head);  //结果: 1 2 4 3 6 7 9 11 10 8 5
    posOrderUnRecure2($head);  //结果: 1 2 4 3 6 7 9 11 10 8 5
  • 相关阅读:
    过完年又是跳槽涨工资的好机会了,许多毕业生也正打算参与社会,希望我的经历能给你们带来点什么
    临时表dataTable 求助,
    C#代码与javaScript函数的相互调用(转)
    一些基本的GIS 和国土方面知识(来自网上,序号都没改)
    VS 2008 安装求助 麻烦看下 下班就取下来,感觉不适合放这
    有关环境 错误 的记录
    我公司在招人 AE的
    ARCgis server 安装与起步(转自冬儿的BLOG )
    pytorch入门之安装和配置
    python出现编码问题的原因及编码问题的解决
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/10053756.html
Copyright © 2011-2022 走看看