java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html

在此基础上添加了非递归中序遍历二叉树:

二叉树类的代码:

package binarytree;

import linkedstack.LinkStack;
import linkqueue.LinkQueue; public class BinaryTree { class Node
{
public Object data;
public Node lchild;
public Node rchild; public Node(Object data)
{
this.data = data;
this.lchild = null;
this.rchild = null;
}
} //根节点
private Node root = null;
private Node node = null;
/**
* 创建树
*
* 以完全二叉树的格式来创建(子树不存在的用0填充),
* 对完全二叉树中每一个节点从0开始进行编号,
* 那么第i个节点的左孩子的编号为2*i+1,右孩子为2*i+2。
*
* */
void createTree(String strtree)
{
LinkQueue lQueue = new LinkQueue();
lQueue.initQueue();
/**
* 完全二叉树中第i层的结点的个数最多为第1到i-1层上所有节点的个数和
* 所以父节点的个数最多为N-1个,N表示节点个数
* */
for(int parentIndex =0; parentIndex<strtree.split(" ").length/2;parentIndex++)
{
if(root == null)
{
root= new Node(strtree.split(" ")[parentIndex]);
//左孩子
root.lchild = new Node(strtree.split(" ")[parentIndex*2+1]);
lQueue.enQueue(root.lchild);
//右孩子
root.rchild = new Node(strtree.split(" ")[parentIndex*2+2]);
lQueue.enQueue(root.rchild);
}else
{
if(!lQueue.isEmpty() && parentIndex*2+1<strtree.split(" ").length)//队列不空
{
node = (Node) lQueue.deQueue();
if(parentIndex*2+1<strtree.split(" ").length)
{
//左孩子
node.lchild = new Node(strtree.split(" ")[parentIndex*2+1]);
lQueue.enQueue(node.lchild);
}
if(parentIndex*2+2<strtree.split(" ").length)
{
//右孩子
node.rchild = new Node(strtree.split(" ")[parentIndex*2+2]);
lQueue.enQueue(node.rchild);
}
}else
{
return;
}
}
}
} /**
* 先序遍历二叉树
* */
void preOrderTraverse(Node node)
{
if(node == null)
{
return;
}
visit(node);
preOrderTraverse(node.lchild);
preOrderTraverse(node.rchild);
}
/**
* 中序遍历二叉树
* */
void inOrderTraverse(Node node)
{
if(node == null)
{
return;
}
inOrderTraverse(node.lchild);
visit(node);
inOrderTraverse(node.rchild);
} /**
* 非递归中序遍历二叉树
* */
void inOrderTraverse2(Node node)
{
if(node == null)
{
return;
}
LinkStack lStack = new LinkStack();
lStack.initStack();//初始化栈
Node p =node;
while(p!=null || !lStack.isEmpty())
{
if(p!=null)//遍历左子树,向左走到尽头,并将走过的节点入栈
{
lStack.push(p);
p=p.lchild;
}else//访问节点,向右走一步
{
p=(Node) lStack.pop();
visit(p);
p=p.rchild;
}
}
} /**
* 后序遍历二叉树
* */
void postOrderTraverse(Node node)
{
if(node == null)
{
return;
}
postOrderTraverse(node.lchild);
postOrderTraverse(node.rchild);
visit(node);
} /**
* 打印二叉树
* */
public void print()
{
System.out.print("先序遍历:");
preOrderTraverse(root);
System.out.print("\n中序遍历:");
inOrderTraverse(root);
System.out.print("\n非递归中序遍历:");
inOrderTraverse2(root);
System.out.print("\n后序遍历:");
postOrderTraverse(root); } /**
* 访问节点
* */
private void visit(Node node)
{
if(!node.data.equals("0"))
{
System.out.print(node.data);
}
}
}

  

测试类代码:

package binarytree;

public class BinaryTreeMain {

	public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
String strtree="- + / a * e f 0 0 b - 0 0 0 0 0 0 0 0 0 0 c d";//0表示没有值的位置
binaryTree.createTree(strtree);
binaryTree.print();
}
}

  

java创建二叉树并实现非递归中序遍历二叉树的更多相关文章

  1. LeetCode 94 | 基础题,如何不用递归中序遍历二叉树?

    今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的 ...

  2. java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历

    import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; //structure of binary ...

  3. LeetCode:二叉树的非递归中序遍历

    第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ /* inorder traversal binary tree */ #include <stdi ...

  4. Python实现二叉树的非递归中序遍历

    思路: 1. 使用一个栈保存结点(列表实现): 2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空: 3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树: 4. 栈不为空 ...

  5. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  6. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  7. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. YTU 2346: 中序遍历二叉树

    原文链接:https://www.dreamwings.cn/ytu2346/2606.html 2346: 中序遍历二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 12  解决: ...

  9. 递归/非递归----python深度遍历二叉树(前序遍历,中序遍历,后序遍历)

    递归代码:递归实现很简单 '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...

随机推荐

  1. Linux通过FTP上传文件到服务器

    1.如果没有安装ftp,可执行: 输入:yum -y install ftp,回车 等待安装完毕 2.连接服务器 输入:ftp 服务器IP,回车 根据提示输入用户名和密码 3.上传下载操作 1). 上 ...

  2. javascript面向对象思想

    JavaScript 使用函数来定义类.语法:function className(){    // 具体操作} function Person() { this.name=" 张三 &qu ...

  3. 【BZOJ 4665】 4665: 小w的喜糖 (DP+容斥)

    4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 94  Solved: 53 Description 废话不多说,反正小w要发喜 ...

  4. java 安全 技术

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 加密对应的类 是 Cipher ,意思 是加密的意思.这个类 在  javax.cryp ...

  5. 【BZOJ 2121】字符串游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=2121 dp,设\(f(i,j,k,l)\)表示原串i到j这个子串能否被删成第k个串的长度为l的前缀. ...

  6. 【多重背包小小的优化(。・∀・)ノ゙】BZOJ1531-[POI2005]Bank notes

    [题目大意] Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出 ...

  7. maven搭建企业级多模块项目

    1.创建一个maven项目 选择pom 完成 2.创建模块 项目右键选择module,创建模块.创建子模块 其余的打包时都为jar 地址:https://github.com/LeviFromCN/m ...

  8. C程序运行的背后(2)

    话说上回说到,C程序运行之前,必须要加载到其进程地址空间中.今儿咱就扯扯这个加载到底是怎么加载的. 一图胜前言,这个图简单说明了可执行文件加载过程的逻辑流,在此只做粗粒度概要说明.需要准确描述的,请出 ...

  9. WebSQL的基本使用过程

    1.创建或打开数据库(openDatabase) var db = openDatabase('dbname', '1.0', 'discription', 2 * 1024); // 目前测试只有C ...

  10. 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树

    C - 秋实大哥与快餐店 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 ...