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 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...
随机推荐
- 【java编程】java的关键字修饰符
一.transient java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持.换句话来说就是,用transient关键字标记的成员变量不参与序列化 ...
- Eamon 埃蒙
发售年份 1980 平台 AppleII 开发商 Donald Brown 类型 文字冒险 https://www.youtube.com/watch?v=uvZIxnIvRG8
- day061 cookie和session
一. cookie 1.cookie 的原理 工作原理是:浏览器访问服务端,带着一个空的cookie,然后由服务器产生内容, 浏览器收到相应后保存在本地:当浏览器再次访问时,浏览器会自动带上Cooki ...
- wireless
思科的AP分为胖AP和瘦AP,但其实只是AP中的Image不一样而已,硬件都是一样的,胖AP和瘦AP之间可以互相转换.即使你下单的时候下的是胖AP,拿到货要当瘦AP用,转换一下即可. [相互转换] 详 ...
- python中使用 C 类型的数组以及ctypes 的用法
Python 在 ctypes 中为我们提供了类似C语言的数据类型, 它的用途(我理解的)可能是: (1) 与 其他语言(如 C.Delphi 等)写的动态连接库DLL 进行交换数据,因为 pytho ...
- MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed
MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed 1.安装完MySQL设置root密码报错如下 [root@vm ...
- [ZZ] 深度学习三巨头之一来清华演讲了,你只需要知道这7点
深度学习三巨头之一来清华演讲了,你只需要知道这7点 http://wemedia.ifeng.com/10939074/wemedia.shtml Yann LeCun还提到了一项FAIR开发的,用于 ...
- [ZZ] MATLAB曲线拟合
MATLAB曲线拟合 http://blog.sina.com.cn/s/blog_5db2286f0100enlo.html MATLAB软件提供了基本的曲线拟合函数的命令: 多项式函数拟合: a ...
- hg (Mercurial)multiple heads (hg 多头)、撤销 commit,并保留修改
有时候 commit 后才意识到还未 pull,这个时候会有如下提示: wlan-0-182:mobile-v2 lixiumei$ hg pull -upulling from ssh://hg@b ...
- abaqus 帮助文档 Substructure(子结构) 理论
对于静态问题,可以缩减到只保留Retain Node的刚度矩阵和载荷矩阵: 但对于动力问题,还需要增加内部节点作为retain node,但这样会有点麻烦,更为常用的方式是保留子结构的模态和振型.