概念性的东西,自行百度。

按照国际管理,直接上代码来分析。

1、Node节点类

package com.tree.thread;

/**
* Author: lihao
* Date:2017/8/30
* Description:ThreadBinaryTree Node
*/
public class Node
{
private int data;
private Node left;
private boolean leftIsThread; // 左孩子是否为线索
private Node right;
private boolean rightIsThread; // 右孩子是否为线索 public Node(int data)
{
this.data = data;
this.left = null;
this.leftIsThread = false;
this.right = null;
this.rightIsThread = false;
} public int getData()
{
return data;
} public void setData(int data)
{
this.data = data;
} public Node getLeft()
{
return left;
} public void setLeft(Node left)
{
this.left = left;
} public boolean isLeftIsThread()
{
return leftIsThread;
} public void setLeftIsThread(boolean leftIsThread)
{
this.leftIsThread = leftIsThread;
} public Node getRight()
{
return right;
} public void setRight(Node right)
{
this.right = right;
} public boolean isRightIsThread()
{
return rightIsThread;
} public void setRightIsThread(boolean rightIsThread)
{
this.rightIsThread = rightIsThread;
} @Override
public boolean equals(Object obj)
{
if (obj instanceof Node)
{
Node temp = (Node) obj;
if (temp.getData() == this.data)
{
return true;
}
}
return false;
} @Override
public int hashCode()
{
return super.hashCode() + this.data;
}
}

2、创建二叉树、二叉树中序线索化(线索化有3种,此处单讲中序)

package com.tree.thread;

public class ThreadTree {
private Node root; // 根节点
private Node pre = null; // 线索化的时候保存前驱 public ThreadTree() {
this.root = null;
this.pre = null;
} public ThreadTree(int[] data) {
this.pre = null;
this.root = createTree(data, 0); // 创建二叉树
} /**
* 创建二叉树
*/
public Node createTree(int[] data, int index) {
if (index >= data.length) {
return null;
}
Node node = new Node(data[index]);
node.setLeft(createTree(data, 2 * index + 1));
node.setRight(createTree(data, 2 * index + 2));
return node;
} /**
* 将以root为根节点的二叉树线索化 中序法
*/
public void inThread(Node root) {
if (root != null) {
inThread(root.getLeft()); // 线索化左孩子
if (null == root.getLeft()) // 左孩子为空
{
root.setLeftIsThread(true); // 将左孩子设置为线索
root.setLeft(pre);
}
if (pre != null && null == pre.getRight()) // 右孩子为空
{
pre.setRightIsThread(true);
pre.setRight(root);
}
pre = root; //每次将当前节点设置为pre
inThread(root.getRight()); // 线索化右孩子
}
} /**
* 中序遍历线索二叉树
*/
public void inThreadList(Node root) {
if (root == null) {
return;
}
//查找中序遍历的起始节点
while (root != null && !root.isLeftIsThread()) {
root = root.getLeft();
}
while (root != null) {
System.out.print(root.getData() + ",");
if (root.isRightIsThread()) // 如果右孩子是线索
{
root = root.getRight();
} else // 有右孩子
{
root = root.getRight();
while (root != null && !root.isLeftIsThread()) {
root = root.getLeft();
}
}
} } /**
* 中序遍历
*/
public void inList(Node root) {
if (root != null) {
inList(root.getLeft());
System.out.print(root.getData() + ",");
inList(root.getRight());
}
} public Node getRoot() {
return root;
} public void setRoot(Node root) {
this.root = root;
}
}













