Java--算法--线索化二叉树

  1. 线索化二叉树的介绍:
    1. 按照某种遍历方式顺序存放到数组中:[0,1,2,3,4,5,6]:则1的前驱节点时0后继节点时2
  2. 线索二叉树的线索化代码实现:

    1. package com.model.tree;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/14 9:39
       * 线索化二叉树的代码实现
       */
      public class TreeDemo03 {
          public static void main(String[] args) {
              HeroNode node1 = new HeroNode(1, "a");
              HeroNode node2 = new HeroNode(3, "b");
              HeroNode node3 = new HeroNode(6, "c");
              HeroNode node4 = new HeroNode(8, "d");
              HeroNode node5 = new HeroNode(10, "e");
              HeroNode node6 = new HeroNode(14, "f");
              node1.setLeft(node2);
              node1.setRight(node3);
              node2.setLeft(node4);
              node2.setRight(node5);
              node3.setLeft(node6);
              ThreadedBinaryTree tree = new ThreadedBinaryTree(node1);
              tree.threadedNode();
              System.out.println(node5.getLeft());
              System.out.println(node5.getRightType());
      
      
          }
      }
      
      class ThreadedBinaryTree {
          public HeroNode root;
          //第一个进行线索化的节点的前驱就是null
          private HeroNode pre=null;
      
          public ThreadedBinaryTree(HeroNode root) {
              this.root = root;
          }
          public ThreadedBinaryTree() {
          }
      
          public HeroNode getRoot() {
              return root;
          }
      
          public void setRoot(HeroNode root) {
              this.root = root;
          }
      
          public void threadedNode(){threadedNode(root);}
      //        编写中中序化线索树
      
          public void threadedNode(HeroNode node){
              if (node==null){
                  return;
              }else {
      //        线索化左子树
                  threadedNode(node.getLeft());
      //        线索化当前节点
                  if (node.getLeft()==null){
                      node.setLeft(pre);
                      node.setLeftType(1);
                  }
                  //当pre移动到node节点是,再对node的右指针进行处理
                  if (pre!=null&&pre.getRight()==null){
                      pre.setRight(node);
                      pre.setRightType(1);
                  }
                  pre=node;
      //        线索化右子树
                  threadedNode(node.getRight());
              }
          }
      
      
      
      
      }
      
      class HeroNode {
          private int id;
          private String name;
          private HeroNode left;
          private HeroNode right;
      
          //指定做指针事 是指向左右子树还是执行前驱或后继节点,如果是0则表示是字数如果是是1则表示指向前驱和后继
          private int leftType;
          private int rightType;
      
          public HeroNode(int id, String name, HeroNode left, HeroNode right) {
              this.id = id;
              this.name = name;
              this.left = left;
              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 "Node{" +
                      "id=" + id +
                      ", name='" + name + '\'' +
                      '}';
          }
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          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 HeroNode() {
          }
          public HeroNode(int id, String name) {
              this.id = id;
              this.name = name;
          }
      
          public void preOrder(){
              
          }
      
      }
  3. 线索化二叉树的遍历:

    1.  

    2. package com.model.tree;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/14 9:39
       * 线索化二叉树的代码实现
       */
      public class TreeDemo03 {
          public static void main(String[] args) {
              HeroNode node1 = new HeroNode(1, "a");
              HeroNode node2 = new HeroNode(3, "b");
              HeroNode node3 = new HeroNode(6, "c");
              HeroNode node4 = new HeroNode(8, "d");
              HeroNode node5 = new HeroNode(10, "e");
              HeroNode node6 = new HeroNode(14, "f");
              node1.setLeft(node2);
              node1.setRight(node3);
              node2.setLeft(node4);
              node2.setRight(node5);
              node3.setLeft(node6);
              ThreadedBinaryTree tree = new ThreadedBinaryTree(node1);
      //        中序线索化
              tree.threadedNode();
              System.out.println(node5.getLeft());
              System.out.println(node5.getRightType());
              tree.preOrder();
              System.out.println("---------------------");
              tree.preOrder03();
      
      //        先序线索化
      //
      //        tree.threadedInfix();
      //        System.out.println(node4.getLeft());
      //        System.out.println(node4.getRight());
      
      
          }
      }
      
      class ThreadedBinaryTree {
          public HeroNode root;
          //第一个进行线索化的节点的前驱就是null
          private HeroNode pre = null;
      
          public ThreadedBinaryTree(HeroNode root) {
              this.root = root;
          }
      
          public ThreadedBinaryTree() {
          }
      
          public HeroNode getRoot() {
              return root;
          }
      
          public void setRoot(HeroNode root) {
              this.root = root;
          }
      
          public void threadedNode() {
              threadedNode(root);
          }
      
      //    public void threadedInfix() {
      //        threadedInfix(root);
      //    }
      //        编写中中序化线索树
      
          public void threadedNode(HeroNode node) {
              if (node == null) {
                  return;
              } else {
      //        线索化左子树
                  threadedNode(node.getLeft());
      //        线索化当前节点
                  if (node.getLeft() == null) {
                      node.setLeft(pre);
                      node.setLeftType(1);
                  }
                  //当pre移动到node节点是,再对node的右指针进行处理
                  if (pre != null && pre.getRight() == null) {
                      pre.setRight(node);
                      pre.setRightType(1);
                  }
                  pre = node;
      //        线索化右子树
                  threadedNode(node.getRight());
              }
          }
      
      //    //    先序遍历线索化
      //    public void threadedInfix(HeroNode node) {
      //        if (node == null) {
      //            return;
      //        }
      ////        线索化当前节点
      //        if (node.getLeft() == null) {
      //            node.setLeft(pre);
      //            node.setLeftType(1);
      //        }
      //        if (pre != null && pre.getRight() == null) {
      //            pre.setRight(node);
      //            pre.setRightType(1);
      //        }
      //
      //        pre = node;
      ////       序列化左子树
      //        if (node.getLeft()!=null){threadedInfix(node.getLeft());}
      ////        序列化右子树
      //        if (node.getRight()!=null){threadedInfix(node.getRight());}
      //        
      //    }
      
          public void preOrder() {
              root.preOrder();
          }
      
          //利用线索化进行遍历《
      //    线索化二叉树,线索化的节点,指向的下一个节点就是中序遍历的下一个节点,
      //    但是如果没有线索化的节点,并不是中序遍历的节点
      //    我们可以根据中序遍历找到第一个节点,根据线索化,right 向后遍历,但是需要判断后面的是 线索化的还是普通的指向其右子树
      //    如果本节点的right 是线索化的我们可以直接 进行输出,因为他就是中序遍历的下一个节点,如果本节点的right指向的是其右子树
      //    我们子需要重新找到这个右子树的第一个节点,自此进行上述遍历
          public void preOrder02() {
              HeroNode temp = root;
      //       找到第一个节点,利用线索化向后一定,遍历所有的元素
              while (temp != null) {
                  while (temp.getLeftType() == 0) {
                      temp = temp.getLeft();
                  }
      //            找到第一个节点
                  System.out.println(temp);
      
                  while (temp.getRightType() == 1) {
                      temp = temp.getRight();
                      System.out.println(temp);
                  }
                  temp = temp.getRight();
              }
          }
      
          public void preOrder03() {
              HeroNode temp = root;
              while (temp != null) {
      //      1.得到这个树中序遍历的第一个节点(第一次是整个树,第二次就是某个节点的子树)
                  while (temp.getLeftType() == 0) {
                      temp = temp.getLeft();
                  }
      //       2.打印第一个节点
                  System.out.println(temp);
      //        3. 根据节点的线索化判断其右节点是其子树还是线索化的下一节点
                  while (temp.getRightType() == 1) {
      //                说明temp的右节点就是一个线索化的节点,就是中序遍历的下一节点
                      temp = temp.getRight();
                      System.out.println(temp);
                  }
      //         4.跳出循环说明其右指针指向的是一个右子树
                  temp = temp.getRight();//执行右子树重复上叙操作
      
              }
      
          }
      
      
      }
      
      class HeroNode {
          private int id;
          private String name;
          private HeroNode left;
          private HeroNode right;
      
          //指定做指针事 是指向左右子树还是执行前驱或后继节点,如果是0则表示是字数如果是是1则表示指向前驱和后继
          private int leftType;
          private int rightType;
      
          public HeroNode(int id, String name, HeroNode left, HeroNode right) {
              this.id = id;
              this.name = name;
              this.left = left;
              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 "Node{" +
                      "id=" + id +
                      ", name='" + name + '\'' +
                      '}';
          }
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          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 HeroNode() {
          }
      
          public HeroNode(int id, String name) {
              this.id = id;
              this.name = name;
          }
      
          public void preOrder() {
              if (this == null) {
                  return;
              } else {
                  if (this.left != null && this.leftType == 0) {
                      this.left.preOrder();
                  }
                  System.out.println(this);
                  if (this.right != null && this.leftType == 0) {
                      this.right.preOrder();
                  }
              }
      
          }
      
      }

       

        

        
posted @ 2021-07-14 16:10  张紫韩  阅读(85)  评论(0编辑  收藏  举报