二叉树遍历(Java)---前序遍历,中序遍历,后序遍历

什么是遍历二叉树?

遍历二叉树指的是按某种规律依次访问二叉树的每个节点,对二叉树的遍历过程就是将非线性结构的二叉树中的节点排列成线性序列的过程

遍历二叉树有哪几种方法?

如果采用链表来保存二叉树的节点,则有以下两种遍历方式。

深度优先遍历:这种遍历算法将先访问到树中最深层次的节点。
广度优先遍历:这种遍历算法将逐层访问每层的节点,广度优先遍历又被称为按层遍历。

对于深度优先算法而言,它又可分为以下三种:

先(前)序遍历,分三步:
(1)访问根节点
(2)递归遍历左子树
(3)递归遍历右子树
中序遍历
(1)递归遍历左子树
(2)访问根节点
(3)递归遍历右子树
后序遍历
(1)递归遍历左子树
(2)递归遍历右子树
(3)访问根节点
本文主要介绍深度优先算法下的三种遍历方式

package 遍历二叉树;
import java.util.*; //导入含有集合框架的包
public class Example2 { 
    static int SIZE=10;   //设定数组大小
    /*
     * 构建createArray方法生成大小为SIZE的随机数组
     * Math。random()方法返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
     * 返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。 
     */
    public static int[] createArray(){
        int[] shuzu=new int[10];
        for(int i=0;i<SIZE;i++){
             shuzu[i]=(int)(Math.random()*100);
          }

        return shuzu;
    }

    public static int[] array=createArray();  //将createArray方法的返回值赋给数组变量array
    private static List<Node> nodeList=null;   //将List实例对象初始化为空
    /*
     * 构建Node类
     * 分别定义其左子节点leftChild,右子节点rightChild
     */

    private static class Node{
        Node leftChild;
        Node rightChild;
        int data;
        Node(int newData){
            leftChild=null;
            rightChild=null;
            data=newData;

        }

    }
    /*
     * 构建完全二叉树
     */
    public void createBinTree(){
        nodeList=new LinkedList<Node>();
        System.out.println("原数组是:");
        for(int i=0;i<array.length;i++){
            System.out.print(array[i]+"  ");
        }

        for(int nodeIndex=0;nodeIndex<array.length;nodeIndex++){
            nodeList.add(new Node(array[nodeIndex]));
        }
        for(int parentIndex=0;parentIndex<array.length/2-1;parentIndex++){
            nodeList.get(parentIndex).leftChild=nodeList.get(parentIndex*2+1);
            nodeList.get(parentIndex).rightChild=nodeList.get(parentIndex*2+2);
        }
        int lastParentIndex=array.length/2-1;
        nodeList.get(lastParentIndex).leftChild=nodeList.get(lastParentIndex*2+1);

        if(array.length%2==1){
            nodeList.get(lastParentIndex).rightChild=nodeList.get(lastParentIndex*2+2);
        }
    }
    //前序遍历
    public static void preOrder(Node node){
        if( node==null) return;
        System.out.print (node.data+"  ");
        preOrder(node.leftChild);
        preOrder(node.rightChild);

    }
    //中序遍历
    private static void inOrder(Node node) {
        if(node==null)return;

        inOrder(node.leftChild);
        System.out.print(node.data+"  ");
        inOrder(node.rightChild);

    }
    //后序遍历
    private static void postOrder(Node node) {
        if(node==null)return;

        postOrder(node.leftChild);

        postOrder(node.rightChild);
        System.out.print(node.data+"  ");

    }
    public static void main(String[] args) {
        Example2 binTree=new Example2();
        binTree.createBinTree();
        Node root=nodeList.get(0);
        System.out.println();
        System.out.println("先序遍历是"+"   ");
        preOrder(root);
        System.out.println();
        System.out.println("中序遍历是"+"   ");
        inOrder(root);
        System.out.println();
        System.out.println("后序遍历是"+"   ");
        postOrder(root);
        System.out.println();
    }

}


算法的核心在什么地方??

结合本程序来看,算法的精髓有两点:
第一,构建完全二叉树部分,也就是程序中的createBinTree()方法,方法中首先将原数组中的每个元素按下表顺序填入空树的各个节点中,然后遍历所有存在孩子节点的节点(也就是前SIZE/2个节点),然后为各个父节点建立与子节点的联系,最后生成一个完全二叉树。
第二,遍历二叉树部分,将构建完成的完全二叉树按照递归的方式进行逐个访问,按照访问顺序将节点中的数据打印出来。

非递归遍历二叉树

前序遍历

package 前序遍历;

import java.util.Stack;

public class PreOrder {
    public void preOrder(Node head){
        Stack<Node> stack=new Stack();
        stack.push(head);
        while(stack!=null){
            head=stack.pop();
            System.out.print(head+" ");
            if(head.right!=null){
                stack.push(head.right);
            }
            if(head.left!=null){
                stack.push(head.left);
            }
        }
    }

}
class Node{
    public int info;
    public Node left;
    public Node right;
    public Node(int data){
        this.info=data;
    }

}

中序遍历

package 中序遍历;

import java.util.Stack;
/*
 * 编程思路:首先将首节点和所有左子节点压入栈中
 * 然后开始出栈,第一个出栈的是最左边的叶子节点,再出就是该叶子节点的父节点
 * 到这里,就要找该父节点的右子节点,这样便完整出栈了一个二叉树结构
 * 最后,按这个思路继续进行,便得到我们想要的结果
 */
public class InOrder {
       public void inOrder(Node head){
           if(head!=null){
               Stack<Node> stack=new Stack();
               while(!stack.isEmpty()||head!=null){
                   if(head!=null){
                       stack.push(head);
                       head=head.left;
                   }else{
                       head=stack.pop();
                       System.out.println(head.info+" ");
                       head=head.right;
                   }

               }
           }


       }
}
class Node{
    public int info;
    public Node left;
    public Node right;
    public Node(int data){
        this.info=data;
    }
}

后序遍历

package 后序遍历;

import java.util.Stack;
/*
 * s1为辅助堆栈,s2为真正的目标堆栈
 * 基本思路是后序遍历应该怎样输出,堆栈s2的元素就应该按后序遍历的逆序入栈
 * s1出栈顺序决定s2的入栈顺序,所以如何控制s1的出入栈是算法的核心
 */
public class PosOrder {
     public void posOrder(Node head){
         if(head!=null){
             Stack<Node> s1=new Stack();
             Stack<Node> s2=new Stack();
             s1.push(head);
             while(s1!=null){
                 head=s1.pop();
                 s2.push(head);
                 if(head.left!=null){
                     s1.push(head.left);
                 }
                 if(head.right!=null){
                     s1.push(head.right);
                 }
             }while(s2!=null){
                 System.out.print(s2.pop().info+" ");
             }

         }
     }
}
class Node{
    public int info;
    public Node left;
    public Node right;
    public Node(int data){
        this.info=data;
    }

}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值