28_线索二叉树
一、代码实现
1.1 前期准备
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
HeroNode root = new HeroNode(1, "tom");
HeroNode node2 = new HeroNode(3, "jack");
HeroNode node3 = new HeroNode(6, "smith");
HeroNode node4 = new HeroNode(8, "mary");
HeroNode node5 = new HeroNode(10, "king");
HeroNode node6 = new HeroNode(14, "dim");
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
private int leftType;
private int rightType;
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
public int getLeftType() {
return leftType;
}
public void setLeftType(int leftType) {
this.leftType = leftType;
}
public int getRightType() {
return rightType;
}
public void setRightType(int rightType) {
this.rightType = rightType;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}
1.2 线索化二叉树
package no9_tree.threadedbinarytree;
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNodes();
HeroNode left = node5.getLeft();
System.out.println("10号结点的前驱结点是 = " + left);
HeroNode right = node5.getRight();
System.out.println("10号结点的后继结点是 = " + right);
}
}
class ThreadedBinaryTree{
private HeroNode root;
private HeroNode pre = null;
public void setRoot(HeroNode root) {
this.root = root;
}
public void threadedNodes(){
this.threadedNodes(root);
}
public void threadedNodes(HeroNode node){
if (node == null){
return;
}
threadedNodes(node.getLeft());
if (node.getLeft() == null){
node.setLeft(pre);
node.setLeftType(1);
}
if (pre != null && pre.getRight() == null){
pre.setRight(node);
pre.setRightType(1);
}
pre = node;
threadedNodes(node.getRight());
}
}
------------------------------
"D:\IntelliJ IDEA 2019.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3\lib\idea_rt.jar=5814:D:\IntelliJ IDEA 2019.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\Desktop\StudyAlgorithm\out\production\StudyAlgorithm;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-container\1.7.0.Alpha9\arquillian-junit-container-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-core\1.7.0.Alpha9\arquillian-junit-core-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-api\1.7.0.Alpha9\arquillian-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-api\1.7.0.Alpha9\arquillian-core-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-spi\1.7.0.Alpha9\arquillian-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-spi\1.7.0.Alpha9\arquillian-core-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-api\1.7.0.Alpha9\arquillian-container-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-api\1.2.6\shrinkwrap-api-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-api-base\2.0.0\shrinkwrap-descriptors-api-base-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-spi\1.7.0.Alpha9\arquillian-container-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-spi\1.7.0.Alpha9\arquillian-container-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-impl-base\1.7.0.Alpha9\arquillian-core-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-impl-base\1.7.0.Alpha9\arquillian-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-impl-base\1.7.0.Alpha9\arquillian-container-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-api\1.7.0.Alpha9\arquillian-config-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-impl-base\1.7.0.Alpha9\arquillian-config-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-spi\1.7.0.Alpha9\arquillian-config-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-spi\2.0.0\shrinkwrap-descriptors-spi-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-impl-base\1.7.0.Alpha9\arquillian-container-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-impl-base\1.2.6\shrinkwrap-impl-base-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-spi\1.2.6\shrinkwrap-spi-1.2.6.jar no9_tree.threadedbinarytree.ThreadedBinaryTreeDemo
10号结点的前驱结点是 = HeroNode{no=3, name='jack'}
10号结点的后继结点是 = HeroNode{no=1, name='tom'}
1.3 遍历线索化二叉树
package no9_tree.threadedbinarytree;
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
threadedBinaryTree.threadedList();
}
}
class ThreadedBinaryTree{
private HeroNode root;
private HeroNode pre = null;
public void setRoot(HeroNode root) {
this.root = root;
}
public void threadedNodes(){
this.threadedNodes(root);
}
public void threadedList(){
HeroNode node = root;
while (node != null){
while (node.getLeftType() == 0){
node = node.getLeft();
}
System.out.println(node);
while (node.getRightType() == 1){
node = node.getRight();
System.out.println(node);
}
node = node.getRight();
}
}
}
-------------------------
"D:\IntelliJ IDEA 2019.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3\lib\idea_rt.jar=5814:D:\IntelliJ IDEA 2019.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\Desktop\StudyAlgorithm\out\production\StudyAlgorithm;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-container\1.7.0.Alpha9\arquillian-junit-container-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-core\1.7.0.Alpha9\arquillian-junit-core-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-api\1.7.0.Alpha9\arquillian-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-api\1.7.0.Alpha9\arquillian-core-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-spi\1.7.0.Alpha9\arquillian-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-spi\1.7.0.Alpha9\arquillian-core-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-api\1.7.0.Alpha9\arquillian-container-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-api\1.2.6\shrinkwrap-api-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-api-base\2.0.0\shrinkwrap-descriptors-api-base-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-spi\1.7.0.Alpha9\arquillian-container-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-spi\1.7.0.Alpha9\arquillian-container-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-impl-base\1.7.0.Alpha9\arquillian-core-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-impl-base\1.7.0.Alpha9\arquillian-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-impl-base\1.7.0.Alpha9\arquillian-container-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-api\1.7.0.Alpha9\arquillian-config-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-impl-base\1.7.0.Alpha9\arquillian-config-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-spi\1.7.0.Alpha9\arquillian-config-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-spi\2.0.0\shrinkwrap-descriptors-spi-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-impl-base\1.7.0.Alpha9\arquillian-container-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-impl-base\1.2.6\shrinkwrap-impl-base-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-spi\1.2.6\shrinkwrap-spi-1.2.6.jar no9_tree.threadedbinarytree.ThreadedBinaryTreeDemo
HeroNode{no=8, name='mary'}
HeroNode{no=3, name='jack'}
HeroNode{no=10, name='king'}
HeroNode{no=1, name='tom'}
HeroNode{no=14, name='dim'}
HeroNode{no=6, name='smith'}