一些实用的数据结构

平衡树 PPT动画

class RedBlackBST<Key extends Comparable<Key>,Value>{
    boolean RED = true; boolean BLACK = false;
    class Node{ 
        Key key; Value val; Node l,r; int N;
        boolean color;//当前节点与父节点之间链接的颜色
    }
    boolean isRed(Node x){ 
        if(x == null) return false;
        return x.color == RED; 
    }
    Node root;
    int size(){ return size(root); }//size可以没有
    int size(Node x){ if(x==null) return 0; else return x.N; }
    Node rotateLeft(Node h){//这些定义的局部操作有一个共同点:保证没有平衡性
        Node x = h.r; h.r = x.l; x.l = h; x.color = RED; x.N = h.N;
        h.N = 1 + size(h.l) + size(h.r); return x;
    }
    Node rotateRight(Node h){
        Node x = h.l;h.l = x.r;x.r = h;x.color = h.color;h.color = RED;x.N = h.N; 
        h.N = 1 + size(h.l) + size(h.r); return x;
    }
    void flipColors(Node h){
        h.color = RED;h.l.color = BLACK;h.r.color = BLACK;
    }
    void put(Key key,Value val){
        root = put(root,key,val);root.color = BLACK;
    }//root一定黑
    Node put(Node h,Key key,Value val){
        //指的就是建立一个新的结点
        if(h == null) return new Node(key,val,1,RED);
        int cmp = key.compareTo(h.key);//根据Key大小排的序,自上而下找到要插入的位置
        if(cmp<0)  h.l = put(h.l,key,val);
        else if(cmp>0)  h.r = put(h.r,key,val);
        else h.val = val;//如果key相等,就相当于是修改,不会建立新
        //自下而上对每一个结点进行转换建立红黑树
        if(isRed(h.r) && !isRed(h.l)) h = rotateLeft(h);// 不允许出现红色右链接
        if(isRed(h.l) && isRed(h.l.l)) h = rotateRight(h);// 不允许左子和左孙都是红结点
        if(isRed(h.l) && isRed(h.r)) flipColors(h); // 左右链接都是红色时要将颜色向上翻转
        h.N = size(h.l) + size(h.r) + 1; return h;         
    } //这三步有执行的优先级
}
用的时候RedBlackBST<Integer,String> root = new RedBlackBST<Integer, String>();
root.put(3, "wo"); root.put(1, "ni");…

2-3 树(最多有2个节点,3个出度)其实就是一颗3阶(最多3个子树)红黑树

排序算法 PPT动画

  1. 快排

2. 堆排 

KMP(先对match串进行分析)时间O(m+n)

public static int getIndexOf(String s, String m) {
    if (s.length() < m.length()) { return -1; } 
    char[] ss = s.toCharArray();char[] ms = m.toCharArray();
    int si = 0;int mi = 0;
    int[] next = getNextArray(ms);
    while (si < ss.length && mi < ms.length) { 
        if (ss[si] == ms[mi]) {si++;mi++;} 
        else if (mi> 0) {mi = next[mi];} 
        else {si++;}
    }
    return mi == ms.length ? si - mi : -1;      //只有mi==0是next才是-1
}       
public static int[] getNext(char[] ms){ //也是一个双指针
    if(ms.length == 1) return new int[] {-1};
    int[] next = new int[ms.length+1];
    next[0] = -1;int k = 0;int i = 1;
    while(i<ms.length){
        if(ms[i] == ms[k]) { next[++i] = ++k; }
        else if(k>0) { k = next[k]; } //再挣扎一下
        else { next[++i] = 0; } //说明当前i的next只能回到起点了
    } return next;
}

DFA的方法:

public KMP(String pat){ 
    this.pat = pat;int M = pat.length();int R = 256;dfa = new int[M][R];
    dfa[0][pat.charAt(0)] = 1;//其他的默认为0
    for(int X=0,cur=1;cur<M;cur++){
    for(int c=0;c<R;c++)  
        dfa[cur][c] = dfa[X][c];  
        dfa[cur][pat.charAt(cur)] = cur+1;
        //只有匹配当前字符的dfa的值才进入cur+1,其他情况都要回退   
        X = dfa[X][pat.charAt(cur)];//要好好理解这句话,更新位置X,即最长相等前后缀中前缀的末位置
    }                                        
}
public int search(String txt){
    int i,x=0,N = txt.length(),M=pat.length();
    for(i=0;i<N&&x<M;i++) { 
        x = dfa[x][txt.charAt(i)]; 
    }
    if(x == M) { return i-M; } 
    else { return N; }
}

• 跳表插入:找到要插入的位置,然后向上回溯每个关键点,随机确定是否插入关键点,越往上插入概率越小。

public class SkipList {
    MAX_LEVEL = 16; levelCount = 1;Node head = new Node();
    public void insert(int value){
        int level = randomLevel();
        Node newNode = new Node();
        newNode.data = value;
        newNode.maxLevel = level;   
        Node update[] = new Node[level]; 
        for(int i = 0; i < level; ++i){ 
        	update[i] = head; 
        } 
        Node p = head;
        for(int i = level - 1; i >= 0; --i){ 找到第一个比value大的,→的优先级比↓大
            while(p.next[i] != null && p.next[i].data < value){
            	p = p.next[i];
            } 
            update[i] = p; 
        }
       for(int i = 0; i < level; ++i){ 
           newNode.next[i] = update[i].next[i]; update[i].next[i] = newNode; 
       }
       if(levelCount < level){ levelCount = level; }
    }
}
https://blog.csdn.net/pcwl1206/article/details/83512600#commentBox

Radix树,即基数树,也称压缩前缀树,是一种提供key-value存储查找的数据结构。与Trie不同的是,它对Trie树进行了空间优化,只有一个子节点的中间节点将被压缩。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值