Java-线索二叉树的实现
来源:互联网 发布:淘宝网店保证金怎么退 编辑:程序博客网 时间:2024/06/03 22:41
概念性的东西,自行百度。
按照国际管理,直接上代码来分析。
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; }}
阅读全文
0 0
- Java-线索二叉树的实现
- Java实现线索二叉树
- 线索二叉树的实现
- 线索二叉树的实现
- 线索二叉树的实现
- 线索二叉树的实现
- 线索二叉树的实现
- 线索二叉树的实现
- 线索二叉树的实现
- 线索二叉树的实现
- (学习java)线索二叉树的实现以及遍历
- 线索化二叉查找树 java实现
- 线索二叉树及其Java代码实现
- 中序线索二叉树Java实现
- 线索二叉树实现
- 线索二叉树实现
- 二叉线索树实现
- 中序线索二叉树的实现
- 正睿OI noip2017冲刺 第一次考试T2 解题报告
- 华为机试(字符串最后一个单词长度,计算字符串的个数,明明的随机数) Java实现
- 实现秒表功能 : 显示时,分,秒 , 点击按钮开始计时
- Android启动过程深入解析
- js简介
- Java-线索二叉树的实现
- 点击圆环颜色改变背景颜色
- sql server 关于表数据的插入
- 1101 第1课
- [cvpr2017]Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
- 十进制大数的加法运算
- 避免函数中过深的耦合
- main函数之前做的工作
- Codeforces #430 dv.2 842A ,842B