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. 密码嗅探工具dsniff

    密码嗅探工具dsniff   网络大量的服务都使用密码方式对使用者身份进行认证.如果使用非加密的方式传输,一旦数据被截获,就容易被嗅探到.Kali Linux预置了一款专用的密码嗅探工具dsniff. ...

  2. openvpn部署centos7

    [root@openvpn ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装包 yum upgrade yum i ...

  3. luogu P3383 【模板】线性筛素数

    题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...

  4. Android Studio NDK开发浅谈

    环境: Android Studio 1.1.0 NDK-r10d 1.新建项目--->包名:com.mxl.az.ndk 新建包含native方法的类:JniOperation.class p ...

  5. Java编程思想学习(五)----第5章:初始化与清理

    随着计算机革命的发展,“不安全”的编程方式已逐渐成为编程代价高昂的主因之一. C++引入了构造嚣(constructor)的概念,这是一个在创建对象时被自动调用的特殊方法.Java中也采用了构造器,并 ...

  6. [转]Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件

      Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件. 1.为了防止拖动ListView时,在列表末尾重复数据显示.需要加入 HashMap<In ...

  7. MySQL5.7添加授权账号及修改默认端口

    1.修改默认端口 打开配置文件 vim /etc/my.cnf 分别添加端口在client.mysql节点 [client] port=15099 [mysqld] port=15099 需要注意se ...

  8. 破解MyEclipse2015 stable3.0(亲测可用)

    整个破解过程最好断网: 1.安装好MyEclipse2015 stable3后,打开设置好工作目录后,退出.2.将plugins文件夹中的文件拷贝到myeclipse安装目录的plugins文件夹下, ...

  9. KVM虚拟机安装使用教程(Ubantu)

    背景: 公司在某电信机房有50台ubantu的实体机,机器配置的ip是192.168.100.x的ip,内存和cpu都是高端配置.假如哪些端口需要对外映射,就通过机房的防火墙完成端口映射. 100.1 ...

  10. Circuit level-shifts ac signals

    AC signals can emanate from many sources, and many of these sources are incompatible with the most p ...