class07

本文详细介绍了三种排序算法:前缀树排序、计数排序和基数排序。分别阐述了每种算法的思路,并提供了相应的编码实现。最后讨论了排序算法的稳定性问题。
摘要由CSDN通过智能技术生成

一、前缀树

思路:

1)单个字符串中,字符从前到后的加到一棵多叉树上
2)字符放在路上,节点上有专属的数据项(常见的是pass和end值)
3)所有样本都这样添加,如果没有路就新建,如有路就复用
4)沿途节点的pass值增加1,每个字符串结束时来到的节点end值增加1
解决问题:
1、hash 表能判断是否存在某个字符串
2、前缀树也能判断是否存在某个字符串
3、前缀树还能判断一个字符串出现了多少次
4、前缀树还能判断以某个前缀开头的字符串出现了多少次    

编码:

public static class Node1 {
    
    // 记录出现了多少次
    public int pass;
    // 记录结尾出现了多少次
    public int end;
    // 该结点的后续结点
    public Node1[] nexts;

    // char tmp = 'b'  (tmp - 'a')
    public Node1() {
    
        pass = 0;
        end = 0;
        // 0    a
        // 1    b
        // 2    c
        // ..   ..
        // 25   z
        // nexts[i] == null   i方向的路不存在
        // nexts[i] != null   i方向的路存在
        nexts = new Node1[26];
    }
}

public static class Trie1 {
    
    private Node1 root;

    public Trie1() {
    
        root = new Node1();
    }

    public void insert(String word) {
    
        if (word == null) {
    
            return;
        }
        // 转字符
        char[] str = word.toCharArray();
        Node1 node = root;
        node.pass++; // 出现的次数 + 1
        int path;
        for (int i = 0; i < str.length; i++) {
     // 从左往右遍历字符
            // 找到当前字符所走的路
            path = str[i] - 'a'; // 由字符,对应成走向哪条路
            // 路不存在,创建结点
            if (node.nexts[path] == null) {
    
                node.nexts[path] = new Node1();
            }
            // 往下走
            node = node.nexts[path];
            // 当前结点出现次数+1
            node.pass++;
        }
        // 结尾时,结尾次数+1
        node.end++;
    }

    // 删除指定字符串
    public void delete(String word) {
    
        // 先查字符串是否存在
        if (search(word) != 0) {
    
            // 转字符
            char[] chs = word.toCharArray();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值