• java线索二叉树(含线索二叉树代码展示)


    目录

    一:java线索二叉树的介绍

    二:java线索二叉树的展示

    三:java线索二叉树的案例

    四:java线索化二叉树代码展示

    4.1 二叉树的线索化代码

    4.2 中序遍历二叉树


    一:java线索二叉树的介绍

    线索二叉树的目的是去利用没有利用到的空指针

    n个结点有n+1个空指针

    对二叉树进行(先序、中序、后序的遍历)让其空指针指向前驱和后继的过程就是线索二叉树。

    二:java线索二叉树的展示

    中序遍历

    三:java线索二叉树的案例

    第一步:把普通的二叉树线索成线索二叉树

    第二步:输出线索二叉树

    四:java线索化二叉树代码展示

    4.1 二叉树的线索化代码

    1. //中序线索化二叉树
    2. public void clueNode(Node node) {
    3. if(node == null) {
    4. return;
    5. }
    6. clueNode(node.getLeft());
    7. if(node.getLeft() == null) {
    8. node.setLeft(pre);
    9. node.setLeftno(1);
    10. }
    11. if(pre != null && pre.getRight() == null) {
    12. pre.setRight(node);
    13. pre.setRightno(1);
    14. }
    15. pre = node;
    16. clueNode(node.getRight());
    17. }

    4.2 中序遍历二叉树

    1. //中序遍历二叉树
    2. public void preSelect() {
    3. Node node =root;
    4. while(node != null) {
    5. while(node.getLeftno() == 0) {
    6. node = node.getLeft();
    7. }
    8. System.out.println(node);
    9. while(node.getRightno() == 1) {
    10. node = node.getRight();
    11. System.out.print(node);
    12. }
    13. node = node.getRight();
    14. }
    15. }

    全部代码

    1. package tree;
    2. public class Node {
    3. private int no;
    4. private String name;
    5. private Node left;
    6. private Node right;
    7. private int leftno;
    8. private int rightno;
    9. public Node(int no, String name) {
    10. this.no = no;
    11. this.name = name;
    12. }
    13. public int getNo() {
    14. return no;
    15. }
    16. public void setNo(int no) {
    17. this.no = no;
    18. }
    19. public String getName() {
    20. return name;
    21. }
    22. public void setName(String name) {
    23. this.name = name;
    24. }
    25. public Node getLeft() {
    26. return left;
    27. }
    28. public void setLeft(Node left) {
    29. this.left = left;
    30. }
    31. public Node getRight() {
    32. return right;
    33. }
    34. public void setRight(Node right) {
    35. this.right = right;
    36. }
    37. public int getLeftno() {
    38. return leftno;
    39. }
    40. public void setLeftno(int leftno) {
    41. this.leftno = leftno;
    42. }
    43. public int getRightno() {
    44. return rightno;
    45. }
    46. public void setRightno(int rightno) {
    47. this.rightno = rightno;
    48. }
    49. @Override
    50. public String toString() {
    51. return "Node [no=" + no + ", name=" + name + "]";
    52. }
    53. public void preSelect() {
    54. System.out.println(this);
    55. if(this.getLeft() != null) {
    56. this.getLeft().preSelect();
    57. }
    58. if(this.getRight() != null) {
    59. this.getRight().preSelect();
    60. }
    61. }
    62. public void indexSelect() {
    63. if(this.getLeft() != null) {
    64. this.getLeft().indexSelect();
    65. }
    66. System.out.println(this);
    67. if(this.getRight() != null) {
    68. this.getRight().indexSelect();
    69. }
    70. }
    71. public void postSelect() {
    72. if(this.getLeft() != null) {
    73. this.getLeft().postSelect();
    74. }
    75. if(this.getRight() != null) {
    76. this.getRight().postSelect();
    77. }
    78. System.out.println(this);
    79. }
    80. public Node preId(int no) {
    81. if(this.getNo() == no) {
    82. return this;
    83. }
    84. Node node = null;
    85. if(this.getLeft() != null) {
    86. node = this.getLeft().preId(no);
    87. }
    88. if(node != null) {
    89. return node;
    90. }
    91. if(this.getRight() != null) {
    92. node = this.getRight().preId(no);
    93. }
    94. return node;
    95. }
    96. public Node indexId(int no) {
    97. Node node = null;
    98. if(this.getLeft() != null) {
    99. node = this.getLeft().indexId(no);
    100. }
    101. if(node != null) {
    102. return node;
    103. }
    104. if(this.getNo() == no) {
    105. return this;
    106. }
    107. if(this.getRight() != null) {
    108. node = this.getRight().indexId(no);
    109. }
    110. return node;
    111. }
    112. public Node postId(int no) {
    113. Node node = null;
    114. if(this.getLeft() != null) {
    115. node = this.getLeft().postId(no);
    116. }
    117. if(node != null) {
    118. return node;
    119. }
    120. if(this.getRight() != null) {
    121. node = this.getRight().postId(no);
    122. }
    123. if(node != null) {
    124. return node;
    125. }
    126. if(this.getNo() == no) {
    127. return this;
    128. }
    129. return node;
    130. }
    131. //删除
    132. public void delNode(int no) {
    133. if(this.getLeft().getNo() == no) {
    134. this.left = null;
    135. return;
    136. }
    137. if(this.getRight().getNo() == no) {
    138. this.right = null;
    139. return;
    140. }
    141. if(this.getLeft() != null) {
    142. this.left.delNode(no);
    143. }
    144. if(this.getRight() != null) {
    145. this.right.delNode(no);
    146. }
    147. }
    148. }
    1. package tree;
    2. public class ClueTree {
    3. private Node root;
    4. private Node pre;
    5. public void setRoot(Node node) {
    6. this.root = node;
    7. }
    8. public void clueNode() {
    9. this.clueNode(root);
    10. }
    11. //中序线索化二叉树
    12. public void clueNode(Node node) {
    13. if(node == null) {
    14. return;
    15. }
    16. clueNode(node.getLeft());
    17. if(node.getLeft() == null) {
    18. node.setLeft(pre);
    19. node.setLeftno(1);
    20. }
    21. if(pre != null && pre.getRight() == null) {
    22. pre.setRight(node);
    23. pre.setRightno(1);
    24. }
    25. pre = node;
    26. clueNode(node.getRight());
    27. }
    28. //中序遍历二叉树
    29. public void preSelect() {
    30. Node node =root;
    31. while(node != null) {
    32. while(node.getLeftno() == 0) {
    33. node = node.getLeft();
    34. }
    35. System.out.println(node);
    36. while(node.getRightno() == 1) {
    37. node = node.getRight();
    38. System.out.print(node);
    39. }
    40. node = node.getRight();
    41. }
    42. }
    43. }
    1. package tree;
    2. public class Test {
    3. public static void main(String[] args) {
    4. Node root = new Node(1,"吕布");
    5. Node node2 = new Node(3,"貂蝉");
    6. Node node3 = new Node(6,"曹操");
    7. Node node4 = new Node(8,"刘备");
    8. Node node5 = new Node(10,"关羽");
    9. Node node6 = new Node(14,"张飞");
    10. root.setLeft(node2);
    11. root.setRight(node3);
    12. node2.setLeft(node4);
    13. node2.setRight(node5);
    14. node3.setLeft(node6);
    15. ClueTree cl = new ClueTree();
    16. cl.setRoot(root);
    17. cl.clueNode();
    18. Node left = node5.getLeft();
    19. Node right = node5.getRight();
    20. System.out.println("前驱:" + left.getNo() + ",后继:" + right.getNo());
    21. cl.preSelect();
    22. }
    23. }

  • 相关阅读:
    MU editor IDE编辑器 二次开发记录与踩坑
    (三) CPU 性能测试 (CPU负载高对应的不同情况)
    [Android 四大组件] --- Activity
    深入浅出Java多线程(九):synchronized与锁
    马斯洛的的五层需求完美吗 不
    动态内存管理(malloc free calloc realloc)
    java-net-php-python-ssm高校建党管理信息系统计算机毕业设计程序
    声学相关词汇
    docker部署Jenkins(Jenkins+Gitlab+Maven实现CI/CD)
    TensorFlow之文本分类算法-2
  • 原文地址:https://blog.csdn.net/qq_56127002/article/details/126474983