码迷,mamicode.com
首页 > 其他好文 > 详细

2-3-4 tree留坑

时间:2019-08-31 23:23:37      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:pair   style   pre   define   max   color   struct   ace   class   

 

#include<bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define mp make_pair
using namespace std;
const int maxx = 2e5+6;
const double alpha = 0.75;
struct node{
  int l,ml,mr,r;
  int val[4];
  int size;
  int fa;
}tree[maxx];
int cnt=0;
int root;
int newnode(int w,int fa){
  tree[++cnt].size=1;
  tree[cnt].val[1]=w;
  tree[cnt].fa=fa;
  return cnt;
}
int inserts(int rt,int w){
   int fa;
   if (tree[rt].size==3){
      if (rt==root){
          root=newnode(tree[rt].val[2],-1);
          rt=root;
          tree[root].l=newnode(tree[rt].val[1],root);
          tree[root].ml=newnode(tree[rt].val[3],root);
          tree[tree[root].l].l=tree[rt].l;
          tree[tree[root].l].ml=tree[rt].ml;
          tree[tree[root].ml].l=tree[rt].mr;
          tree[tree[root].ml].ml=tree[rt].r;
      }else {
         int fa=tree[rt].fa;
         if (tree[fa].size==1){
             if (tree[rt].val[2]<tree[fa].val[1]){
                tree[fa].mr=tree[fa].ml;

                tree[fa].ml=newnode(tree[rt].val[3],fa);
                tree[tree[fa].ml].l=tree[rt].mr;
                tree[tree[fa].ml].ml=tree[rt].r;

                tree[fa].l=newnode(tree[rt].val[1],fa);
                tree[tree[fa].l].l=tree[rt].l;
                tree[tree[fa].l].ml=tree[rt].ml;

                tree[fa].val[2]=tree[fa].val[1];
                tree[fa].val[1]=tree[rt].val[2];

             }else {
                tree[fa].val[2]=tree[rt].val[2];
                tree[fa].ml=newnode(tree[rt].val[1],fa);
                tree[fa].mr=newnode(tree[rt].val[3],fa);
                tree[tree[fa].ml].l=tree[rt].l;
                tree[tree[fa].ml].ml=tree[rt].ml;
                tree[tree[fa].mr].l=tree[rt].mr;
                tree[tree[fa].mr].mr=tree[rt].r;
             }
         }else if (tree[fa].size==2){
             if (tree[rt].val[2]<tree[fa].val[1]){
                tree[fa].val[3]=tree[fa].val[2];
                tree[fa].val[2]=tree[fa].val[1];
                tree[fa].val[1]=tree[rt].val[2];
                tree[fa].r=tree[fa].mr;
                tree[fa].mr=tree[fa].ml;
                tree[fa].ml=newnode(tree[rt].val[3],fa);
                tree[fa].l=newnode(tree[rt].val[1],fa);
                tree[tree[fa].ml].l=tree[rt].mr;
                tree[tree[fa].ml].ml=tree[rt].r;
                tree[tree[fa].l].l=tree[rt].l;
                tree[tree[fa].l].ml=tree[rt].ml;
             }else if (tree[rt].val[2]<tree[fa].val[2]){
                tree[fa].val[3]=tree[fa].val[2];
                tree[fa].val[2]=tree[rt].val[2];
                tree[fa].r=tree[fa].mr;
                tree[fa].ml=newnode(tree[rt].val[1],fa);
                tree[fa].mr=newnode(tree[rt].val[3],fa);
                tree[tree[rt].ml].l=tree[rt].l;
                tree[tree[rt].ml].ml=tree[rt].ml;
                tree[tree[rt].mr].l=tree[rt].mr;
                tree[tree[rt].mr].mr=tree[rt].r;
             }else {
                tree[fa].val[3]=tree[rt].val[2];
                tree[fa].mr=newnode(tree[rt].val[1],fa);
                tree[fa].r=newnode(tree[rt].val[3],fa);
                tree[tree[fa].mr].l=tree[rt].l;
                tree[tree[fa].mr].ml=tree[rt].ml;
                tree[tree[fa].r].l=tree[rt].ml;
                tree[tree[fa].r].ml=tree[rt].mr;
             }
         }
      }
   }
   rt=fa;
   if (w<tree[rt].val[1])
   
}
int main(){
  scanf("%d")
  return 0;
}

 

2-3-4 tree留坑

标签:pair   style   pre   define   max   color   struct   ace   class   

原文地址:https://www.cnblogs.com/bluefly-hrbust/p/11440643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!