Java-线索二叉树的实现的更多相关文章

  1. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...

  2. 线索二叉树的理解和实现(Java)

    线索二叉树的基本概念 我们按某种方式对二叉树进行遍历,将二叉树中所有节点排序为一个线性序列,在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结 ...

  3. 【Java】 二叉树的遍历(递归与循环+层序遍历)

    在[Java] 大话数据结构(9) 树(二叉树.线索二叉树)一文中,已经实现了采用递归方法的前.中.后序遍历,本文补充了采用循环的实现方法.以及层序遍历并进行了一个总结. 递归实现 /* * 前序遍历 ...

  4. 数据结构《9》----Threaded Binary Tree 线索二叉树

    对于任意一棵节点数为 n 的二叉树,NULL 指针的数目为  n+1 , 线索树就是利用这些 "浪费" 了的指针的数据结构. Definition: "A binary ...

  5. 线索二叉树Threaded binary tree

    摘要   按照某种遍历方式对二叉树进行遍历,可以把二叉树中所有结点排序为一个线性序列.在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结点.这 ...

  6. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  7. 树和二叉树->线索二叉树

    文字描述 从二叉树的遍历可知,遍历二叉树的输出结果可看成一个线性队列,使得每个结点(除第一个和最后一个外)在这个线形队列中有且仅有一个前驱和一个后继.但是当采用二叉链表作为二叉树的存储结构时,只能得到 ...

  8. 图解中序遍历线索化二叉树,中序线索二叉树遍历,C\C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. 数据结构之线索二叉树——C语言实现

     线索二叉树操作 (1) 线索二叉树的表示:将每个节点中为空的做指针与右指针分别用于指针节点的前驱和后续,即可得到线索二叉树. (2) 分类:先序线索二叉树,中序线索二叉树,后续线索二叉树 (3) 增 ...

  10. 后序线索二叉树中查找结点*p的后继

    在后序线索二叉树中查找结点*p的后继: 1.若结点*p为根,则无后继:2.若结点*p为其双亲的右孩子,则其后继为其双亲:3.若结点*p为其双亲的左孩子,且双亲无右子女,则其后继为其双亲:4.若结点*p ...

随机推荐

  1. PhoneGap+JQuery Mobile移动应用开发学习笔记

    最近一直在学习使用PhoneGap+JQuery Mobile的开发框架开发Android应用,抛开这个框架的运行效率不说,暂且将使用中遇到的问题进行一下整理. 1.JS文件引用顺序 也许在进行web ...

  2. stringByAppendingString和stringByAppendingPathComponent

    NSString提供了两个拼串的方法: /** * @brief 简单的字符串拼接,头文件 NSString (NSStringExtensionMethods) * * @param aString ...

  3. 一个4年工作经验的java程序员的困惑,怎样才能能为一个架构师,请教大神

    一个4年工作经验的java程序员的困惑,怎样才能能为一个架构师 LZ本人想往架构师发展, 业余时间也会看一些书籍, 但是感觉没有头绪, 有些书看了,也没有地方实践 我做了4年的java开发, 在一个公 ...

  4. PHP计算两个日期相差的年月日时分秒

    $start_time = '2017-09-06 15:12:20'; $end_time = '2018-09-08 10:20:45'; get_time($start_time,$end_ti ...

  5. 万门大学Python零基础10天进阶班视频教程

    点击了解更多Python课程>>> 万门大学Python零基础10天进阶班视频教程 课程简介: 旨在通过两周的学习,让学生不仅能掌握python编程基础从而进行计算机程序的开发, 还 ...

  6. Python使用ORM控制MongoDB(MongoEngine)

    简介: MongoEngine是一个对象文档映射器(ODM),相当于一个基于SQL的对象关系映射器(ORM) pymongo来操作MongoDB数据库,但是直接把对于数据库的操作代码都写在脚本中,这会 ...

  7. LeetCode(190) Reverse Bits

    题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  8. Linux下ioctl函数理解

    一. 什么是ioctl ioctl是设备驱动程序中对设备的I/O通道进行管理的函数.所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率.马达的转速等等.它的调用个数如下: i ...

  9. Python之code对象与pyc文件(三)

    上一节:Python之code对象与pyc文件(二) 向pyc写入字符串 在了解Python如何将字符串写入到pyc文件的机制之前,我们先来了解一下结构体WFILE: marshal.c typede ...

  10. executing an update/delete query问题

    是因为在做SpringDataJpa更新和删除操作的时候Repository层没有加事务的注解,加上就行了: @Transactional @Query(value = "update ms ...