java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
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建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历的更多相关文章
- PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次.要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历.中序遍历.后序遍历.具体说明如下: 前序遍 ...
- 二叉树遍历(前序、中序、后序)-Java实现
一.前序遍历 访问顺序:先根节点,再左子树,最后右子树:上图的访问结果为:GDAFEMHZ. 1)递归实现 public void preOrderTraverse1(TreeNode root) { ...
- 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)
前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- C语言实现链式二叉树静态创建,(先序遍历),(中序遍历),(后续遍历)
#include <stdio.h>#include <stdlib.h> struct BTNode{ char data ; struct BTNode * pLchild ...
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- lintcode 66.67.68 二叉树遍历(前序、中序、后序)
AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode le ...
- 递归/非递归----python深度遍历二叉树(前序遍历,中序遍历,后序遍历)
递归代码:递归实现很简单 '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- 玩透二叉树(Binary-Tree)及前序(先序)、中序、后序【递归和非递归】遍历
基础预热: 结点的度(Degree):结点的子树个数:树的度:树的所有结点中最大的度数:叶结点(Leaf):度为0的结点:父结点(Parent):有子树的结点是其子树的根节点的父结点:子结点/孩子结点 ...
随机推荐
- Java线程同步_1
Java线程同步_1 synchronized 该同步机制的的核心是同步监视器,任何对象都可以作为同步监视器,代码执行结束,或者程序调用了同步监视器的wait方法会导致释放同步监视器 synchron ...
- 前端技能汇总 Frontend Knowledge Structure
Frontend Knowledge Structure 项目起源 还记得@jayli 的这幅前端知识结构图么. 图片的形式具有诸多的不便.缺失源图的我们,无法为此图贡献些什么,随着时间的迁移,或许有 ...
- WebApi参数传递
c# webapi的参数传递方式:1.查询字符串(query string):2.内容主体(content body) 当然也有cookie或url部分或头部信息(header)等其它传方式,这里仅讨 ...
- easyui不提交window中的form表单数据
<form id="ff" method="post">, <div id="win" class="easyu ...
- Windows Server 2003单网卡搭建VPN
Windows Server 2003单网卡搭建VPN 1.打开[控制面板] --> [管理工具] --> [路由和远程访问] 2.鼠标右击你要管理的电脑 在弹出式菜单中选中[配置并启 ...
- cannot restore segment prot after reloc: Permission denied
编辑/etc/selinux/config,找到这段:# This file controls the state of SELinux on the system. # SELINUX= can t ...
- 邮件发送服务AWS SES,Mailgun以及SendCloud(转)
原文:http://www.l4zy.com/posts/aws_ses-mailgun-sendcloud.html 电子邮件这一已经诞生很多年的互联网基础服务并没有随着时间的推移而慢慢消亡,实际上 ...
- Type Encoding
[Type Encodings] The compiler encodes the return and argument types for each method in a character s ...
- Struts Hello World Example
In this tutorial we show you how to develop a hello world web application using classic Struts 1.3 f ...
- 2013年度Python Git工具
Pycoders周刊根据读者对周刊文章的点击数据,评选出了2013年最受关注的和Git相关的Python工具. git-workflow (github.com) 可视化你的 git 工作流程的工具, ...