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

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

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. 记录一次mysql中自定义获取UUID的函数

    循环方式一: DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() ) begin ) ...

  2. Python——函数基础

    函数是什么 它相当于一个独立的代码块,可以被重复使用,如果需要增加一个功能或者修改一个功能,只需要,增加或者修改函数即可. 函数分类 内置函数 python解释器已经为我们定义好的参数,比如:len( ...

  3. Spring XML配置文件无法自动提示 eclipse中XML配置文件open with打开方式选择 XML Editor:注意它的编辑方式也是有两种的design和source

    双击XML配置文件,如果打开方式不正确 则如下图: 都是灰色显示,不会有自动提示,也不会有颜色标注 右击XML配置文件,选择打开方式为XML Editor,则会有颜色标注 如果此时没有自动提示 则要手 ...

  4. 【NOIP2017提高组模拟7.3】B

    树上路径统计,点分治解决. 统计一段区间,naive地用了set解决,这样的复杂度是O(nlog^2n)的 考场代码出了个问题,统计答案时找到了之前的最优答案,但是没有加上新的一段,导致60分 #in ...

  5. mariadb多源主从复制错误跳过.md

    mysql 的主从错误跳过和mariadb的多源主从复制错误跳过操作不同,请注意: 更改会话的default_master_connection变量 STOP SLAVE 'slave_account ...

  6. logback写日志

    https://blog.csdn.net/u010128608/article/details/76618263 https://blog.csdn.net/zhuyucheng123/articl ...

  7. 小试nginx日志分析xlog

    nginx配置: http { #...其他配置 log_format tpynormal '$remote_addr | [$time_local] | $host | "$request ...

  8. 用Python实现小说中的汉字频率统计

     环境: Python 3的代码,亲测可用. 思路: 是先把每个字符提出来放在列表里:再过滤掉其中的标点符号:最后用字典对某个字出现的频率进行累加. 扩展: 用处很多,稍微改改,既可以用来统计小说或文 ...

  9. Artwork Gym - 101550A 离线并查集

    题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...

  10. (转)减少oracle sql回表次数 提高SQL查询性能

    要写出高效的SQL,那么必须必须得清楚SQL执行路径,介绍如何提高SQL性能的文章很多,这里不再赘述,本人来谈谈如何从 减少SQL回表次数 来提高查询性能,因为回表将导致扫描更多的数据块. 我们大家都 ...