数据结构 分别用递归和非递归方法实现二叉树先序,中序,后序遍历

来源:互联网 发布:javascript生成随机数 编辑:程序博客网 时间:2024/04/30 09:43

程序员代码面试指南(左程云)读书笔记

 第二章

分别用递归和非递归方法实现二叉树先序,中序,后序遍历
 题目:
   用递归和非递归方式,分别按照二叉树先序,中序,后序打印所有的节点。
package com.chen.Recur;

import java.util.Stack;

public class Recur {
  public static void main(String[] args) {

}
  //先序遍历递归方法
  static void preOrderRecur(Node head){
      if(head==null){
          return;
      }
      System.out.println(head.value+"");
      preOrderRecur(head.left);
      preOrderRecur(head.right);
  }
  //中序遍历递归方法
  static void inOrderRecur(Node head){
      if(head==null){
          return;
      }
      inOrderRecur(head.left);
      System.out.println(head.value+"");
      inOrderRecur(head.right);
  }
  //后序遍历递归方法
  static void posOrderRecur(Node head){
      if(head==null){
          return;

      }
      posOrderRecur(head.left);
      posOrderRecur(head.right);
      System.out.println(head.value+"");
  }

  //非递归方法实现先序遍历
  /*
   * 具体过程如下:
   * 1 申请一个新的栈,stack. 然后将头节点head压入栈中
   * 2 从stack中弹出栈顶节点,记为cur ,然后打印cur的值,再将cur的右孩子(不为空的话)先压入stack栈中,
   * 最后将cur的左孩子(不为空的话)压入stack栈中
   * 3  不断重负步骤2,知道栈为空,结束
   *    1
   *   / \
   *  2   3
   * / \  /  \
   * 4  5 6   7
   *  1  1入栈,弹出打印,然后3入栈,2入栈,stack:2 ,3
2 2弹出打印,5入栈,4入栈 stack :4 5 3
3 弹出4打印,4没有孩子 stack:5 3
4 弹出5打印 ,5没有孩子,stack:3
5 弹出3打印 ,7入栈,6入栈  ,stack:6 7
6 弹出6打印 , 6没有孩子,stack:7
7 弹出7打印 ,7没有孩子 stack 空。结束

   * */

  public static void preOrderUnRecur(Node head){
      System.out.println("pre-order:");
      if(head!=null){
          Stack<Node> stack=new Stack<Node>();
          stack.add(head);
          while(!stack.isEmpty()){
              head=stack.pop();
              System.err.print(head.value+"");
              if(head.right!=null){
                  stack.push(head.right);
              }
              if(head.left!=null){
                  stack.push(head.left);
              }
          }
      }
      System.out.println();
  }

//非递归方法实现中序遍历
  /*
   * 1  申请一个新的栈,记为stack, 初始时,令变量cur=head;
   * 2  先把cur节点压入栈中,对以cur节点为头的整棵树来说,依次把左边界压入栈中,即不停的令cur=cur.left 然后重复步骤2.
   * 3  不断重复步骤2,直到发现cur为空,此时从stack中弹出第一个节点,记为node,打印node的值,并且让node=node.right 
   *    然后继续重复步骤2
   * 4  当stack为空且cur为空时,停止整个过程   
   * */
  public static void inOrderUnRecur(Node head){
      System.out.println("in-order:");
      if(head!=null){
          Stack<Node> stack=new Stack<Node>();
          while(!stack.isEmpty() || head!=null){
              if(head!=null){
                  stack.push(head);
                  head=head.left;
              }else{
                  head=stack.pop();
                  System.out.print(head.value+"");
                  head=head.right;
              }
          }
      }
      System.out.println();
  }
//非递归方法实现中序遍历1
  /*1  申请一个栈s1 ,然后将头节点head压入s1中。
   *2  从s1中弹出头节点记为cur,然后依次将cur的左孩子和右孩子压入s1中。
   *3  在这个过程中,每一个从s1中弹出的节点都放进s2中。
   *4  不断重复2 3.直到s1为空,过程停止
   *5  从s2中依次弹出并打印节点,打印的顺序就是后序遍历的顺序
   * 
   * */

  public static void posOrderUnRecur1(Node head){
      System.out.println("post-order:");
      if(head!=null){
          Stack<Node> s1=new Stack<Node>();
          Stack<Node> s2=new Stack<Node>();
          s1.push(head);
          while(!s1.isEmpty()){
              head=s1.pop();
              s2.push(head);
              if(head.left!=null){
                  s1.push(head.left);
              }
              if(head.right!=null){
                  s1.push(head.right);
              }
              while(!s2.isEmpty()){
                  System.out.print(s2.pop().value+"");
              }
          }
      }
      System.out.println("");
  }
//非递归方法实现中序遍历2
  /*1  申请一个栈stack ,然后将头节点head压入stack中。同时设置两个变量h和c,在整个流程中,
   *   h代表最近一次弹出并打印的节点,c代表stack的栈顶点,初始时h为头节点,c为null.
   *   
   *2  每次令c等于当前stack的栈顶点,但是不从stack弹出,此时分三种情况
   *   a  如果c的左孩子不为null,并且h不等于c的左孩子,也不等于c的右孩子,则把c左孩子压入stack中,
   *3  在这个过程中,每一个从s1中弹出的节点都放进s2中。
   *4  不断重复2 3.直到s1为空,过程停止
   *5  从s2中依次弹出并打印节点,打印的顺序就是后序遍历的顺序
   * 
   * */

}

0 0
原创粉丝点击