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

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

发布时间:2020-09-05 04:21:08

来源:脚本之家

阅读:74

作者:zhangbinu

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

二叉树如下:

f5a128731188ad7361b2400d5c56723a.png

遍历结果如下:

7f270306e6f893634cecfe409c7e9c0d.png

以下是实现代码:

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 stack = new Stack();

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 stack = new Stack();

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 stack = new Stack(); // 初始化栈

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);

}

}

}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值