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. 运行Python2.x程序报编码错误的解决办法-UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordina not in range(128)

    Python编码问题的终极解决方案:在python的Lib\site-packages文件夹下新建一个sitecustomize.py文件,输入: import sys sys.setdefaulte ...

  2. 我常用的VBS方法(QTP)

    这些是4年前在HP用QTP做自动化测试时候总结的一些,现在贴出来,说不准以后会不会用到 当初花了2天时间写的一个自动生成的Excel Report Public Function Report (st ...

  3. 提高CSS开发能力的技巧集

    1. 使用:not()给导航条添加间隔线 我们通常使用如下代码给导航条增加间隔线 /* add border */ .nav li { border-right: 1px solid #666; } ...

  4. spring BeanFactory概述

    BeanFactory是Spring提供的两种容器类型之一,它是基础的IoC容器,并提供完整的IoC服务支持.如果没有指定,默认采用延迟初始化策略.只有当客户端对象需要访问容器中的某个受管对象的时候, ...

  5. ansible定时任务模块和用户组模块使用

    接上篇,还是一些基础模块的使用,这里主要介绍的是系统模块的使用. 下面例子都进行过相关的实践,从而可以直接进行使用相关的命令. 3.用户模块的使用 用户模块主要用来管理用户账号和用户的属性(对远程主机 ...

  6. 修改ssh服务的默认端口

    修改ssh服务的默认端口 1.查看当前服务端口 一般ssh服务的默认端口为22端口,查看监听的端口用netstat,如下: [root@ansiblemoniter ~]# netstat -tnlp ...

  7. wpf4 文字 模糊 不清晰 解决方法

    在窗口或控件上设置字体属性就可以了,如下:<UserControl x:Class="..."             xmlns="http://schemas. ...

  8. Flex里的命名空间,fx、mx、s【转】

    Flex 4带给我们的,是全新的命名空间.了解这些命名空间必定是一件好事情.Flex 4有三个非常重要的命名空间,分别是: xmlns:fx=”http://ns.adobe.com/mxml/200 ...

  9. NServiceBus-网关和多站点分布

    多站点部署的企业的数量.净系统由于增加的挑战高可用性和用户要求更快的响应时间,服务器和数据访问更接近. RPC技术迅速陷入困境在这些环境中,使机器在同一个站点和远程站点看起来是一样的. 在这些情况下, ...

  10. HW7.12

    import java.util.Scanner; public class Solution { public static void main(String[] args) { double[] ...