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):有子树的结点是其子树的根节点的父结点:子结点/孩子结点 ...
随机推荐
- Oracle中本行记录和上一行记录进行比较lead over 函数处理
遇到问题:多表关联查询,有一个要求是,同一保单号,对应多个投资产品Code.以及投资比例,每一个保单号有一个总的投资金额.要求同一保单号那一行,只有第一个总金额有值,剩下的code对应的总金额置空. ...
- RSA加密(跨平台通用的)
/// <summary> /// RSA加密 /// </summary> /// <param name="strPublickey">&l ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 推荐一个网站Stack Overflow
网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...
- 了解一下jsp
本着和大家共同学习jsp的原则,今天谈一谈jsp. 首先,JSP(全称JavaServer Pages)是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户 ...
- SpringMVC + Spring + MyBatis 学习笔记:为MyBatis增加打印SQL功能 (最简化配置)
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 1.以下jar包拷贝到 ...
- Protocol Buffer详解
1.Protocol Buffer 概念 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 ...
- cocos2d-js取不到cocostudio里面控件问题
var winSize = cc.director.getWinSize(); //add main node var mainNode = cc.Node.create(); ; mainNode. ...
- dom 侧栏分享
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- dom 关键字提示
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...