1. 程式人生 > 其它 >Java--演算法--二叉排序樹(BST)

Java--演算法--二叉排序樹(BST)

  1. 二叉排序樹的基本介紹:
  2. 二叉排序樹的遍歷和建立

    1. package com.model.tree;
      
      /**
       * @Description:測試類
       * @Author: 張紫韓
       * @Crete 2021/7/16 9:34
       * 二叉排序樹的建立與遍歷(Binary Sort tree)
       */
      public class TreeDemo08 {
          public static void main(String[] args) {
              BinarySortTree sortTree = new BinarySortTree();
              SortNode root 
      = new SortNode(0); SortNode node1 = new SortNode(1); SortNode node2 = new SortNode(2); SortNode node3 = new SortNode(3); sortTree.setRoot(root); sortTree.add(node1); sortTree.add(node2); sortTree.add(node3); sortTree.infixOrder(); } }
      class BinarySortTree{ private SortNode root; public void add(SortNode node){ if (root!=null) { root.add(node); }else { root=node; } } public void infixOrder(){ if (root!=null) { root.infixOrder(); }else { System.out.println(
      "樹為空"); } } public SortNode getRoot() { return root; } public void setRoot(SortNode root) { this.root = root; } } class SortNode{ private int value; private SortNode left; private SortNode right; public int getValue() { return value; } // 新增 public void add(SortNode node){ if (node==null){ return; } if (this.value< node.value){ if (this.right==null){ this.right= node; }else { this.right.add(node); } }else { if (this.left==null){ this.left=node; }else { this.left.add(node); } } } // 中序遍歷 public void infixOrder(){ if (this==null){ return; }else { if (this.left!=null){ this.left.infixOrder(); } System.out.println(this.toString()); if (this.right!=null){ this.right.infixOrder(); } } } @Override public String toString() { return "SortNode{" + "value=" + value + '}'; } public void setValue(int value) { this.value = value; } public SortNode getLeft() { return left; } public void setLeft(SortNode left) { this.left = left; } public SortNode getRight() { return right; } public void setRight(SortNode right) { this.right = right; } public SortNode() { } public SortNode(int value) { this.value = value; } }
  3. 二叉排序樹的節點刪除:
    1. package com.model.tree;
      
      /**
       * @Description:測試類
       * @Author: 張紫韓
       * @Crete 2021/7/16 9:34
       * 二叉排序樹的建立與遍歷(Binary Sort tree)
       */
      public class TreeDemo08 {
          public static void main(String[] args) {
              BinarySortTree sortTree = new BinarySortTree();
      //        SortNode root = new SortNode(0);
      //        SortNode node1 = new SortNode(1);
      //        SortNode node2 = new SortNode(2);
      //        SortNode node3 = new SortNode(3);
      //        SortNode node4 = new SortNode(-1);
      //        SortNode node5 = new SortNode(-2);
      //        sortTree.setRoot(root);
      //        sortTree.add(node2);
      //        sortTree.add(node1);
      //        sortTree.add(node3);
      //        sortTree.add(node4);
      //        sortTree.add(node5);
      //        sortTree.infixOrder();
      //        sortTree.delete(1);
      //        System.out.println("-----------------------");
      //        sortTree.infixOrder();
              int[] array={7,3,10,12,5,1,9,2};
              for (int i = 0; i < array.length; i++) {
                  sortTree.add(new SortNode(array[i]));
              }
              sortTree.infixOrder();
              System.out.println("------------------");
              sortTree.delete(2);
              sortTree.infixOrder();
      
      
      
          }
      
      }
      
      class BinarySortTree {
      
          private SortNode root;
      
          public void add(SortNode node) {
              if (root != null) {
                  root.add(node);
              } else {
                  root = node;
              }
          }
      
          public void infixOrder() {
              if (root != null) {
                  root.infixOrder();
              } else {
                  System.out.println("樹為空");
              }
          }
      
          public SortNode searchParent(int value) {
              if (root == null) {
                  return null;
              } else {
                  if (root.getValue() == value) {
                      return null;
                  } else {
                      return root.searchParent(value);
                  }
              }
          }
      
          public SortNode search(int value) {
              if (root == null) {
                  return null;
              } else {
                  if (root.getValue() == value) {
                      return root;
                  } else {
                      return root.search(value);
                  }
              }
          }
          public void delete(int value){
              if (root==null){
                  System.out.println("樹為空,無法進行刪除");
                  return;
              }else {
                  SortNode delNode = search(value);
                  SortNode parentNode = searchParent(value);
                  if (delNode!=null&&parentNode!=null){//要刪除的節點和他的父節點都存在
                      if (parentNode.getLeft()==delNode){//要刪除的節點為父節點的左節點
                          if (delNode.getLeft()==null&&delNode.getRight()==null){//要刪除節點的為葉子節點
                              parentNode.setLeft(null);
                          }else if (delNode.getLeft()==null&&delNode.getRight()!=null) {
                              parentNode.setLeft(delNode.getRight());
                          }else if (delNode.getLeft()!=null&&delNode.getRight()==null){
                              parentNode.setLeft(delNode.getLeft());
                          }else {//左右子樹都不為空,從右子樹中找到最小的一個放在要刪除的節點行即可
                              int temp = delNode.getRight().searchMin().getValue();
                              delete(temp);
                              delNode.setValue(temp);
                          }
                      }else {//要刪除的節點為父節點的右節點
                          if (delNode.getLeft()==null&&delNode.getRight()==null){//要刪除節點的為葉子節點
                              parentNode.setRight(null);
                          }else if (delNode.getLeft()==null&&delNode.getRight()!=null) {
                              parentNode.setRight(delNode.getRight());
                          }else if (delNode.getLeft()!=null&&delNode.getRight()==null){
                              parentNode.setRight(delNode.getLeft());
                          }else {//左右子樹都不為空,從右子樹中找到最小的一個放在要刪除的節點行即可
                              int temp = delNode.getRight().searchMin().getValue();
                              delete(temp);
                              delNode.setValue(temp);
                          }
                      }
                  }else if (parentNode==null&&delNode==root){//要刪除的節點存在,他的父節點不存在,接為 root節點
                      if (delNode.getLeft()==null&&delNode.getRight()==null){//要刪除節點的為葉子節點
                          root=null;
                      }else if (delNode.getLeft()==null&&delNode.getRight()!=null) {
                          root=delNode.getRight();
                      }else if (delNode.getLeft()!=null&&delNode.getRight()==null){
                        root=delNode.getLeft();
                      }else {//左右子樹都不為空,從右子樹中找到最小的一個放在要刪除的節點行即可
                          int temp = delNode.getRight().searchMin().getValue();
                          delete(temp);
                          root.setValue(temp);
                      }
                  }else {//兩個節點都為空
                      System.out.println("沒有此節點,無法刪除");
                      return;
                  }
              }
          }
      
          public SortNode getRoot() {
              return root;
          }
      
          public void setRoot(SortNode root) {
              this.root = root;
          }
      
      }
      
      class SortNode {
          private int value;
          private SortNode left;
          private SortNode right;
      
          public void delete(int value){
      
      
          }
      //    找到樹的最小值
          public SortNode searchMin(){
              if (this.left==null){
                  return this;
              }else {
                 return this.left.searchMin();
              }
          }
      
          //    查詢某個節點的父節點
          public SortNode searchParent(int value) {
              if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
                  return this;
              } else {
                  if (this.value <= value && this.right != null) {
                      return this.right.searchParent(value);
                  } else if (this.value > value && this.left != null) {
                      return this.left.searchParent(value);
                  } else {
                      return null;
                  }
              }
          }
          public SortNode searchParent02(int value) {
              if (this.left != null) {
                  if (this.left.value == value) {
                      return this;
                  } else {
                      if (this.value > value) {
                          return this.left.search(value);
                      } else {
                          return null;
                      }
                  }
              } else if (this.right != null) {
                  if (this.right.value == value) {
                      return this;
                  } else {
                      if (this.value <= value) {
                          return this.right.search(value);
                      } else {
                          return null;
                      }
                  }
              } else {
                  return null;
              }
          }
      
          //    通過 某個值查詢某個節點
          public SortNode search(int value) {
              if (value == this.value) {
                  return this;
              } else {
                  if (this.value > value) {
                      if (this.left == null) {
                          return null;
                      } else {
                          return this.left.search(value);
                      }
                  } else {
                      if (this.right == null) {
                          return null;
                      } else {
                          return this.right.search(value);
                      }
                  }
              }
          }
      
          //    新增
          public void add(SortNode node) {
              if (node == null) {
                  return;
              }
              if (this.value < node.value) {
                  if (this.right == null) {
                      this.right = node;
                  } else {
                      this.right.add(node);
                  }
              } else {
                  if (this.left == null) {
                      this.left = node;
                  } else {
                      this.left.add(node);
                  }
              }
          }
      
          //    中序遍歷
          public void infixOrder() {
              if (this == null) {
                  return;
              } else {
                  if (this.left != null) {
                      this.left.infixOrder();
                  }
                  System.out.println(this.toString());
                  if (this.right != null) {
                      this.right.infixOrder();
                  }
              }
          }
      
      
          @Override
          public String toString() {
              return "SortNode{" +
                      "value=" + value +
                      '}';
          }
      
          public int getValue() {
              return value;
          }
      
          public void setValue(int value) {
              this.value = value;
          }
      
          public SortNode getLeft() {
              return left;
          }
      
          public void setLeft(SortNode left) {
              this.left = left;
          }
      
          public SortNode getRight() {
              return right;
          }
      
          public void setRight(SortNode right) {
              this.right = right;
          }
      
          public SortNode() {
          }
      
          public SortNode(int value) {
              this.value = value;
          }
      }