Java 非递归实现 二叉树的前中后遍历以及层级遍历
class MyBinaryTree<T> {
BinaryNode<T> root;
public MyBinaryTree() {
root = new BinaryNode<>();
}
/**
* 堆栈进行先序遍历
*
* @param ts
*/
public void preorderTraversal(T... ts) {
Stack<BinaryNode<T>> binaryStack = new Stack<>();
BinaryNode<T> temp = root;
while (!binaryStack.isEmpty() || temp != null) {
if (temp != null) {
// 处理
System.out.println(temp.data);
// 先序
if (temp != null) {
binaryStack.push(temp);
}
temp = temp.left;
} else {
temp = binaryStack.pop();
temp = temp.right != null ? temp.right : null;
}
}
}
/**
* 堆栈中序遍历
*
* @param ts
*/
public void inorderTraversal(T... ts) {
Stack<BinaryNode<T>> binaryStack = new Stack<>();
BinaryNode<T> temp = root;
while (!binaryStack.isEmpty() || temp != null) {
if (temp != null) {
binaryStack.push(temp);
temp = temp.left;
} else {
temp = binaryStack.pop();
// 处理
System.out.println(temp.data);
temp = temp.right != null ? temp.right : null;
}
}
}
/**
* 堆栈后序遍历
*
* @param ts
*/
public void postorderTraversal(T... ts) {
Stack<BinaryNode<T>> binaryStack = new Stack<>();
BinaryNode<T> temp = root;
BinaryNode<T> node = null;
while (!binaryStack.isEmpty() || temp != null) {
if (temp != null) {
binaryStack.push(temp);
temp = temp.left;
} else {
temp = binaryStack.pop();
if (temp.right == null || temp.right == node) {
// 处理
System.out.println(temp.data);
node = temp;
temp = null;
} else {
binaryStack.push(temp);
temp = temp.right;
binaryStack.push(temp);
temp = temp.left;
}
}
}
}
/**
* 层级遍历
*/
public void levelTraversal() {
BinaryNode<T> temp = root;
Queue<BinaryNode<T>> queue = new LinkedList<>();
queue.add(temp);
while(!queue.isEmpty()) {
temp = queue.poll();
// 处理
System.out.println(temp.data);
if(temp.left!=null) {
queue.add(temp.left);
}
if(temp.right!=null) {
queue.add(temp.right);
}
}
}
}
class BinaryNode<T> {
BinaryNode<T> left;
T data;
BinaryNode<T> right;
public BinaryNode() {
}
public BinaryNode(BinaryNode<T> left, T data, BinaryNode<T> right) {
this.left = left;
this.data = data;
this.right = right;
}
}
测试代码:
private MyBinaryTree<String> binaryTree;
@Before
public void setUp() {
binaryTree = new MyBinaryTree<>();
binaryTree.root.data = "A";
binaryTree.root.left = new BinaryNode<>(new BinaryNode<>(null, "D", null), "B", null);
binaryTree.root.right = new BinaryNode<>(new BinaryNode<>(null, "F", null), "C",
new BinaryNode<>(null, "E", null));
}
@Test
public void preorderTest() {
binaryTree.preorderTraversal();
System.out.println("---------------先序遍历------------------");
}
@Test
public void inorderTest() {
binaryTree.inorderTraversal();
System.out.println("---------------中序遍历------------------");
}
@Test
public void postorderTest() {
binaryTree.postorderTraversal();
System.out.println("---------------后序遍历------------------");
}
@Test
public void levelTest() {
binaryTree.levelTraversal();
System.out.println("---------------层级遍历------------------");
}
Java 非递归实现 二叉树的前中后遍历以及层级遍历的更多相关文章
- Java非递归的方式获取目录中所有文件(包括目录)
零.思路解析 对于给出的文件查看其下面的所有目录,将这个目录下的所有目录放入待遍历的目录集合中,每次取出该集合中的目录遍历,如果是目录再次放入该目录中进行遍历. 一.代码 /** * 非递归的方式获取 ...
- Qt实现 动态化遍历二叉树(前中后层次遍历)
binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...
- java实现二叉树的前中后遍历(递归和非递归)
这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- Binary Tree Traversal 二叉树的前中后序遍历
[抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...
- 数据结构-C语言递归实现树的前中后序遍历
#include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...
- 五二不休息,今天也学习,从JS执行栈角度图解递归以及二叉树的前、中、后遍历的底层差异
壹 ❀ 引 想必凡是接触过二叉树算法的同学,在刚上手那会,一定都经历过题目无从下手,甚至连题解都看不懂的痛苦.由于leetcode不方便调试,题目做错了也不知道错在哪里,最后无奈的cv答案后心里还不断 ...
- 二叉树前中后/层次遍历的递归与非递归形式(c++)
/* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if(pRoot==NULL) ...
- [C++] 非递归实现前中后序遍历二叉树
目录 前置技能 需求描述 binarytree.h 具体实现 binarytree.cpp main.cpp 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...
随机推荐
- python padas 学习
import matplotlib from pandas import DataFrame import numpy as np import pandas as pd import MySQLdb ...
- 交换排序-C#实现
交换排序包括:冒泡排序和快速排序 具体代码如下: 冒泡排序: /// <summary> /// 冒泡排序 /// 稳定性:稳定 /// 时间复杂度:O(n2) /// </summ ...
- Python 反射(reflection)
反射是指通过字符串映射或修改程序运行时的状态.属性.方法, 有以下4个方法 1.getattr(object, name, default = None) 根据字符串获取 obj 对象里对应 str ...
- linux配置Anaconda python集成环境
1.下载anaconda与安装 利用anaconda来配置python环境 如果你上面两步已经没有问题了,那么这一步可以省略. 如果你想简单一些,利用anaconda来配置python环境,那么直接从 ...
- pache tomcat慢速HTTP拒绝服务攻击安全问题解决办法
问题说明:HTTP协议的设计要求服务器在处理之前完全接收到请求.如果HTTP请求未完成,或者传输速率非常低,则服务器将保持其资源占用等待剩余的数据.如果服务器占用的资源太多,则会造成拒绝服务. 漏洞危 ...
- Inno Setup打包带有MSI文件的程序
[Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其他安装程序使用相同的AppId值. ; (生成新的GUID,点击 工具|在IDE中生成GUID.) AppId={{47A1 ...
- tcpdump+wireshark抓包分析
上一篇文章中,我们介绍了tcpdump如何抓包. tcpdump是命令行下便捷的抓包和分析工具,但使用方式不够友好, wireshark是带图形化界面的抓包和分析工具,操作简便,但需要主机有显示器. ...
- [转]C#调用C++dll
本文转载至http://www.cnblogs.com/ysharp/archive/2012/05/25/2517803.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问 ...
- 关于sql 索引
1.聚集索引一个表只能有一个,而非聚集索引有个表能有多个 2.聚集索引和非聚集索引的根本区别是表记录的排列顺序和与索引的排列顺序是否一致,其实理解起来非常简单,还是举字典的例子:如果按照拼音查询,那么 ...
- PRBS
PRBS是Pseudo Random Binary Sequence的缩写,即“伪随机二进制序列”的意思.PRBS码具有“随机”特性,是因为在PRBS码流中,二进制数“0”和“1”是随机出现的,但是它 ...