博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树的遍历学习
阅读量:5276 次
发布时间:2019-06-14

本文共 6935 字,大约阅读时间需要 23 分钟。

1.创建对象

 
package com.zl; public class TreeNode {
private String vlaue; private TreeNode leftTreeNode; private TreeNode rightTreeNode; public TreeNode(String vlaue, TreeNode leftTreeNode, TreeNode rightTreeNode) {
this.vlaue = vlaue; this.leftTreeNode = leftTreeNode; this.rightTreeNode = rightTreeNode; } public String getVlaue() {
return vlaue; } public void setVlaue(String vlaue) {
this.vlaue = vlaue; } public TreeNode getLeftTreeNode() {
return leftTreeNode; } public void setLeftTreeNode(TreeNode leftTreeNode) {
this.leftTreeNode = leftTreeNode; } public TreeNode getRightTreeNode() {
return rightTreeNode; } public void setRightTreeNode(TreeNode rightTreeNode) {
this.rightTreeNode = rightTreeNode; } }
 

 

2.遍历算法实现

 
package com.zl; import java.util.LinkedList; import java.util.Stack; public class BinaryTree {
/* 迭代方式 先序遍历二叉树 */ public static void preOrderIteration(TreeNode tree) {
Stack
stack = new Stack<>(); while (true) {
while (tree != null) {
System.out.printf(tree.getVlaue() + "\t"); stack.push(tree); tree = tree.getLeftTreeNode(); } if (stack.empty()) {
break; } TreeNode popTreeNode = stack.pop(); tree = popTreeNode.getRightTreeNode(); } } /* 迭代方式 中序遍历二叉树 */ public static void midOrderIteration(TreeNode tree) {
Stack
stack = new Stack<>(); while (true) {
while (tree != null) {
stack.push(tree); tree = tree.getLeftTreeNode(); } if (stack.empty()) {
break; } TreeNode popTreeNode = stack.pop(); System.out.printf(popTreeNode.getVlaue() + "\t"); tree = popTreeNode.getRightTreeNode(); } } /* 迭代方式 后序遍历二叉树 */ public static void postOrderIteration(TreeNode tree) {
TreeNode hasPrintTree = null; Stack
stack = new Stack<>(); while (true) {
while (tree != null) {
stack.push(tree); tree = tree.getLeftTreeNode(); } if (stack.empty()) {
break; } if (stack.lastElement().getRightTreeNode() == null) {
//1.右子树为空 TreeNode treeNode = stack.pop(); hasPrintTree = treeNode; System.out.print(treeNode.getVlaue() + "\t"); tree = null; } else if (stack.lastElement().getRightTreeNode() == hasPrintTree) {
//2.右子树不为空,且已经打印过了 TreeNode treeNode = stack.pop(); hasPrintTree = treeNode; System.out.print(treeNode.getVlaue() + "\t"); tree = null; } else {
//3.右子树不为空 没有打印过 TreeNode rightTreeNode = stack.lastElement().getRightTreeNode(); tree = rightTreeNode; } } } /* 迭代方式层序遍历二叉树 */ public static void levelOrderIteration(TreeNode tree) {
if (tree == null) return; LinkedList
treeNodeList = new LinkedList<>(); treeNodeList.add(tree); while (!treeNodeList.isEmpty()) {
TreeNode pushTree = treeNodeList.removeFirst(); System.out.print(pushTree.getVlaue() + "\t"); if (pushTree.getLeftTreeNode() != null) {
treeNodeList.addLast(pushTree.getLeftTreeNode()); } if (pushTree.getRightTreeNode() != null) {
treeNodeList.addLast(pushTree.getRightTreeNode()); } } } public static void main(String[] args) {
//测试二叉树形式 /* 1 / \ 2 3 / / \ 4 5 6 / \ 7 8 \ 9 */ //构建二叉树 TreeNode treeNode9 = new TreeNode("9", null, null); TreeNode treeNode4 = new TreeNode("4", null, null); TreeNode treeNode7 = new TreeNode("7", null, null); TreeNode treeNode6 = new TreeNode("6", null, null); TreeNode treeNode8 = new TreeNode("8", null, treeNode9); TreeNode treeNode2 = new TreeNode("2", treeNode4, null); TreeNode treeNode5 = new TreeNode("5", treeNode7, treeNode8); TreeNode treeNode3 = new TreeNode("3", treeNode5, treeNode6); TreeNode tree = new TreeNode("1", treeNode2, treeNode3); System.out.print("递归先序遍历tree:"); preOrderRecursive(tree); System.out.print(System.lineSeparator()); System.out.print("迭代先序遍历tree:"); preOrderIteration(tree); System.out.print(System.lineSeparator()); System.out.print(System.lineSeparator()); System.out.print("递归中序遍历tree:"); midOrderRecursive(tree); System.out.print(System.lineSeparator()); System.out.print("迭代中序遍历tree:"); midOrderIteration(tree); System.out.print(System.lineSeparator()); System.out.print(System.lineSeparator()); System.out.print("递归后序遍历tree:"); postOrderRecursive(tree); System.out.print(System.lineSeparator()); System.out.print("迭代后序遍历tree:"); postOrderIteration(tree); System.out.print(System.lineSeparator()); System.out.print(System.lineSeparator()); System.out.print("迭代层序遍历tree:"); levelOrderIteration(tree); System.out.print(System.lineSeparator()); System.out.print(System.lineSeparator()); } /* 递归方式先序遍历二叉树 */ public static void preOrderRecursive(TreeNode tree) {
if (null == tree) {
return; } else {
System.out.print(tree.getVlaue() + "\t"); preOrderRecursive(tree.getLeftTreeNode()); preOrderRecursive(tree.getRightTreeNode()); } } /* 递归方式中序遍历二叉树 */ public static void midOrderRecursive(TreeNode tree) {
if (null == tree) {
return; } else {
midOrderRecursive(tree.getLeftTreeNode()); System.out.print(tree.getVlaue() + "\t"); midOrderRecursive(tree.getRightTreeNode()); } } /* 递归方式后序遍历二叉树 */ public static void postOrderRecursive(TreeNode tree) {
if (null == tree) {
return; } else {
postOrderRecursive(tree.getLeftTreeNode()); postOrderRecursive(tree.getRightTreeNode()); System.out.print(tree.getVlaue() + "\t"); } } }
 

 

转载于:https://www.cnblogs.com/codeLei/p/10615687.html

你可能感兴趣的文章
Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
查看>>
php上传文件及头像预览
查看>>
大四java实习生的一些经历
查看>>
线程池的概念
查看>>
Oracle_Statspack性能诊断工具
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
如何快速掌握一门技术
查看>>
利用AMPScript获取Uber用户数据的访问权限
查看>>
vagrant 同时设置多个同步目录
查看>>
python接口自动化28-requests-html爬虫框架
查看>>
生成随机数的模板
查看>>