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.  

    2. 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;
          }
      }

       

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