用非递归方式实现二叉树先序便利

来源:互联网 发布:杭州java平均薪资 编辑:程序博客网 时间:2024/06/04 23:31

先序遍历:中、左、右

中序遍历:左、中、右

后序遍历:左、右、中


比如下面这科树

             1

        2       3

    4    5   6    7

 package com.sangfor.tree;      public class Node {      public int value;      public Node left;      public Node right;      public Node(int value) {          this.value = value;      }  } 
package com.sangfor.tree;import java.util.Stack;public class ForEachTree {    public static void main(String[] args) {    Node node1 = new Node(1);    Node node2 = new Node(2);    Node node3 = new Node(3);    Node node4 = new Node(4);    Node node5 = new Node(5);    Node node6 = new Node(6);    Node node7 = new Node(7);    node1.left = node2;    node1.right = node3;    node2.left = node4;    node2.right = node5;    node3.left = node6;    node3.right = node7;    System.out.println("前序列递归");    priOrder(node1);    System.out.println();    System.out.println("前序列非递归");    priUnRecur(node1);    System.out.println();    System.out.println("中序列递归");    inOrder(node1);    System.out.println();    System.out.println("后序列递归");    posOrder(node1);}        //非递归前序    public static void priUnRecur(Node head) {    if (head != null) { //这里一定要记得判断head是否为空,编程要严谨    Stack<Node> stack = new Stack<Node>();        stack.push(head);        while (!stack.isEmpty()) {            head = stack.pop();        System.out.print(head.value + "\t");        if (head.right != null){        stack.push(head.right);        }        if (head.left != null) {        stack.push(head.left);        }        }    } else {    System.out.println("tree is no data");        }    }        //前序列递归    public static void priOrder(Node head) {    if (head == null) {    return;    }    System.out.print(head.value+"\t");    priOrder(head.left);    priOrder(head.right);    }    //    //非中序列递归//    public static void inUncur(Node head) {//    if (head != null) {//    Stack<Node> stack = new Stack<Node>();//    stack.add(head);//    while (!stack.isEmpty()) {   //注意这个地方不是 stack != null//                head = stack.pop();//          while (head.left != null) {//          head = head.left;//          if (head.right != null) {//          stack.push(head.right);//          }//          stack.push(head);//          System.out.println(head.value + "\t");//       }//          ////          if (head.right != null) {////          stack.push(head.right);////          ////          }//    }//    } else {//    System.out.println("tree is no data");////            if (head.right != null) {////            Node newRight = head.right;////            while (newRight != null) {////                   ////            }////            }//    }//    }    //中序列递归    public static void inOrder(Node head) {    if (head == null) {    return;    }    inOrder(head.left);    System.out.print(head.value+"\t");    inOrder(head.right);    }        //后序列递归    public static void posOrder(Node head) {    if (head == null) {    return;    }    posOrder(head.left);    posOrder(head.right);    System.out.print(head.value+"\t");    }}

结果:

前序列递归1245367前序列非递归1245367中序列递归4251637后序列递归4526731



0 0
原创粉丝点击