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

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

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;
}
}













最新文章

  1. cessss
  2. xcode 插件管理工具
  3. JavaScript显示分页按钮
  4. 查看linux占用内存/CPU最多的进程
  5. hdu 4711 动态规划
  6. dbutils 执行sql返回的数据类型
  7. node.js 访问sql server的 node_modules “msnodesql"的安装编译方法
  8. C++中弱符号(弱引用)的意义及实例
  9. OD: Kernel Exploit - 1
  10. FragmentPagerAdapter与FragmentStatePagerAdapter差异
  11. perl 爬取某理财网站产品信息
  12. saiku安装方法总结
  13. c++中的 堆和栈
  14. ViewData ViewBag ViewModel
  15. HBase集群安装
  16. Hello World -- 第一篇博客
  17. 搭建基于Docker社区版的Kubernetes本地集群
  18. net core体系-web应用程序-4net core2.0大白话带你入门-10asp.net core session的使用
  19. JavaScript if 条件语句
  20. ccc切割刚体

热门文章

  1. JDK安装及环境变量配置详解
  2. cocos2dx lua 打印和保存日志
  3. python入门:从安装python开始
  4. 【Redis】DENIED Redis is running in protected mode
  5. 【Linux命令】nohup和&差异,查看进程和终止进程!
  6. GoF23种设计模式之行为型模式之状态模式
  7. nordic芯片开发——烧写方法记录
  8. Persona5
  9. Android兼容性测试CTS --环境搭建、测试执行、结果分析
  10. Makefile基础(三)