二叉树(二叉链表实现)JAVA代码

publicclassTest{
publicstaticvoid main(String[] args){
char[] ch =newchar[]{'A','B','D','#','#','G','#','#','C','J','#','#','M','#','#'};
BinaryTree binaryTree =newBinaryTree(ch);
binaryTree.preOrder();
System.out.println();
binaryTree.inOrder();
System.out.println();
binaryTree.postOrder();
System.out.println();
binaryTree.levelOrder();
System.out.println();
}
}
publicclassBinaryTree{
//二叉树结点
publicclassBiNode{
char data;
BiNode left;
BiNode right;
BiNode(char data,BiNode left,BiNode right){
this.data = data;
this.left = left;
this.right = right;
}
int flag =0;//供非递归后序遍历使用
}
//根结点
publicBiNode root;
privatestaticint i;
publicBinaryTree(char[] pre){
i =0;
root = create(pre);
}
//初始化(先序遍历顺序存放数组、'#'表示null)
privateBiNode create(char[] pre)
{
if(i < pre.length)
{
if(pre[i]=='#') //结点为空
{
i++;
return null;
}
BiNode p =newBiNode(pre[i], null, null); //结点非空
i++;
p.left = create(pre); //递归建立左子树
p.right = create(pre); //递归建立右子树
return p;
}
return null;
}
//先序遍历
publicvoid preOrder(){
System.out.print("preOrder traversal with recursion:");
preOrder(root);
System.out.println();
System.out.print("preOrder traversal without recursion:");
preOrder2(root);
System.out.println();
}
//递归
privatevoid preOrder(BiNode root){
if(root == null)return;
System.out.print(root.data); //访问结点
preOrder(root.left);
preOrder(root.right);
}
//非递归
privatevoid preOrder2(BiNode head){
LinkedList<BiNode> s =newLinkedList<BiNode>();
while(head != null ||!s.isEmpty())
{
while(head != null) //访问左子树
{
System.out.print(head.data); //访问左子树
s.push(head); //结点入栈(待后面找其右子树使用)= =(“递归”)
head = head.left;
}
if(!s.isEmpty()) //转向右子树
{
head = s.peek().right; //转向右子树
s.pop(); //结点出栈(已经找到其右子树)= =(“递归结束”)
}
}
}
//中序遍历
publicvoid inOrder(){
System.out.print("inOrder traversal with recursion:");
inOrder(root);
System.out.println();
System.out.print("inOrder traversal without recursion:");
inOrder2(root);
System.out.println();
}
//递归
privatevoid inOrder(BiNode root){
if(root == null)return;
inOrder(root.left);
System.out.print(root.data); //访问结点
inOrder(root.right);
}
//非递归
privatevoid inOrder2(BiNode head){
LinkedList<BiNode> s =newLinkedList<BiNode>();
while(head != null ||!s.isEmpty())
{
while(head != null) //左子树入栈
{
s.push(head); //结点入栈(待后面找其右子树使用)= =(“递归”)
head = head.left;
}
System.out.print(s.peek().data); //访问左子树
if(!s.isEmpty()) //转向右子树
{
head = s.peek().right; //转向右子树
s.pop(); //结点出栈(已经找到其右子树)= =(“递归结束”)
}
}
}
//后序遍历
publicvoid postOrder(){
System.out.print("postOrder traversal with recursion:");
postOrder(root);
System.out.println();
System.out.print("postOrder traversal without recursion:");
postOrder2(root);
System.out.println();
}
//递归
privatevoid postOrder(BiNode root){
if(root == null)return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data); //访问结点
}
//非递归
//后序遍历特点:递归左右子树后,还需访问结点:
//1、左子树入栈
//2、“两次出栈”(用flag标记模仿):第一次是为了找到左子树相应的右子树结点;第二次是为了访问结点
privatevoid postOrder2(BiNode head){
LinkedList<BiNode> s =newLinkedList<BiNode>();
while(head != null ||!s.isEmpty())
{
while(head != null) //左子树入栈
{
head.flag =1;
s.push(head); //结点连同flag入栈(待后面找其右子树使用)= =(“递归”)
head = head.left;
}
while(!s.isEmpty()&& s.peek().flag ==2) //若flag为2(已经找到其右子树出过一次栈),访问结点
{
System.out.print(s.peek().data); //访问结点元素
s.pop(); //(第二次“结点出栈”)实际结点出栈(已经访问结点元素)= =(“递归结束”)
}
if(!s.isEmpty()) //flag为1,转向右子树
{
head = s.peek().right; //转向右子树
s.peek().flag =2; //(第一次“flag模拟出栈”)标记为2,但实际结点不出栈(已经找到其右子树)
}
}
}
//层序遍历
publicvoid levelOrder(){
levelOrder(root);
}
privatevoid levelOrder(BiNode root){
LinkedList<BiNode>queue=newLinkedList<BiNode>(); //LinkedList实现了Queue接口
BiNode p = root;
while(p != null){
System.out.print(p.data); //访问结点
if(p.left != null)
queue.add(p.left);
if(p.right != null)
queue.add(p.right);
p =queue.poll(); //队头出队并返回为p
}
}
//在p结点后插入data
publicvoid insert(BiNode p,char data, boolean left){
if(p != null){
if(left) //插入位置为左孩子
p.left =newBiNode(data,p.left,null);
else //插入位置为右孩子
p.right =newBiNode(data,p.right,null);
}
}
//删除p的一个子树
publicvoiddelete(BiNode p, boolean left){
if(p != null){
if(left) //删除目标为左子树
p.left = null;
else //删除目标为右子树
p.right = null;
}
}
}
二叉树(二叉链表实现)JAVA代码的更多相关文章
- C#实现二叉树--二叉链表结构
二叉树的简单介绍 关于二叉树的介绍请看这里 : 二叉树的简单介绍 http://www.cnblogs.com/JiYF/p/7048785.html 二叉链表存储结构: 二叉树的链式存储结构是指,用 ...
- C语言递归实现二叉树(二叉链表)的三种遍历和销毁操作(实验)
今天写的是二叉树操作的实验,这个实验有三个部分: ①建立二叉树,采用二叉链表结构 ②先序.中序.后续遍历二叉树,输出节点值 ③销毁二叉树 二叉树的节点结构定义 typedef struct BiTNo ...
- 二叉树的二叉链表存储结构及C++实现
前言:存储二叉树的关键是如何表示结点之间的逻辑关系,也就是双亲和孩子之间的关系.在具体应用中,可能要求从任一结点能直接访问到它的孩子. 一.二叉链表 二叉树一般多采用二叉链表(binary linke ...
- c使用二叉链表创建二叉树遇到的一些疑问和思考
二叉链表存储二叉树 学习的时候参考的是<大话数据结构>,书中是这样定义的 typedef char TElemType; typedef struct BiTNode { TElemTyp ...
- 树(二叉树 & 二叉搜索树 & 哈夫曼树 & 字典树)
树:n(n>=0)个节点的有限集.有且只有一个root,子树的个数没有限制但互不相交.结点拥有的子树个数就是该结点的度(Degree).度为0的是叶结点,除根结点和叶结点,其他的是内部结点.结点 ...
- 编程算法 - 二叉搜索树 与 双向链表 代码(C++)
二叉搜索树 与 双向链表 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目:输入一颗二叉搜索树, 将该二叉搜索树转换成一个排序的双向链表. 要求 ...
- 【算法与数据结构】二叉搜索树的Java实现
为了更加深入了解二叉搜索树,博主自己用Java写了个二叉搜索树,有兴趣的同学可以一起探讨探讨. 首先,二叉搜索树是啥?它有什么用呢? 二叉搜索树, 也称二叉排序树,它的每个节点的数据结构为1个父节点指 ...
- 二叉搜索树 思想 JAVA实现
二叉搜索树:一棵二叉搜索树是以一棵二叉树来组织的,这样一棵树可以使用链表的数据结构来表示(也可以采用数组来实现).除了key和可能带有的其他数据外,每个节点还包含Left,Right,Parent,它 ...
- 二叉搜索树及java实现
二叉搜索树(Binary Search Tree) 二叉搜索树是二叉树的一种,是应用非常广泛的一种二叉树,英文简称为 BST 又被称为:二叉查找树.二叉排序树 任意一个节点的值都大于其左子树所有节 ...
- 二叉搜索树的java实现
转载请注明出处 一.概念 二叉搜索树也成二叉排序树,它有这么一个特点,某个节点,若其有两个子节点,则一定满足,左子节点值一定小于该节点值,右子节点值一定大于该节点值,对于非基本类型的比较,可以实现Co ...
随机推荐
- 【USACO 1.3.2】修理牛棚
[描述] 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜. 有些牛棚里有牛 ...
- UVA 10131 - Is Bigger Smarter? (动态规划)
Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. ...
- (转载)小课堂UI-有关配色的一个小技巧
- canvas新属性
lineCap默认值是butt,还有aquare,round 使用:context.lineCap="butt" lineJoin miter是默认 还可以是round,bevel ...
- php中const定义常量
const 常量 1.在定义时必须被初始值,2.前面不加任何修饰符3.变量名字母一般都大写4.常量可以被子类继承5.一个常量是属于一个类的,而不是某个对象的 作用:当某些值是固定不变的,就用const ...
- Js屏蔽键盘事件
<script> function KeyDown(){ //屏蔽鼠标右键.Ctrl+n.shift+F10.F5刷新.退格键 //alert(& ...
- 温故而知新 C++ 类型转换
C语言类型转换 在C语言里用到的类型转换方式,一般都是用强制类型转换,语法:(类型说明符)(表达式),例如: (float)a 把a转换为实型,(int)(x+y) 把x+y的结果转换为整型.C语言这 ...
- 倒水问题-->经典面试题目
题目详细: 有两个容器,容积分别为A升和B升,有无限多的水,现在需要C升水.我们还有一个足够大的水缸,足够容纳C升水.起初它是空的,我们只能往水缸里倒入水,而不能倒出.可以进行的操作是:把一个容器灌满 ...
- 【手机安全卫士01】项目Splash页面的开发与设计
首先建立一个安卓的项目,然后修改manifest.xml文件,修改应用程序的logo和显示名称,效果图如下: 对应的代码如下: <?xml version="1.0" enc ...
- 托管host
托管在googlecode的host https://smarthosts.googlecode.com/svn/trunk/hosts