• java 线索二叉树的构建


    1. public class test {
    2. public static void main(String[] args) {
    3. Node root = new Node(1);
    4. Node node2 = new Node(2);
    5. Node node3 = new Node(3);
    6. Node node4 = new Node(4);
    7. Node node5 = new Node(5);
    8. Node node6 = new Node(6);
    9. Tree tree = new Tree();
    10. tree.root = root;
    11. root.setLeft(node2);
    12. root.setRight(node3);
    13. node2.setLeft(node4);
    14. node2.setRight(node5);
    15. node3.setLeft(node6);
    16. tree.indixThread(root);
    17. tree.throughIndixThread(root);
    18. }
    19. }
    20. class Tree{
    21. // 自己的线索二叉树
    22. public Node root;
    23. public Node pre = null;
    24. public Tree(){
    25. }
    26. // 可以进行中序线索化和不使用递归的中序遍历
    27. public void indixThread(Node node){
    28. if(node==null){
    29. return ;
    30. // 退出
    31. }
    32. // 左中右
    33. indixThread(node.getLeft());
    34. // 来中 前 后
    35. if(node.getLeft()==null){
    36. node.setLeft(pre);
    37. node.setLefttype(1);
    38. }
    39. if(pre!=null&&pre.getRight()==null){
    40. pre.setRight(node);
    41. pre.setRighttype(1);
    42. }
    43. // 更新
    44. pre = node;
    45. // 后
    46. indixThread(node.getRight());
    47. }
    48. // 进行线索二叉树的中序遍历
    49. public void throughIndixThread(Node node){
    50. while(node!=null){
    51. while(node.getLefttype()==0){
    52. node = node.getLeft();
    53. }
    54. System.out.println(node);
    55. while(node.getRighttype()==1){
    56. node = node.getRight();
    57. System.out.println(node);
    58. }
    59. node = node.getRight();
    60. }
    61. }
    62. }
    63. class Node{
    64. // 节点类 需要val left right leftType rightType
    65. int val;
    66. Node left;
    67. Node right;
    68. @Override
    69. public String toString() {
    70. return "Node{" +
    71. "val=" + val +
    72. '}';
    73. }
    74. int lefttype;
    75. int righttype;
    76. public Node(int val) {
    77. this.val = val;
    78. }
    79. public int getVal() {
    80. return val;
    81. }
    82. public void setVal(int val) {
    83. this.val = val;
    84. }
    85. public Node getLeft() {
    86. return left;
    87. }
    88. public void setLeft(Node left) {
    89. this.left = left;
    90. }
    91. public Node getRight() {
    92. return right;
    93. }
    94. public void setRight(Node right) {
    95. this.right = right;
    96. }
    97. public int getLefttype() {
    98. return lefttype;
    99. }
    100. public void setLefttype(int lefttype) {
    101. this.lefttype = lefttype;
    102. }
    103. public int getRighttype() {
    104. return righttype;
    105. }
    106. public void setRighttype(int righttype) {
    107. this.righttype = righttype;
    108. }
    109. }

    线索二叉树是用二叉树中多余的 n+1 个指针来构建当前节点的前序和后继的过程 下面我来解释一下如何从普通的二叉树构建成线索二叉树以及不使用递归来进行线索二叉树的中序遍历

    1. public void indixThread(Node node){
    2. if(node==null){
    3. return ;
    4. // 退出
    5. }
    6. // 左中右
    7. indixThread(node.getLeft());
    8. // 来中 前 后
    9. if(node.getLeft()==null){
    10. node.setLeft(pre);
    11. node.setLefttype(1);
    12. }
    13. if(pre!=null&&pre.getRight()==null){
    14. pre.setRight(node);
    15. pre.setRighttype(1);
    16. }
    17. // 更新
    18. pre = node;
    19. // 后
    20. indixThread(node.getRight());
    21. }

    我们的tree类定义了一个pre 这个就是当前节点按照遍历的顺序的前一个节点 当我们遍历到第一个元素的时候 它是没有前一个节点的 因为它是遍历的第一个 所以它的前一个节点就是null 

    我们初始化pre的时候就为null

    我们有了node 当前节点和 pre上一个节点 就可以判断能不能搭桥

    // 由于是中序遍历 我们需要先左 再中右

    indixThread(node.left);

    if(node.left==null) 当前节点没有左指针 就利用这个节点指向前一个节点

    node.setLeft(pre)

    在判断前一个节点

    if(pre!=null&&pre.right==null){

            pre.setRight(node)
    }

    这时 我们正式完成了指针的利用 需要进入到下一个节点 所以就需要更新pre的值

    pre = node;

    最后 进入到右子树进行遍历

    indixThread(node.right);

     

    1. public void throughIndixThread(Node node){
    2. while(node!=null){
    3. while(node.getLefttype()==0){
    4. node = node.getLeft();
    5. }
    6. System.out.println(node);
    7. while(node.getRighttype()==1){
    8. node = node.getRight();
    9. System.out.println(node);
    10. }
    11. node = node.getRight();
    12. }
    13. }

    进行线索二叉树的无递归遍历

    我们首先要找到第一个元素 由于是中序遍历 所以它的特点是在左子树第一个leftType为1的地方

    因为我们设置它的前序是null 

    使用while来寻找 直到找到 打印出这个节点

    当right的type为1时 right就是下一个节点 我们可以直接打印

    while(node.getRightType()==1){

    node = node.right();

    sout(node);

    }        

    当出循环的时候 就来到了中间的节点 因为它本来就有右子树 所以righttype为0

    我们需要进入右子树再进行中序遍历的逻辑 所以

    node = node.getRight(); 此时停手 进入下一轮循环

    相当于进入右子树后在根据中序遍历的逻辑进行判断.

  • 相关阅读:
    嵌入式笔试面试刷题(day11)
    python--由wrfouput的数据计算位势涡度,并插值到指定压力层
    怎么查找和自己专业有关的英文文献?
    ubutu18.04 使用update-alternatives 对python和python3进行版本管理
    MongoDB基础运维
    什么是零日攻击?
    Mybatis-注解开发
    B03_02可转债转股
    05704-A-0145 HONEYWELL 将autoML技术应用于预训练的模型
    有些东西你要是早点懂,也不至于走那么多冤枉路
  • 原文地址:https://blog.csdn.net/wantsnowfly/article/details/127849232