• 【墨鳌】【数据结构】【二叉搜索树】【AVL树预备备】【BinarySearchTree】


    二叉树 - Node

    • 数据存储部分 key-value
    • 左儿子 left
    • 右儿子 right
    public class Node {
        private Key key; // 键
        private Value val; // 值
        private Node left, right; // 指向子树的链接
    }
    

    对此无需过多的解释了,相应的我们可以完善get/set方法,constructor方法,为了debug方便可以重载toString方法。

    二叉搜索树

    在二叉树的基础上多一个子树大小 size 的属性,方便查询rank和按照排名select。

    在增删改查时,时刻维护整颗树,左小右大的特性。

    public class Node {
            private Key key; // 键
            private Value val; // 值
            private Node left, right; // 指向子树的链接
            private int N; // 以该结点为根的子树中的结点总数
    }
    

    至此,我们可以设计二叉搜索树的基本属性的基本方法(因为篇幅问题,省略private函数定义声明,以及具体实现代码)

    public class BinaryTree<Key extends Comparable<Key>, Value> {
        private Node root; // 二叉查找树的根结点
    
        public int size() { return size(root); }
        // 在以x为根结点的子树中查找并返回key所对应的值;
        public Value get(Key key) { return get(root, key); }
        // 查找key,找到则更新它的值,否则为它创建一个新的结点
        public void put(Key key, Value val) { ... }
    
        // min()、floor()方法
        public Node min() { return min(root); }
        public Node floor(Key key) { ... }
    
        // max()、ceiling()方法
        public Node max() { return max(root); }
        public Node ceiling(Key key) { ... }
    
        // select()、rank()方法
        public Node select(int k) {  return select(root, k); }
        public int rank(Key key) { return rank(key, root); }
    
        // delete()、deleteMin()、deleteMax()方法
    	public void delete(Key key) { root = delete(root, key); }
        public void deleteMin() { root = deleteMin(root); }
        public void deleteMax() { root = deleteMax(root); }
    
        // keys()方法
        public Iterable<Key> keys() { return keys(min().key, max().key); }
        public Iterable<Key> keys(Key lo, Key hi) { ... }
        
    
        // --------------------------- In-order ---------------------------
    
        public void print() { print(root); }
    
        // -------------------------- Print Tree --------------------------
    
        public void printTree() {
            System.out.println(" >> START TO PRINT THE TREE:");
            printTree(root);
            System.out.println(" << END TO PRINT THE TREE");
        }
    }
    

    测试部分

    public class Tests {
        public static void main(String[] args) {
            BinaryTree<Integer, String> BST = new BinaryTree<>();
    
    
            List<Integer> keys = Arrays.asList(1, 19, 30, 36, 50, 89, 101, 40, 90, 105, 103);
            for (int key : keys) {
                BST.put(key, Integer.toString(key));
            }
    
            BST.printTree();
            BST.deleteMax();
            BST.deleteMin();
            BST.delete(50);
            BST.printTree();
        }
    }
    

    详细实现,我的 GitHub

    参考资料: 《算法-第4版》

  • 相关阅读:
    暑期学习录
    08管道命名符
    07输入输出重定向
    06grep与find命令详解
    05tar命令详解
    04文件目录管理命令
    03工作目录切换命令与文本文件编辑命令
    02系统状态检测命令
    01常用系统工作命令
    vue2.0细节剖析
  • 原文地址:https://www.cnblogs.com/JasonCow/p/16106791.html
Copyright © 2020-2023  润新知