美文网首页
简单的二分搜索树-BST

简单的二分搜索树-BST

作者: 与诗小睡 | 来源:发表于2022-04-04 09:51 被阅读0次
package demo;

import java.util.LinkedList;

/**
 * 二分查找树
 * 
 * @author Administrator
 *
 * @param <K>
 * @param <V>
 */
public class BST<K extends Comparable<K>, V> {

    private final static Node EmptyNode = null;
    private Node head;
    private int count;

    private static class Node<K extends Comparable<K>, V> {
        K key;
        V value;
        Node<K, V> left;
        Node<K, V> right;

        private Node(K key, V val) {
            this.key = key;
            this.value = val;
            this.left = EmptyNode;
            this.right = EmptyNode;
        }

        
        private Node(Node<K,V> node) {
            this.key = node.key;
            this.value = node.value;
            this.left = node.left;
            this.right = node.right;
        }
        
        
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("{ ").append(key).append(":").append(value).append(" }");
            return sb.toString();

        }

    }

    public BST() {
        head = EmptyNode;
        count = 0;
    }

    public int size() {
        return count;
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public void insert(K key, V val) {
        checkArg(key, "key is null");
        head = insert(head, key, val);

    }

    private Node<K, V> insert(Node<K, V> root, K key, V val) {
        if (root == EmptyNode) {
            count++;
            return new Node<K, V>(key, val);
        }
        if (root.key.compareTo(key) > 0)
            root.left = insert(root.left, key, val);
        else if (root.key.compareTo(key) < 0)
            root.right = insert(root.right, key, val);
        else
            root.value = val;
        return root;
    }

    public V search(K key) {
        return (V) search(head, key);
    }

    private V search(Node<K, V> root, K key) {
        checkArg(key, "key is null");
        if (root == EmptyNode)
            return null;

        if (root.key.compareTo(key) == 0)
            return root.value;

        else if (root.key.compareTo(key) > 0)
            return (V) search(root.left, key);
        else
            return (V) search(root.right, key);
    }

    public void preOrder() {
        preOrder(head);
    }

    private void preOrder(Node root) {
        if (root == EmptyNode)
            return;
        System.out.print(root);
        preOrder(root.left);
        preOrder(root.right);
    }

    public void midOrder() {
        midOrder(head);
    }

    private void midOrder(Node root) {

        if (root == EmptyNode)
            return;
        midOrder(root.left);
        System.out.print(root);
        midOrder(root.right);

    }

    public void postOrder() {
        postOrder(head);
    }

    private void postOrder(Node root) {
        if (root == EmptyNode)
            return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root);
    }

    private void checkArg(Object key, String thrStr) {
        if (key == null)
            throw new IllegalArgumentException(thrStr);
    }

    public void levelOrder() {
        if (head == EmptyNode)
            return;
        LinkedList<Node> ll = new LinkedList();
        ll.addLast(head);
        while (!ll.isEmpty()) {
            Node node = ll.removeFirst();
            System.out.println(node);
            if (node.left != EmptyNode)
                ll.addLast(node.left);
            if (node.right != EmptyNode)
                ll.addLast(node.right);
        }
    }

    public K getMinNode() {
        checkArg(head, "this tree is empty!!");
        return (K) getMinNode(head).key;

    }

    private Node getMinNode(Node root) {
        if (root.left == EmptyNode)
            return root;
        return getMinNode(root.left);
    }

    public K getMaxNode() {
        checkArg(head, "this tree is empty!!");
        return (K) getMaxNode(head).key;
    }

    private Node<K, V> getMaxNode(Node<K, V> root) {
        if (root.right == EmptyNode)
            return root;
        return getMaxNode(root.right);
    }

    public void removeMin() {
        checkArg(head, "the tree is empty");
        head = removeMin(head);
    }

    private Node removeMin(Node root) {
        if (root.left == EmptyNode) {
            Node rightNode = root.right;
            root = EmptyNode;
            count--;
            return rightNode;
        }
        root.left = removeMin(root.left);
        return root;
    }

    public void removeMax() {
        checkArg(head, "the tree is empty");
        head = removeMax(head);
    }

    private Node removeMax(Node root) {
        if (root.right == EmptyNode) {
            Node leftNode = root.left;
            root = null;
            count--;
            return leftNode;
        }
        root.right = removeMax(root.right);
        return root;
    }

    public void remove(K key) {
        head = remove(head, key);
    }

    private Node remove(Node root, K key) {
         if(root ==EmptyNode) return null;
        if (root.key.compareTo(key) < 0) {//
            root.right = remove(root.right, key);
            return root;
        } else if (root.key.compareTo(key) > 0) {
            root.left = remove(root.left, key);
            return root;
        } else {// equal
            if(root.left ==EmptyNode) {//没有左节点
                
                Node rightNode = root.right;
                root=EmptyNode;
                count--;
                return rightNode;
                
            }
            if(root.right == EmptyNode) {//没有右节点
                Node leftNode = root.right;
                root=EmptyNode;
                count--;
                return leftNode;
            }
            //左右节点都存在
            Node min = getMinNode(root.right); 
            Node s = new Node<K, V>(min);
            s.right = removeMin(root.right);
            s.left = root.left;
            return s;
            
        }
    }

    
    public static void main(String[] args) {
        BST<Integer, Object> bst = new BST();
        bst.insert(1, "hello");
        bst.insert(10, "hello");
        bst.insert(9, "hello");
        bst.insert(20, "hello");
        bst.insert(11, "hello");
        bst.insert(14, "hello");
        bst.insert(90, "hello");
        bst.insert(25, "hello");
        bst.midOrder();
        bst.remove(10);
        System.out.println("");
        bst.midOrder();
    }
    
    
}

相关文章

  • 简单的二分搜索树-BST

  • BinarySearchTree二叉搜索树

    BST概述 二分搜索树是二叉树二分搜索树的每个节点的值:1左子树上所有的节点的值都小于它的根节点的值。2右子树上所...

  • Week 18 0717--0723

    question 1 逆序遍历二分树 给定一个二分搜索树(BST),对于每一个节点,加上所有比它大的节点的和,然后...

  • Algorithm小白入门 -- 二叉搜索树

    二叉搜索树二叉搜索树 BSTBST 的基本操作计算合法的 BST 1. 二叉搜索树 BST 二叉搜索树(Binar...

  • 23-红黑树

    1.二叉搜索树(BST)继承二叉树(BinaryTree) 2.平衡二叉搜索树(BBST)继承二叉搜索树(BST)...

  • 二叉搜索树(BST)

    BST简介 查询BST 插入和删除 #1. BST简介 BST(Binary Search Tree),二叉搜索树...

  • 一、二叉搜索树(BST)

  • 第六章 二分搜索树

    第六章 二分搜索树 1 树结构无处不在 , 文件夹 , 图书馆书分类 , 公司的组织结构2 BST也是一种二分思...

  • avl树

    概述 BST(二叉搜索树)可以提高查找效率,理想的情况下BST的每个子树的高度差相等(BST平衡),搜索的时间复杂...

  • 2019-02-07 Day 33

    二叉搜索树中的搜索 给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回...

网友评论

      本文标题:简单的二分搜索树-BST

      本文链接:https://www.haomeiwen.com/subject/neccsrtx.html