【原创】二切贪食蛇(BCS)

来源:互联网 发布:ps mac触摸板无法缩放 编辑:程序博客网 时间:2024/06/05 13:27

特别版权声明:此数据结构为bzy原创数据结构,转载请先联系作者,QQ:1143710044.

二切贪食蛇(BCS)

二切贪食蛇(Binary-Cuts-Snake),是一种蛇(树)形数据结构,先来看看它的各种作用与复杂度.(-_-)


  • 插入元素:O(64)
  • 删除元素:O(64)
  • 查找元素是否存在O(64)
  • 查找元素对应存储数据O(64)
  • 查找元素在数据结构中的排列位置O(64)
  • 查询元素总量O(1)
  • 查询最小的元素O(64)
  • 空间复杂度O(2n - 64n)

再看代码:

class binary_cut_snake{    typedef unsigned long long _ull;    private :        struct node{            int value;            int degree[2];            _ull up,down;            node *left;            node *right;            node (_ull _up,_ull _down){                up = _up ;                down = _down ;                left = NULL ;                right = NULL ;                degree[0] = degree[1] = 0 ;            }        };        static const _ull _maxull = 18446744073709551615ull;        node  *first;    public :        binary_cut_snake() : first(new node(0,_maxull)){}        void push(int num,int value){            _ull _lf=0,_ri = _maxull;            node **now = &first;            _ull _noi = 9223372036854775808ull;            bool _isnimi = ! include (num) ;            while(_lf ^ _ri){                _ull _mid = (_lf + _ri)>>1;                if(_noi & num){                    if((*now)->right == NULL){                        (*now)->right = new node(_mid + 1 ,_ri);                    }                    (*now)->degree[1] += _isnimi ;                    now = & ((*now)->right);                    _lf = _mid + 1;                    (*now) ->down= _lf ;                    (*now) -> up = _ri ;                 }else{                    if((*now)->left == NULL){                        (*now)->left = new node(_lf ,_mid);                    }                    (*now)->degree[0] += _isnimi ;                    now = & ((*now)->left);                    _ri = _mid ;                    (*now) ->down= _lf ;                    (*now) -> up = _ri ;                 }                _noi >>= 1 ;                if(_lf == _ri)(*now) -> value = value ;            }           }        int get(int num){            _ull _noi = 9223372036854775808ull;            node **now = &first;            while(_noi){                if(*now==NULL)return 0;                if(_noi & num){                    now = & ((*now)->right) ;                }else{                    now = & ((*now)->left)  ;                }                _noi >>= 1;             }            return (*now) ->value;        }        int search_fore(_ull num){            _ull _noi = 9223372036854775808ull;            node **now = &first;            int _ans = 0;            while(_noi){                if(*now==NULL)return _ans;                if(_noi & num){                    _ans += (*now)->degree[0];                    now = & ((*now)->right);                }else{                    now = & ((*now)->left);                }                _noi >>=1 ;            }            return _ans + 1;        }        bool include(_ull num){            _ull _noi = 9223372036854775808ull;            node **now = &first;            while(_noi){                if(*now==NULL)return 0;                if(_noi & num){                    now = & ((*now)->right);                }else{                    now = & ((*now)->left);                }                _noi >>=1 ;            }            return 1;        }        int count(){            return first->degree[0]+first->degree[1];        }        void del(int num){            _ull _noi = 9223372036854775808ull;            node **now = &first;            while(_noi){                if(*now==NULL)return;                if(_noi & num){                    (*now) -> degree[1] --;                    if((*now) ->degree[1] + (*now) -> degree[0] ==0){                        (*now)=NULL;                        return ;                    }                    now = & ((*now) -> right);                }else{                    (*now) -> degree[0] --;                    if((*now) ->degree[0] + (*now) -> degree[1] ==0){                        (*now)=NULL;                        return ;                    }                    now = & ((*now) -> left);                }                _noi >>=1;            }            return ;        }        _ull least(){            _ull _noi = 9223372036854775808ull;            _ull _ans = 0;            node **now = &first;            while(_noi){                if(*now==NULL)return 0;                if((*now)->degree[0]){                    now = &((*now)->left);                    _ans <<= 1;                }else{                    now = &((*now)->right);                    _ans <<= 1;                    _ans = _ans | 1;                }                _noi >>= 1;            }            return _ans;        }};

以上就是BCS的全部代码.
功能
我们可以拿它代替map.push(),get()函数即可满足此功能,
还可以代替堆Heap.用least(),del(),push()函数即可。
还可以代替set,include()函数可以判重
还可以代替二分查找树(binary-sort-tree)Search-Fore()函数可以直接查找元素位置
还可以兼顾线段树部分功能.


此文章与此数据结构版权归bzy所有。