温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java实现二叉树遍历的三种方式

发布时间:2020-09-05 04:21:08 来源:脚本之家 阅读:153 作者:zhangbinu 栏目:编程语言

本文实例为大家分享了java实现二叉树遍历的具体代码,供大家参考,具体内容如下

二叉树如下:

java实现二叉树遍历的三种方式

遍历结果如下:

java实现二叉树遍历的三种方式

以下是实现代码:

package binTree;

import java.util.Stack;

/**
 * @author bin.zhang
 * @version 2017年8月29日 上午10:22:01
 */
public class BinTreeTraversal {
 public static void main(String[] args) {
  System.out.print("前序:");
  Traversal.preOrder();
  Traversal.preOrderRecursion(Traversal.createBinTree());
  System.out.print("中序:");
  Traversal.inOrder();
  Traversal.inOrderRecursion(Traversal.createBinTree());
  System.out.print("后序:");
  Traversal.postOrder();
  Traversal.postOrderRecursion(Traversal.createBinTree());
 }
}

/**
 * 节点数据结构
 * 
 * @author bin.zhang
 * @version 2017年8月30日 上午11:49:38
 */
class BinTreeNode {

 BinTreeNode() {
 }

 BinTreeNode(char data, int flag, BinTreeNode lchild, BinTreeNode rchild) {
  this.data = data;
  this.flag = flag;
  this.lchild = lchild;
  this.rchild = rchild;
 }

 char data;
 int flag;
 BinTreeNode lchild, rchild;

}

class Traversal {

 /**
  * 创建一棵二叉树
  * 
  * @author bin.zhang
  * @return 根节点
  */
 public static BinTreeNode createBinTree() {
  BinTreeNode R3 = new BinTreeNode('F', 0, null, null);
  BinTreeNode L2 = new BinTreeNode('D', 0, null, null);
  BinTreeNode R2 = new BinTreeNode('E', 0, null, R3);
  BinTreeNode L1 = new BinTreeNode('B', 0, L2, R2);
  BinTreeNode R1 = new BinTreeNode('C', 0, null, null);
  BinTreeNode T = new BinTreeNode('A', 0, L1, R1);
  return T;
 }

 // 前序
 public static void preOrder() {

  BinTreeNode p = createBinTree();

  Stack<BinTreeNode> stack = new Stack<BinTreeNode>();

  while (p != null || !stack.empty()) {
   if (p != null) {
    System.out.print(p.data);
    stack.push(p);
    p = p.lchild;
   }
   else {
    p = stack.pop();
    p = p.rchild;
   }
  }
  System.out.println();

 }

 // 前序递归
 public static void preOrderRecursion(BinTreeNode top) {
  if (top != null) {
   System.out.println(top.data);
   preOrderRecursion(top.lchild);
   preOrderRecursion(top.rchild);
  }
 }

 // 中序
 public static void inOrder() {

  BinTreeNode p = createBinTree();

  Stack<BinTreeNode> stack = new Stack<BinTreeNode>();

  while (p != null || !stack.empty()) {
   if (p != null) {
    stack.push(p);
    p = p.lchild;
   }
   else {
    p = stack.pop();
    System.out.print(p.data);
    p = p.rchild;
   }
  }
  System.out.println();
 }

 // 中序递归
 public static void inOrderRecursion(BinTreeNode top) {
  if (top != null) {
   inOrderRecursion(top.lchild);
   System.out.println(top.data);
   inOrderRecursion(top.rchild);
  }
 }

 // 后序
 public static void postOrder() {

  BinTreeNode p = createBinTree();

  Stack<BinTreeNode> stack = new Stack<BinTreeNode>(); // 初始化栈

  int mark = 1; // 转向标志
  while (p != null || !stack.empty()) { // 遍历
   if (p != null && mark != 0) {
    stack.push(p);
    p = p.lchild;
   }// 转向左子树
   else {
    p = stack.pop();
    p.flag++; // 退栈
    if (p.flag == 1) {
     stack.push(p);
     p = p.rchild;
     mark = 1;
    } // 转向右子树
    else if (p.flag == 2 && !stack.empty()) { // 输出结点
     System.out.print(p.data);
     mark = 0;
    }
    else if (p.flag == 2 && stack.empty()) { // 输出根结点并退出
     System.out.print(p.data);
     break;
    }
   } // if-else
  } // while
  System.out.println();
 }

 // 后序递归
 public static void postOrderRecursion(BinTreeNode top) {
  if (top != null) {
   postOrderRecursion(top.lchild);
   postOrderRecursion(top.rchild);
   System.out.println(top.data);
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI