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 非递归实现 二叉树的前中后遍历以及层级遍历的更多相关文章

  1. Java非递归的方式获取目录中所有文件(包括目录)

    零.思路解析 对于给出的文件查看其下面的所有目录,将这个目录下的所有目录放入待遍历的目录集合中,每次取出该集合中的目录遍历,如果是目录再次放入该目录中进行遍历. 一.代码 /** * 非递归的方式获取 ...

  2. Qt实现 动态化遍历二叉树(前中后层次遍历)

    binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...

  3. java实现二叉树的前中后遍历(递归和非递归)

    这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...

  4. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

  5. Binary Tree Traversal 二叉树的前中后序遍历

    [抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...

  6. 数据结构-C语言递归实现树的前中后序遍历

    #include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...

  7. 五二不休息,今天也学习,从JS执行栈角度图解递归以及二叉树的前、中、后遍历的底层差异

    壹 ❀ 引 想必凡是接触过二叉树算法的同学,在刚上手那会,一定都经历过题目无从下手,甚至连题解都看不懂的痛苦.由于leetcode不方便调试,题目做错了也不知道错在哪里,最后无奈的cv答案后心里还不断 ...

  8. 二叉树前中后/层次遍历的递归与非递归形式(c++)

    /* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if(pRoot==NULL) ...

  9. [C++] 非递归实现前中后序遍历二叉树

    目录 前置技能 需求描述 binarytree.h 具体实现 binarytree.cpp main.cpp 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...

随机推荐

  1. DedeCms 数据库类使用实例说明 mysql.php

    //dedecms的数据库操作类说明,非常实用,在二次开发中尤其重要.//引入common.inc.php文件require_once (dirname(__FILE__) . "/incl ...

  2. 插入排序-C#实现

    插入排序包括:直接插入排序和希尔排序. 具体代码如下: 直接插入排序: /// <summary> /// 直接插入排序 /// 适用于少量元素的排序 /// 稳定性:稳定 /// 时间复 ...

  3. Redis 单机部署

    参考文章: https://www.cnblogs.com/zy-303/p/10273167.html#_label0 https://blog.csdn.net/linyifan_/article ...

  4. 视频外同步信号研究---fvh

    视频外同步信号研究---fvh 一个时钟周期有两个edge,分别称为:(1)Leading edge=前一个边沿=第一个边沿,对于开始电压是1,那么就是1变成0的时候:对于开始电压是0,那么就是0变成 ...

  5. Thread.Abort 方法

    [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)] public void Abort() 在调用此方 ...

  6. navicat for mysql 注册码,简简单单,一个搞定(蔡军帅亲测有效)

    打开navicat for mysql接着打开帮助,选中注册, 把下面的复制上去就可以了 NAVH-WK6A-DMVK-DKW3 转载自:https://blog.csdn.net/qq_403845 ...

  7. [转][C#]ImageHelper

    { internal static class ImageHelper { public static Bitmap CloneBitmap(Image source) { if (source == ...

  8. MFC 单文档调用对话框

    1.插入新的Dialog,如下图: 2.修改ID位 IDD_XMB 3.在单文件的Menu 中选中需要链接的按键,右键添加处理程序,如下图所示,添加完成后,在项目的xxxview.cpp中会生成如下函 ...

  9. markdwon语法与Typora

    我也是在视频中看到别人的操作,不知道这是个什么玩意? 直到无意间看到了markdown,才意识到其他人用的是markdown. 当你看到一个人使用类似 ![blockchain](https://ss ...

  10. Ajax异步请求阻塞情况的解决办法(asp.net MVC Session锁的问题)

    讨论今天这个问题之前,我们先来看下浏览器公布的资源并发数限制个数,如下图 不难看出,目前主流浏览器支持都是最多6个并发 需要注意的是,浏览器的并发请求数目限制是针对同一域名的 意即,同一时间针对同一域 ...