import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack; //structure of binary tree
class BiTree {
BiTree lchild;
BiTree rchild;
String data;
} public class BiTreeTest {
static Scanner scanner = new Scanner(System.in); // test case: a b c # # d e # g # # f # # #
static BiTree createBiTree(BiTree root) {
String data = scanner.next();
if (data.equals("#")) {
return null;
} else {
root = new BiTree();
root.data = data;
root.lchild = createBiTree(root.lchild);
root.rchild = createBiTree(root.rchild);
return root;
}
} // preOrder recursive traverse
static void preOrderRecur(BiTree root) {
if (root != null) {
System.out.print(root.data + " ");
preOrderRecur(root.lchild);
preOrderRecur(root.rchild);
}
} // inOrder recursive traverse
static void inOrderRecur(BiTree root) {
if (root != null) {
inOrderRecur(root.lchild);
System.out.print(root.data + " ");
inOrderRecur(root.rchild);
}
} // preOrder in non-recursive
static void preOrder(BiTree root) {
Stack<BiTree> stack = new Stack<BiTree>();
BiTree cur;
stack.push(root);
while (!stack.empty()) {
while ((cur = stack.peek()) != null) {
System.out.print(cur.data + " ");
stack.push(cur.lchild);
}
cur = stack.pop();
if (!stack.empty() && (cur = stack.pop()) != null) {
stack.push(cur.rchild);
}
}
} // inOrder in non-recursive
static void inOrder(BiTree root) {
Stack<BiTree> stack = new Stack<BiTree>();
BiTree cur;
stack.push(root);
while (!stack.empty()) {
while ((cur = stack.peek()) != null) {
stack.push(cur.lchild);
}
stack.pop();
if (!stack.empty() && (cur = stack.pop()) != null) {
System.out.print(cur.data + " ");
stack.push(cur.rchild);
}
}
} // level traverse,use LinkedList instead of queue data structure
static void levelTraverse(BiTree root) {
LinkedList<BiTree> list = new LinkedList<BiTree>();
BiTree cur;
list.add(root);
while (list.size() != 0) {
cur = list.removeFirst();
if (cur != null) {
System.out.print(cur.data + " ");
}
if (cur.lchild != null) {
list.add(cur.lchild);
}
if (cur.rchild != null) {
list.add(cur.rchild);
}
}
} public static void main(String[] args) {
BiTree root = null;
root = createBiTree(root);
// preOrderRecur(root);
// inOrderRecur(root);
// inOrder(root);
levelTraverse(root);
}
}

java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历的更多相关文章

  1. PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)

    前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次.要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历.中序遍历.后序遍历.具体说明如下: 前序遍 ...

  2. 二叉树遍历(前序、中序、后序)-Java实现

    一.前序遍历 访问顺序:先根节点,再左子树,最后右子树:上图的访问结果为:GDAFEMHZ. 1)递归实现 public void preOrderTraverse1(TreeNode root) { ...

  3. 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)

    前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. C语言实现链式二叉树静态创建,(先序遍历),(中序遍历),(后续遍历)

    #include <stdio.h>#include <stdlib.h> struct BTNode{ char data ; struct BTNode * pLchild ...

  6. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  7. lintcode 66.67.68 二叉树遍历(前序、中序、后序)

    AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode le ...

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

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

  9. 玩透二叉树(Binary-Tree)及前序(先序)、中序、后序【递归和非递归】遍历

    基础预热: 结点的度(Degree):结点的子树个数:树的度:树的所有结点中最大的度数:叶结点(Leaf):度为0的结点:父结点(Parent):有子树的结点是其子树的根节点的父结点:子结点/孩子结点 ...

随机推荐

  1. [转]linux之partprobe命令

    转自:http://www.7edown.com/edu/article/soft_48647_1.html linux上,在安装系统之后,可否创建分区并且在不重新启动机器的情况下系统能够识别这些分区 ...

  2. js document对象

    document对象可以通过多种方式获取: 最常见的一种情况是,你在文档的script脚本中直接使用document,这个document代表运行着该脚本的文档.(这个document和window. ...

  3. iOS学习笔记之回调(一)

    什么是回调 看了好多关于回调的解释的资料,一开始总觉得这个概念理解起来有点困难,可能是因为自己很少遇到这种类型的调用吧.探索良久之后,才算有点启发,下面是自己的一点理解. 我们知道,在OSI网络七层模 ...

  4. javascript --- 面向对象 --- 封装

    javascript中有原型对象和实例对象 如有疑问请参考:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_enca ...

  5. 八皇后问题 --- 递归解法 --- java代码

    八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上.八皇后 ...

  6. 使用FTP搭建YUM

    VSFTP搭建YUM源 1.安装FTP [root@FTP kel]# rpm -qa |grep vsftp vsftpd-2.2.2-6.el6_0.1.x86_64 首先需要安装的ftp软件为v ...

  7. SQL Server 2008 备份改进版

    1.Add compressing function with 7-Zip 2.With tool win.rar code so you can change it if you want USE ...

  8. Linux 权限相关

    Linux中,所有文件都有 三种权限:User ,Group,Other 三个文件: /etc/passwd :包括所有系统账号,一般用户身份和root信息 /etc/shadow :保存个人密码 / ...

  9. (转载)OC学习篇之---代理模式

    在前一篇文章我们介绍了OC中的协议的概念,这篇文章我们就来介绍一下OC中的代理模式,关于代理模式,如果还有同学不太清楚的话,就自己去补充知识了,这里就不做介绍了,这里只介绍OC中是如何实现代理模式的. ...

  10. [转]一些实用的图表Chart制作工具

    最近工作过程中需要用到前端一些JS框架,看到一篇博文就转过来备份使用,后续会再完善一些材料.   Flot   Flot一个纯javascript绘画库,基于jQuery开发.它能够在客户端根据任何数 ...