home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _ab_tree.c next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  33.1 KB  |  1,272 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _ab_tree.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/ab_tree.h>
  16.  
  17. //---------------------------------------------------------------
  18. //  
  19. //  Michael Wenzel:
  20. //  function insert_at_node added
  21. //
  22. //  double links between leaf and inner node with same key
  23. //
  24. //  delete overwrites copies of keys in inner nodes
  25. //
  26. //  leafs are nodes of degree 1, linked by son[1], son[2]
  27. //
  28. //---------------------------------------------------------------
  29.  
  30. //------------------------------------------------------------------
  31. // constructors
  32. //------------------------------------------------------------------
  33.  
  34.  
  35. ab_tree_node::ab_tree_node(int p_,int height_,int index_,ab_tree_node* father_,int b)
  36. {
  37.     son  = (ab_tree_node**) allocate_words(b+2);   /* array[1..b+1] */
  38.     k    = (ent*) allocate_words(b+1);             /* array[1..b]   */
  39.     same = (ab_tree_node**) allocate_words(b+1);   /* array[1..b]   */
  40.  
  41.  
  42.     // position 0 contains the size of the array:
  43.  
  44.     son[0]  = (ab_tree_node*)(b+2);
  45.     k[0]    = ent(b+1);
  46.     same[0] = (ab_tree_node*)(b+1);
  47.  
  48.     p=p_;
  49.     height=height_;
  50.     index=index_;
  51.     father=father_;
  52.     for(int i=1;i<=(b+1);son[i++]=0);
  53.     for(i=1;i<=b;k[i++]=0);
  54.     for(i=1;i<=b;same[i++]=0);                
  55.    }
  56.  
  57.  
  58.  ab_tree_node::~ab_tree_node() 
  59.   { deallocate_words(son,int(son[0]));    /* first array element gives size */
  60.     deallocate_words(same,int(same[0]));
  61.     deallocate_words(k,int(k[0]));
  62.    }
  63.  
  64.  
  65.  
  66.  
  67.  ab_tree::ab_tree(int a_,int b_)
  68.  {
  69.    root=maximum=minimum=0;
  70.    count=0;
  71.    height=-1;
  72.    if((a_>=2)&&(b_>=2*a_-1))
  73.     { a=a_;
  74.       b=b_;
  75.      }
  76.    else error_handler(1,"ab_tree: illegal arguments (a,b) in tree constructor");
  77.  }
  78.  
  79.  ab_tree::ab_tree(const ab_tree& T)
  80.   {
  81.      if (T.root==0) {root=maximum=minimum=0;height=-1;count=0;}
  82.      else { abnode help=0;
  83.         root = T.copy_ab_tree(T.root,help,T.b);
  84.             maximum=help;
  85.             help=root;
  86.             while (help->p) help=help->son[1];
  87.         minimum=help;
  88.             height=T.height;
  89.             count=T.count;
  90.           };
  91.      a=T.a;
  92.      b=T.b;
  93.   }
  94.  
  95. //-----------------------------------------------------------------
  96. // operators
  97. //-----------------------------------------------------------------
  98.  
  99.  ab_tree& ab_tree::operator=(const ab_tree& T)
  100.   {  if (this == &T) return *this;
  101.  
  102.      clear();
  103.  
  104.      if (T.root!=0) 
  105.      { abnode help=0;
  106.        root=copy_ab_tree(T.root,help,T.b);
  107.        maximum=help;
  108.        help=root;
  109.        while (help->p) help=help->son[1];
  110.        minimum=help;
  111.        height=T.height;
  112.        count=T.count;
  113.       };
  114.  
  115.      a=T.a;
  116.      b=T.b;
  117.  
  118.      return *this;
  119.   }
  120.  
  121.  
  122. //-----------------------------------------------------------------
  123. // member functions
  124. //-----------------------------------------------------------------
  125.  
  126. void ab_tree::exchange_leaves(ab_tree_node* v, ab_tree_node* w)
  127. { // exchange leaves v and w
  128.  
  129.   ent k1 = v->k[1]; 
  130.   int p1 = v->index;
  131.   ab_tree_node* u1 = v->same[1];
  132.   ab_tree_node* f1 = v->father;
  133.  
  134.   ent k2 = w->k[1]; 
  135.   int p2 = w->index;
  136.   ab_tree_node* u2 = w->same[1];
  137.   ab_tree_node* f2 = w->father;
  138.  
  139.  
  140.   f1->son[p1] = w;
  141.   w->index    = p1;
  142.   w->same[1]  = u1;
  143.   w->father   = f1;
  144.  
  145.   f2->son[p2] = v;
  146.   v->index    = p2;
  147.   v->same[1]  = u2;
  148.   v->father   = f2;
  149.  
  150.   ab_tree_node* pred1 = v->son[1];
  151.   ab_tree_node* succ1 = v->son[2];
  152.  
  153.   ab_tree_node* pred2 = w->son[1];
  154.   ab_tree_node* succ2 = w->son[2];
  155.  
  156.   w->son[1] = pred1;
  157.   if (pred1) pred1->son[2] = w;
  158.   if (succ1!=w) 
  159.   { w->son[2] = succ1;
  160.     succ1->son[1] = w;
  161.    }
  162.  
  163.   if (pred2==v) pred2 = w;
  164.  
  165.  
  166.   v->son[1] = pred2;
  167.   v->son[2] = succ2;
  168.   pred2->son[2] = v;
  169.   if (succ2) succ2->son[1] = v;
  170.  
  171.   // minimum & maximum:
  172.   if (v==minimum) minimum = w;
  173.   if (w==maximum) maximum = v;
  174.  
  175.   // overwrite inner copies:
  176.   
  177.   int pos1=1;
  178.   while(u1->same[pos1]!=v) pos1++;
  179.  
  180.   int pos2=1;
  181.   if (u2) while(u2->same[pos2]!=w) pos2++;
  182.  
  183.   u1->k[pos1] = k2;
  184.   u1->same[pos1] = w;
  185.  
  186.   if (u2) 
  187.   { u2->k[pos2] = k1;
  188.     u2->same[pos2] = v;
  189.    }
  190.  
  191. }
  192.  
  193. void ab_tree::flip(ab_tree_node* v, ab_tree_node* w)
  194. /*
  195.   if (v==nil || w==nil || w!=succ(v)) 
  196.     error_handler(1,"ab_tree: illegal arguments for flip");
  197. */
  198.  
  199.   if (cmp(v->k[1],w->k[1]) < 0) 
  200.     error_handler(1,"ab_tree: illegal flip");
  201.  
  202.   exchange_leaves(v,w);
  203. }
  204.  
  205. void ab_tree::reverse(ab_tree_node* v, ab_tree_node* w)
  206. { // reverse sequence of leaves: v, ..., w
  207.  
  208.   if (v==w) return;
  209.  
  210.   if (cmp(v->k[1],w->k[1])<0) 
  211.   { newline;
  212.     cout << "a = "; print_key(v->k[1]); newline;
  213.     cout << "b = "; print_key(w->k[1]); newline;
  214.     error_handler(1,"ab_tree: illegal reverse(a,b)\n");
  215.    }
  216.  
  217.   ab_tree_node* u;
  218.  
  219.   for(;;)
  220.   { exchange_leaves(v,w);
  221.     u = pred(v);
  222.     if (u == w) break;
  223.     v = succ(w);
  224.     if (v==u) break;
  225.     w = u;
  226.    }
  227.  
  228. }
  229.  
  230.  void ab_tree::clear()
  231.    { if (root!=0) {maximum=minimum=0;del_tree(root);root=0;};
  232.      height=-1;
  233.    }
  234.  
  235.  void ab_tree::del(ent key)
  236.  { if (!root) return;                      // s.n.
  237.  
  238.    ab_tree_node* w=locate(key);
  239.  
  240.    if(w && cmp(w->k[1],key) == 0)  del_node(w);
  241.  
  242.    else /* error_handler(1,"ab_tree: del(key):key is not in tree") */;
  243.  }
  244.  
  245.  
  246. ab_tree_node* ab_tree::expand(ab_tree_node* v,int i)
  247.  
  248. // expand v by returning an additional son to the right of w
  249. // --> w is the i-th son of v
  250. // s.n.: if i==0 then  new leftmost son
  251. // expand does not test a<=number of sons(v)<=b
  252. // m.w. expand does not set same links for new leaves
  253.  
  254.  {
  255.    // move the nodes right to w to give an additional son to 
  256.    // the right of w
  257.  
  258.    int l=(v->p);
  259.  
  260.    if (i < l)
  261.    {
  262.      v->son[l+1]=v->son[l];
  263.      v->son[l+1]->index=l+1;
  264.      l--;
  265.     }
  266.  
  267.    while(i < l) 
  268.    { 
  269.      v->k[l+1] = v->k[l];
  270.      v->same[l+1] = v->same[l];
  271.  
  272.      v->son[l+1]=v->son[l];
  273.      v->son[l+1]->index=l+1;
  274.  
  275.      l--;
  276.     };
  277.  
  278.    if (v->height>1)
  279.       v->son[i+1] = new ab_tree_node(0,v->height-1,i+1,v,b);
  280.    else  
  281.       v->son[i+1] = new ab_tree_node(0,v->height-1,i+1,v,1);
  282.  
  283.    (v->p)++;
  284.  
  285.    return v->son[i+1];
  286. }                     
  287.                      
  288.  
  289. void ab_tree::split_node(ab_tree_node* v)
  290. {
  291.  /* adding a child increases the degree of v by 1. If v->p<=b after
  292.     adding the new leave, then we are done . Otherwise we have to 
  293.     split v. Splitting can propagate ==> Loop 
  294.  
  295.     m.w. changes same links between nodes                          */
  296.  
  297.   ab_tree_node* y;
  298.  
  299.   while (v->p==b+1) 
  300.   {
  301.     if (v!=root)  y = v->father;
  302.     else 
  303.     { y=new ab_tree_node(1,v->height+1,0,0,b);
  304.       root=y;height++;
  305.       y->son[1]=v;
  306.       v->index=1;
  307.       v->father=y;
  308.      };
  309.  
  310.  
  311.     register ab_tree_node* u;
  312.  
  313.     u = expand(y,v->index);   // u <-- new right brother of v
  314.  
  315.     int down =(int)((b+1)/2) ;
  316.     int up = (b+1)- down;
  317.  
  318.  
  319.     if (u->index <= b)
  320.     { y->k[u->index] = y->k[v->index];
  321.       y->same[u->index] = y->same[v->index];
  322.      }
  323.  
  324.     y->k[v->index] = v->k[down];
  325.     y->same[v->index]  = v->same[down];
  326.     y->same[v->index]->same[1] = y;    
  327.  
  328.  
  329.     // split v, i.e. take the rightmost (b+1)/2 children and keys
  330.     // away from v and incorporate them into u and store key v->k[(b+1)/2]
  331.     // in y ( = father(v))  between the pointers to v and u i.e. at position
  332.     // v->index
  333.  
  334.     // m.w. change same links of v to y and u
  335.  
  336.     register int j;
  337.  
  338.     for (j=1;j<up;j++)    
  339.     {
  340.       u->son[j] = v->son[down+j];
  341.       u->son[j]->index=j;
  342.       u->son[j]->father=u;
  343.       u->k[j] = v->k[down+j];
  344.       u->same[j] = v->same[down+j]; 
  345.       u->same[j]->same[1] = u;      
  346.  
  347.       v->son[down+j] = 0;      // muss das sein ?
  348.       v->same[down+j] = 0;          
  349.       v->k[down+j] = 0;
  350.  
  351.      };
  352.  
  353.     u->son[up]=v->son[b+1];
  354.     u->son[up]->index=up;
  355.     u->son[up]->father=u;
  356.  
  357.     v->son[b+1] = 0;          // und das?
  358.     v->same[down] = 0;                 
  359.     v->k[down] = 0;
  360.  
  361.     v->p = down;             // change number of children
  362.     u->p = up;
  363.  
  364.     v = y;
  365.   }
  366.  
  367. };
  368.  
  369. ab_tree_node* ab_tree::insert(ent key, ent inf)
  370. {
  371.  
  372.  if (root==0) { root=new ab_tree_node(0,0,0,0,1);
  373.                 copy_key(key);
  374.                 copy_inf(inf);
  375.                 root->inf = inf;
  376.                 root->k[1]= key;
  377.                 height=0;
  378.                 maximum=minimum=root;
  379.                 count++;
  380.                 return root;
  381.                }
  382.  
  383.  // insert_at_node calls copy_key & copy_inf
  384.  
  385.  ab_tree_node* p = locate(key);
  386.  if (p==nil) p = maximum;
  387.  return insert_at_node(p,key,inf);
  388.  
  389. };
  390.  
  391. ab_tree_node* ab_tree::insert_at_node(ab_tree_node* w, ent key, ent inf)
  392.   // insert a new item (key,inf) left or right of leaf w  (according to key(w))
  393.  
  394.    copy_inf(inf);
  395.  
  396.    ab_tree_node* v;
  397.  
  398.     if (cmp(w->k[1],key)!=0)
  399.     {
  400.       copy_key(key);
  401.  
  402.      if ( w!=minimum && cmp(w->son[1]->k[1],key) > 0)
  403.       { cout << "INSERT_AT: WRONG POSITION\n";
  404.         cout << "insert:   key = "; print_key(key); cout << "\n";
  405.         if (w!=maximum) 
  406.            cout << "succ-pos: key = "; print_key(w->son[2]->k[1]); cout << "\n";
  407.         cout << "position: key = "; print_key(w->k[1]); cout << "\n";
  408.         cout << "pred-pos: key = "; print_key(w->son[1]->k[1]); cout << "\n";
  409.         error_handler(1,"ab_tree::insert_at : wrong position "); 
  410.        }
  411.  
  412.      if ( w!=maximum && cmp(w->son[2]->k[1],key) < 0)
  413.       { cout << "INSERT_AT: WRONG POSITION\n";
  414.         cout << "insert:   key = "; print_key(key); cout << "\n";
  415.         cout << "succ-pos: key = "; print_key(w->son[2]->k[1]); cout << "\n";
  416.         cout << "position: key = "; print_key(w->k[1]); cout << "\n";
  417.         if (w!=minimum)
  418.            cout << "pred-pos: key = "; print_key(w->son[1]->k[1]); cout << "\n";
  419.         error_handler(1,"ab_tree::insert_at : wrong position "); 
  420.        }
  421.  
  422.  
  423.         count++;
  424.         if (root==w) {   v=new ab_tree_node(2,1,0,0,b);
  425.                          root=v;height=1;
  426.                          ab_tree_node* u;
  427.                          if (cmp(key,w->k[1])<0)
  428.                              {   
  429.                                  u=new ab_tree_node(0,0,1,v,1);
  430.                                  v->son[1]=u;u->k[1]=key; u->inf=inf;
  431.                                  v->son[2]=w ;
  432.                                  v->p=2;v->k[1]=u->k[1];
  433.                  u->same[1] = v;
  434.                                  v->same[1] = u;
  435.                  w->index=2;
  436.                                  minimum=u;
  437.                                  u->son[2] = w;
  438.                                  w->son[1] = u;
  439.                              }
  440.                          else {
  441.                                  u=new ab_tree_node(0,0,2,v,1);
  442.                                  v->son[1]=w;
  443.                  w->same[1] = v;
  444.                                  v->same[1] = w;
  445.                                  v->son[2]=u;u->k[1]=key; u->inf=inf;
  446.                                  v->p=2;v->k[1]=w->k[1];
  447.                                  w->index=1; 
  448.                                  maximum=u;
  449.                                  w->son[2] = u;
  450.                                  u->son[1] = w;
  451.                               }
  452.                          w->father=v;
  453.  
  454.                          return u;
  455.                       }; 
  456.  
  457.     if ((w!=maximum) && (cmp(key,w->k[1])>0)) w=w->son[2];
  458.  
  459.         ab_tree_node* u;
  460.  
  461.         int index1 = w->index;
  462.  
  463.         v = w->father;
  464.  
  465.         if (cmp(key,w->k[1])<0)
  466.         { u = expand(v,index1-1);   // new son u left of w
  467. /*
  468.                  v
  469.  
  470.               /  |  \
  471.  
  472.                 (u)  w  
  473. */
  474.           u->k[1] = key;
  475.           u->inf = inf;
  476.  
  477.           u->son[2] = w;
  478.           u->son[1] = w->son[1];
  479.           w->son[1] = u;
  480.  
  481.       u->same[1] = v;        
  482.           v->k[index1] = key;
  483.           v->same[index1] = u ;  
  484.  
  485.           if (minimum == w)  
  486.              minimum=u;
  487.           else 
  488.              u->son[1]->son[2] = u;
  489.  
  490.          }
  491.         else 
  492.         { u = expand(v,index1);   // new son u right of w
  493. /*
  494.                  v
  495.  
  496.               /  |  \
  497.  
  498.              w  (u)     
  499. */
  500.           u->k[1] = key;
  501.           u->inf = inf;
  502.  
  503.           u->son[1] = w;
  504.           u->son[2] = w->son[2];
  505.           w->son[2] = u;
  506.  
  507.           if (maximum == w)  
  508.           {
  509.             maximum=u;
  510.             v->k[index1] = w->k[1];
  511.         w->same[1] = v;        
  512.         v->same[index1] = w ;  
  513.  
  514.            }
  515.           else
  516.           {
  517.             v->k[index1+1] = key;
  518.         u->same[1] = v;        
  519.         v->same[index1+1] = u ;  
  520.  
  521.             u->son[2]->son[1] = u;
  522.            }
  523.  
  524.          }
  525.  
  526.         if (v->p > b) split_node(v);
  527.  
  528.         return u;
  529.       }
  530.    else { clear_inf(w->inf);
  531.           w->inf = inf; 
  532.           return w;
  533.          }
  534.    };
  535.  
  536. ab_tree_node* ab_tree::locate(ent key) const
  537.    {
  538.   /* searching for an element key in a (a,b)-tree ;
  539.    we search down the tree starting at the root r until we reache 
  540.    a leave. In each node v we use the sequence k[1](v),..k[v->p-1](v) 
  541.    in order to guide the search to the proper subtree.In the the
  542.    function we assume that k[0](v)<key<k[v->p](v) for every
  543.    element key in U
  544.    locate returns a pointer to a leave*/ 
  545.  
  546.   register ab_tree_node* v = root;
  547.   register int i;
  548.  
  549.   if (v == nil) return nil;
  550.  
  551.    while (v->p)      /* while v is not a leave*/
  552.    { for(i=1; i < v->p && cmp(key,v->k[i]) > 0; i++);   // s.n.
  553.      v=v->son[i];
  554.     };
  555.  
  556.   return (cmp(key,v->k[1]) <= 0) ? v : nil;
  557. };
  558.  
  559. ab_tree_node* ab_tree::locate_pred(ent key) const
  560. { ab_tree_node* v = locate(key);
  561.   if (v==0) return maximum;
  562.   if (cmp(key,v->k[1])==0) return v;
  563.   return v->son[1];
  564.  }
  565.  
  566.  
  567. ab_tree_node* ab_tree::lookup(ent k) const 
  568. { ab_tree_node* p = locate(k);
  569.   if (p && cmp(k,key(p))!=0 ) p = 0;
  570.   return p;
  571.  }
  572.  
  573.  
  574. void ab_tree::fuse(ab_tree_node* v,ab_tree_node* y)
  575.    {
  576.    /* fuse v and y, i.e. make all sons of y to sons of v and move all
  577.       keys from y to v and delete node y; also move one key (the key
  578.       between the pointers to y and v) from z to v; (note that this will
  579.       shrink z, i.e. decrease the arity of z by one)  
  580.    */
  581.  
  582.    ab_tree_node* z=v->father;
  583.  
  584.    ent hilf=z->k[v->index] ;     /* before k[v->p] was not used {=0}*/
  585.               /* 
  586.                  we only must copy the pointer of son and the node-keys
  587.                  from y to v
  588.               */
  589.           /* change same-pointer of y and z                     */
  590.    v->k[v->p]=hilf; 
  591.    v->same[v->p]=z->same[v->index];  
  592.    v->same[v->p]->same[1] = v;       
  593.  
  594.    for(int i=1;i<y->p;i++) {  v->k[v->p+i]=y->k[i];
  595.                   v->same[v->p+i]=y->same[i];    
  596.                   v->same[v->p+i]->same[1]=v;    
  597.                               v->son[v->p+i]=y->son[i];
  598.                               v->son[v->p+i]->index=v->p+i;
  599.                               v->son[v->p+i]->father=v;
  600.                            };
  601.    v->son[v->p+y->p]=y->son[y->p];   /* copy of the last son from y*/
  602.    v->son[v->p+y->p]->index=v->p+y->p;
  603.    v->son[v->p+y->p]->father=v;
  604.  
  605.    v->p=v->p+y->p;
  606.  
  607.    for(i=v->index;i<z->p;i++) { z->k[i]=z->k[i+1];
  608.                 z->same[i] = z->same[i+1]; 
  609.                                 z->son[i+1]=z->son[i+2];
  610.                                 if (z->son[i+1]!=0){z->son[i+1]->index=i+1; 
  611.                                 z->son[i+1]->father=z;};
  612.                                };
  613.    z->p--;
  614.    z->k[z->p]=0;      
  615.    z->same[z->p]=0;   
  616.  
  617.    delete y;
  618.    
  619.   };
  620.  
  621.  
  622.  void ab_tree::share(ab_tree_node* v,ab_tree_node* y,int direct)
  623.  {
  624.   /* we assume that y is the right brother of v;
  625.      take the leftmost son away from y and make it an additional(right-
  626.      most) son of v; also move one key( the key between the pointers
  627.      to v and y) from z down to v and replace it by the leftmost
  628.      key of y;    
  629.      the other case is equivalent
  630.      let z be the father of v
  631.    */
  632.  
  633.      ab_tree_node* z=v->father;
  634.  
  635.      if (direct==1)  { 
  636.                        v->son[v->p+1]=y->son[1];
  637.                        v->son[v->p+1]->index=v->p+1;
  638.                        v->son[v->p+1]->father=v;
  639.                        v->k[v->p]=z->k[v->index];
  640.                v->same[v->p]=z->same[v->index];   
  641.                        v->same[v->p]->same[1] = v;    
  642.                        z->k[v->index]=y->k[1];
  643.                        z->same[v->index]=y->same[1];      
  644.                z->same[v->index]->same[1]=z;      
  645.  
  646.                        v->p++;    
  647.  
  648.                        for(int i=1;i<(y->p)-1;i++) 
  649.                        { y->son[i]=y->son[i+1];
  650.                          y->son[i]->index=i;
  651.                          y->k[i]=y->k[i+1];
  652.                          y->same[i]=y->same[i+1];
  653.                        };
  654.  
  655.                        y->p--;     // decrease number of children
  656.  
  657.                        y->son[y->p]=y->son[y->p+1];
  658.                        y->son[y->p]->index=y->p;
  659.                        y->son[y->p+1]=0;  
  660.                        y->k[y->p]=0;
  661.                y->same[y->p] = 0;                 
  662.                      }
  663.      else            {    // y is at the left side of v
  664.                        for(int i=v->p;i>=1;i--) 
  665.                        { v->son[i+1]=v->son[i];
  666.                          v->son[i+1]->index=i+1;
  667.                          v->k[i+1]=v->k[i];
  668.                          v->same[i+1]=v->same[i]; 
  669.                         };
  670.  
  671.                        v->son[1]=y->son[y->p];
  672.                        v->son[1]->index=1;
  673.                        v->son[1]->father=v;
  674.                        y->son[y->p]=0;
  675.  
  676.                        v->p++;
  677.                        y->p--;
  678.  
  679.                        v->k[1]=z->k[y->index];
  680.                v->same[1]=z->same[y->index];     
  681.                v->same[1]->same[1] = v;            
  682.                        z->k[y->index]=y->k[y->p];
  683.                        z->same[y->index]=y->same[y->p];  
  684.                        z->same[y->index]->same[1] = z;   
  685.                        y->k[y->p]=0;
  686.                y->same[y->p]=0;                    
  687.                      };
  688.      }
  689.  
  690.  
  691.  void ab_tree::del_node(ab_tree_node* w)
  692.     {
  693.          /* 
  694.           we must delete leave w with father v
  695.           we shrink v by deleting leave w and one of the keys in the 
  696.           adjacent to the pointer to w 
  697.           (if w is the i-th son of v then we delete k[i](v) if i<v->p
  698.           k[i-1](v) if i=v->p  ).
  699.       m.w. if i=v->p we overwrite the inner node 
  700.            in which key w->k[1] is stored with k[i-1](v)
  701.            and then delete k[i-1](v)
  702.          */
  703.  
  704.  
  705.        if (w==nil) error_handler(1,"ab_tree: nil item in del_node");
  706.  
  707.        count--;
  708.  
  709.        if (maximum==minimum)  
  710.         { maximum=minimum=root=0; 
  711.           height=-1; 
  712.           clear_key(w->k[1]);
  713.           clear_inf(w->inf);
  714.           delete w; 
  715.           return;
  716.          }
  717.             /* here w==root==> last leave will be deleted*/
  718.        
  719.        ab_tree_node* succ = w->son[2];
  720.        ab_tree_node* pred = w->son[1];
  721.  
  722.        if (pred) pred->son[2]   = succ;
  723.        else minimum = succ;
  724.        if (succ) succ->son[1] = pred;
  725.        else  maximum = pred;
  726.  
  727.  
  728.        ab_tree_node* v    = w->father;
  729.  
  730.        v->son[w->index]=0;
  731.        if (w->index==v->p) {
  732.                  if (succ)
  733.                  { //overwrite copies in inner node u
  734.                    ab_tree_node* u = w->same[1];
  735.                    int pos=1;
  736.                                while(u->same[pos]!=w) pos++;
  737.                    u->k[pos]=pred->k[1];  
  738.                    u->same[pos]=pred;
  739.                    pred->same[1] = u;
  740.                  }
  741.                  else
  742.                    v->same[v->p-1]->same[1]=0; 
  743.                              v->k[v->p-1]=0;
  744.                  v->same[v->p-1]=0;           
  745.                }
  746.        else                { v->k[w->index]=0 ;
  747.                  v->same[w->index]=0;         
  748.                              for(int i=w->index;i<(v->p)-1;i++)
  749.                              { v->k[i]=v->k[i+1];
  750.                                v->same[i]=v->same[i+1]; 
  751.                                v->son[i]=v->son[i+1];
  752.                                v->son[i]->father=v;
  753.                                v->son[i]->index=i;
  754.                               };
  755.                              v->son[v->p-1]=v->son[v->p];
  756.                              v->son[v->p]=0;
  757.                              v->k[v->p-1]=0;
  758.                              v->same[v->p-1]=0;          
  759.                              v->son[v->p-1]->father=v;
  760.                              v->son[v->p-1]->index=v->p-1;
  761.                            };
  762.  
  763.        clear_key(w->k[1]);
  764.        clear_inf(w->inf);
  765.        delete w;
  766.  
  767.        (v->p)--;
  768.        
  769.        if ((v==root) && (v->p==1)) {  
  770.                                   if (v->son[1]==0) 
  771.                                       {v->son[1]=v->son[2];
  772.                                        v->son[2]=0;  };
  773.                                   root=v->son[1];
  774.                                   delete v;
  775.                                   root->index=0;
  776.                                   root->father=0;
  777.                                   root->p=0;
  778.                                   root->height=0;
  779.                                   height=0;
  780.                                   minimum=maximum=root;
  781.                                   return; 
  782.                                 };
  783.  
  784.        if (v->p >= a) return;
  785.  
  786.     /* otherwise v needs to be rebalanced by either fusing or sharing
  787.      let y be any brother of v*/
  788.      ab_tree_node* z;
  789.      ab_tree_node* y;
  790.      int dir;
  791.        if (v->index==1)  {
  792.                             z=v->father;
  793.                             y=z->son[v->index+1] ;
  794.                            dir=1;    //  y is the right son of v
  795.                          }
  796.        else              { 
  797.                             z=v->father;
  798.                             y=z->son[v->index-1] ;
  799.                            dir =0;   //  y is the left son of v
  800.                          };
  801.        while ((v->p==a-1) && (y->p==a))
  802.             {
  803.              if (dir==1) fuse(v,y); 
  804.              else  fuse(y,v); 
  805.  
  806.              if (z==root) {
  807.                            if (z->p==1) {
  808.                                          if (dir==1){
  809.                                                       root=v;
  810.                                                       v->father=0;
  811.                                                       v->index=0;   }
  812.                                          else {
  813.                                                 root=y;
  814.                                                 y->father=0;
  815.                                                 y->index=0;    };
  816.                                          height--;
  817.                                          delete z;  
  818.                                        };
  819.                            return;
  820.                           };
  821.              v=z;       // continue recursion
  822.              if (v->index==1)  {z=v->father;
  823.                                 y=z->son[v->index+1] ;
  824.                                 dir=1;  // y is the right son of v
  825.                                }
  826.              else              { z=v->father;
  827.                                  y=z->son[v->index-1];
  828.                                  dir=0;
  829.                                };
  830.             };
  831.     /* we have either v->p>=a and rebalancing is completed or
  832.      v->p = a-1 and y->p > a and rebalancing is completed by sharing;*/
  833.  
  834.       if (v->p==a-1)
  835.         {       /*
  836.                  it is important to which side y is in order to v
  837.                  ==> dir is an information about in the function share
  838.                 */
  839.            share(v,y,dir);
  840.         };
  841.       return;
  842.  }
  843.  
  844.  
  845. ab_tree& ab_tree::conc(ab_tree& s2)
  846.  
  847.   if ((a!=s2.a)||(b!=s2.b)) 
  848.      error_handler(1,"ab_tree: incompatible trees in concatenate operation");
  849.  
  850.   if (s2.root==0) return *this;
  851.  
  852.   if (root==0) 
  853.   { root=s2.root;
  854.     maximum=s2.maximum;
  855.     minimum=s2.minimum;
  856.     height=s2.height;
  857.     count =s2.count;
  858.    }
  859.   else
  860.   { if (cmp(maximum->k[1],s2.minimum->k[1])>=0) 
  861.                     error_handler(1,"ab_tree: join(S,T) : max(S)>=min(T)"); 
  862.  
  863.     concat(*this,s2,maximum,maximum->k[1]);
  864.  
  865.     // link leaves 
  866.     maximum->son[2]=s2.minimum;       
  867.     s2.minimum->son[1]=maximum;
  868.  
  869.     maximum=s2.maximum;              
  870.    }
  871.  
  872.   s2.root=0;
  873.   s2.maximum=0;
  874.   s2.minimum=0;
  875.   s2.height=-1;
  876.  
  877.   return *this;
  878. }
  879.  
  880.  
  881. /*---------------------------------------------------------------------
  882.   global functions
  883. ----------------------------------------------------------------------*/
  884.  
  885. void concat(ab_tree& s1,ab_tree& s2,ab_tree_node* current,ent cur_key)
  886.   // Result in s1
  887.  
  888.   ab_tree_node* v=s1.root;
  889.   ab_tree_node* w=s2.root;
  890.   int h1=v->height;     
  891.   int h2=w->height;
  892.   int i;
  893.  
  894.   if(h1==h2)
  895.      { ab_tree_node* z=new ab_tree_node(2,h1+1,0,0,s1.b);
  896.        z->son[1]=v;z->son[2]=w; z->k[1]=cur_key;
  897.        z->son[1]->father=z; z->son[2]->father=z;
  898.        z->son[1]->index=1;z->son[2]->index=2;
  899.        z->same[1]=current;              
  900.        current->same[1]=z;              
  901.        s1.height++;
  902.        s1.root=z;
  903.     }
  904.   else { if (h1>h2)
  905.          {
  906.             for(i=1;i<h1-h2;i++,v=v->son[v->p]);  
  907.             v->son[v->p+1]=w;
  908.             v->son[v->p+1]->father=v;
  909.             v->son[v->p+1]->index=v->p+1;
  910.             v->k[v->p]=cur_key;
  911.         v->same[v->p]=current;     
  912.         current->same[1]=v;        
  913.          v->p++;
  914.         if (v->p==s1.b+1)  {s1.split_node(v);  };
  915.         }
  916.         else /* h1<h2 */
  917.         {
  918.        for(i=1;i<=h2-h1-1;i++,w=w->son[1]);
  919.            for(i=w->p;i>1;i--)
  920.             { w->son[i+1]=w->son[i];
  921.               w->son[i+1]->father=w;
  922.             w->son[i+1]->index=i+1;
  923.               w->k[i]=w->k[i-1];
  924.           w->same[i]=w->same[i-1];   
  925.             };
  926.            w->p++;
  927.            w->son[2]=w->son[1];
  928.            w->son[2]->father=w;
  929.            w->son[2]->index=2;
  930.            w->son[1]=v;
  931.            w->son[1]->father=w;
  932.            w->son[1]->index=1;
  933.            w->k[1]=cur_key;
  934.        w->same[1]=current;             
  935.        current->same[1]=w;             
  936.            if (w->p==s2.b+1) {s2.split_node(w);};
  937.        s1.root =  s2.root;
  938.        s1.height =  s2.height;
  939.         }
  940.       }
  941.  
  942.   /* maximum/minimum are now undefined  */
  943.  
  944. }
  945.  
  946.  
  947.  
  948. /*
  949. void ab_tree::split_at_key(ent y,ab_tree& L,ab_tree& R)
  950. { ab_tree_node* w = locate(y);
  951.   if (!w) error_handler(1,"ab_tree: split: no item ");
  952.   split_at_item(w,L,R);
  953.  }
  954. */
  955.  
  956. void ab_tree::split_at_item(ab_tree_node* w,ab_tree& L,ab_tree& R)
  957.   {
  958.     if(((a!=L.a)||(a!=R.a))||((b!=L.b)||(b!=R.b)))
  959.        error_handler(1,"ab_tree: incompatible trees in split operation");
  960.     
  961.     /* initialisation   */
  962.     L.root=L.minimum=L.maximum=0;L.height=-1;
  963.     R.root=R.minimum=R.maximum=0;R.height=-1;
  964.  
  965.     if(root==0) return;
  966.  
  967.     if (w==0) 
  968.     { R.root = root;
  969.       R.height = height;
  970.       R.maximum = maximum;
  971.       R.minimum = minimum;
  972.       R.count = count;
  973.       root = 0;
  974.       height = -1;
  975.       maximum = 0;
  976.       minimum = 0;
  977.       count = 0;
  978.       return;
  979.      }
  980.  
  981.     if (w==maximum) 
  982.     { L.root = root;
  983.       L.height = height;
  984.       L.maximum = maximum;
  985.       L.minimum = minimum;
  986.       L.count = count;
  987.       root = 0;
  988.       height = -1;
  989.       maximum = 0;
  990.       minimum = 0;
  991.       count = 0;
  992.       return;
  993.      }
  994.  
  995.     ab_tree_node* l;
  996.     ab_tree_node* r;    // pointers to the roots of the left and right subtree
  997.  
  998.  
  999.     /* parameters for concat  */
  1000.  
  1001.     ab_tree_node* current_l=0 ;
  1002.     ent           current_l_key=0;
  1003.  
  1004.     ab_tree_node* current_r=0;  
  1005.     ent           current_r_key=0;
  1006.  
  1007.     int i;
  1008.  
  1009.  
  1010.     /* w is a pointer to the leave y  */
  1011.     ab_tree_node* v;
  1012.  
  1013.     /* store leaf to split at         */
  1014.     ab_tree_node* leaf=w;
  1015.  
  1016.  
  1017.      l = w;
  1018.      r = 0;
  1019.  
  1020.       do{
  1021.          v=w->father;
  1022.          int index_of_w = w->index; 
  1023.  
  1024.        /* now we have construct the  left and right subtrees and the pointers
  1025.           to the roots  --> we must construct two trees with these roots*/
  1026.  
  1027.         if ((L.root==0)&&(l!=0))  { L.root=l;
  1028.                         L.height=l->height; 
  1029.                         L.root->father=0;
  1030.                         L.root->index=0;
  1031.                       }
  1032.         else { if ((L.root!=0)&&(l!=0))
  1033.                  {  ab_tree L1(L.a,L.b);
  1034.                 L1.root=l;
  1035.                     L1.height=l->height;
  1036.                     L1.root->father=0;
  1037.                     L1.root->index=0;
  1038.                 concat(L1,L,current_l,current_l_key);
  1039.                 L.root  = L1.root;
  1040.                     L.height= L1.height;
  1041.                     L.count = L1.count;
  1042.  
  1043.                     L1.root=0;
  1044.              }
  1045.              }
  1046.        if ((R.root==0)&&(r!=0))  {R.root=r;
  1047.                       R.height=r->height;
  1048.                       R.root->father=0;
  1049.                       R.root->index=0;
  1050.                                   R.root->p=r->p;
  1051.                      }
  1052.        else { if ((R.root!=0)&&(r!=0))
  1053.                 { ab_tree R1(R.a,R.b);
  1054.             R1.root=r;
  1055.           R1.height=r->height;
  1056.                   R1.root->father=0;
  1057.                   R1.root->index=0;
  1058.                   R1.root->p=r->p;
  1059.           concat(R,R1,current_r,current_r_key);
  1060.                   R1.root=0;
  1061.             }
  1062.             }
  1063.         if (v!=0)
  1064.         {
  1065.          if (index_of_w==1)     /* w is leftmost son of v */
  1066.          { l=0;
  1067.            r=v;
  1068.            current_r=v->same[1];
  1069.            current_r_key=v->k[1];
  1070.        r->same[1]->same[1]=0;        
  1071.        for(i=2;i<r->p;i++) 
  1072.             {  r->son[i-1]=r->son[i];
  1073.               r->son[i-1]->index=i-1;
  1074.             r->k[i-1]=r->k[i];
  1075.            r->same[i-1]=r->same[i];  
  1076.             }
  1077.            r->son[r->p-1]=r->son[r->p];    /* last son */
  1078.            r->son[r->p-1]->index=r->p-1;
  1079.            r->son[r->p]=0;
  1080.            r->p--; 
  1081.            r->k[r->p]=0;
  1082.        r->same[r->p]=0;      
  1083.            if (r->p==1) r=r->son[1];
  1084.          } 
  1085.          else {if ( index_of_w==v->p )
  1086.                  {  r=0;
  1087.                     l=v;
  1088.             l->son[l->p]=0;  /* last son */
  1089.             l->p--;
  1090.             current_l=l->same[index_of_w-1];
  1091.             current_l_key=current_l->k[1];
  1092.                     l->k[l->p]=0;
  1093.             l->same[l->p]->same[1]=0;   
  1094.             l->same[l->p]=0;            
  1095.                     if (l->p==1) l=l->son[1];
  1096.         } 
  1097.                else  /* if w is not the leftmost or rightmost son of v*/
  1098.                {  
  1099.                  r=v;
  1100.                  l=new ab_tree_node(index_of_w-1,v->height,0,0,R.b);
  1101.           current_l=v->same[index_of_w-1];
  1102.           current_l_key=current_l->k[1];
  1103.          current_r=v->same[index_of_w];   
  1104.          current_r_key=current_r->k[1];
  1105.          // current_r=(v->k[index_of_w])-1;   ERROR: liefert neuen Schluessel ;
  1106.                  for(i=1;i<index_of_w-1;i++)
  1107.              {
  1108.            l->son[i]=v->son[i];
  1109.            l->son[i]->index=i;
  1110.                    l->son[i]->father=l;
  1111.            l->k[i]=v->k[i];
  1112.            l->same[i]=v->same[i];  
  1113.            l->same[i]->same[1]=l;  
  1114.           };
  1115.          l->son[index_of_w-1]=v->son[index_of_w-1];
  1116.          l->son[index_of_w-1]->index=index_of_w-1;
  1117.                  l->son[index_of_w-1]->father=l;
  1118.  
  1119.                  r->son[index_of_w] = 0;   // changed
  1120.  
  1121.              for (i=1;i<r->p-index_of_w;i++)
  1122.           {
  1123.            r->son[i]=r->son[i+index_of_w];
  1124.                    r->son[i+index_of_w]=0;
  1125.                     r->son[i]->index=i;
  1126.                    r->son[i]->father=r;
  1127.            r->k[i]=r->k[i+index_of_w];
  1128.            r->same[i]=r->same[i+index_of_w]; 
  1129.                    r->k[i+index_of_w]=0;
  1130.            r->same[i+index_of_w]=0;
  1131.           };
  1132.              r->son[r->p-index_of_w]=r->son[r->p];  /* last son */
  1133.                  r->son[r->p]=0;
  1134.          r->son[r->p-index_of_w]->index=i;
  1135.                  r->son[r->p-index_of_w]->father=r;
  1136.          r->p=r->p-index_of_w;
  1137.                  if (l->p==1) l=l->son[1];
  1138.                  if (r->p==1) r=r->son[1];
  1139.                 };
  1140.                };
  1141.               };
  1142.  
  1143.  /* initialisation for the next iteration  */
  1144.   w=v;
  1145.  }
  1146.  while (w!=0);
  1147.  
  1148.  
  1149.  /* unlink leaves    m.w.         */
  1150.  leaf->same[1]=0;
  1151.  leaf->son[2]->son[1]=0;
  1152.  leaf->son[2]=0;
  1153.  
  1154.  /* redefine maximum and minimum  */
  1155.  L.minimum=minimum;
  1156.  ab_tree_node* help=L.root;
  1157.  while (help->p) help=help->son[help->p];
  1158.  L.maximum=help;
  1159.  help=R.root;
  1160.  while (help->son[1]!=0) help=help->son[1];
  1161.  R.minimum=help;
  1162.  R.maximum=maximum;
  1163.  
  1164.  maximum=minimum=root=l=r=0;
  1165.  height=-1; 
  1166.  count = 0;
  1167.  
  1168.  delete l;
  1169.  delete r;
  1170.  
  1171. }
  1172.  
  1173. void ab_tree::pr_ab_tree(ab_tree_node* localroot,int blancs) const
  1174.  
  1175.   if (localroot==0)
  1176.    { for(int j=1;j<=blancs;j++) cout<<" ";
  1177.      cout << "NIL\n";
  1178.      return;
  1179.     }
  1180.   
  1181.   if (localroot->p == 0) 
  1182.    { for(int j=1;j<=blancs;j++) cout<<" ";
  1183.      print_key(localroot->k[1]); 
  1184. /*
  1185.      ab_tree_node* s= localroot->same[1];
  1186.      cout << " same = "; 
  1187.      if (s) print_key(s->k[1]); 
  1188.      else cout << "NIL";
  1189. */
  1190.      cout << "\n";
  1191.     }
  1192.  
  1193.    else
  1194.     { for(int i=1;i<localroot->p;i++)
  1195.       { pr_ab_tree(localroot->son[i],blancs+10);
  1196.         for(int j=1;j<=blancs;j++) cout<<" ";
  1197.         print_key(localroot->k[i]); 
  1198. /*
  1199.         cout << " same = "; 
  1200.         print_key(localroot->same[i]->k[1]); 
  1201. */
  1202.         cout << "\n";
  1203.        };
  1204.       pr_ab_tree(localroot->son[localroot->p],blancs+10);
  1205.     }
  1206.  
  1207. ab_tree_node* ab_tree::copy_ab_tree(ab_tree_node* localroot,
  1208.                                     abnode& last_leaf,int b) const
  1209.   ab_tree_node* r;
  1210.  
  1211.   if (localroot->p == 0)   //leaf
  1212.    { r=new ab_tree_node(localroot->p,localroot->height,localroot->index,0,1); 
  1213.  
  1214.      r->k[1]= localroot->k[1];
  1215.      r->inf = localroot->inf;
  1216.  
  1217.      copy_key(r->k[1]);
  1218.      copy_inf(localroot->inf);
  1219.  
  1220.      r->son[1]=last_leaf;
  1221.      if (last_leaf) last_leaf->son[2] = r;
  1222.      last_leaf = r;               
  1223.  
  1224.     }
  1225.   else
  1226.    { r=new ab_tree_node(localroot->p,localroot->height,localroot->index,0,b); 
  1227.  
  1228.      for(int i=1;i<localroot->p;i++)
  1229.      { r->son[i]=copy_ab_tree(localroot->son[i],last_leaf,b);
  1230.        r->son[i]->father=r;
  1231.        r->k[i]=localroot->k[i];
  1232.        last_leaf->same[1]=r;        
  1233.        r->same[i]=last_leaf;        
  1234.       }
  1235.  
  1236.      r->son[localroot->p]=copy_ab_tree(localroot->son[localroot->p],last_leaf,b);
  1237.      r->son[localroot->p]->father=r;
  1238.    }
  1239.  
  1240.   return r;
  1241. }
  1242.         
  1243. void ab_tree::del_tree(ab_tree_node* localroot)
  1244. { // deletes subtree  rooted at localroot
  1245.  
  1246.   int k = localroot->p;
  1247.  
  1248.   for(int i=1;i<=k;i++) del_tree(localroot->son[i]);
  1249.  
  1250.   if (k==0) //leaf
  1251.   { clear_key(localroot->k[1]);
  1252.     clear_inf(localroot->inf);
  1253.    }
  1254.  
  1255.   delete localroot;
  1256. }
  1257.  
  1258. void ab_tree::change_inf(ab_tree_node* p, ent x) 
  1259. { clear_inf(p->inf);
  1260.   copy_inf(x);
  1261.   p->inf = x;
  1262.  }
  1263.  
  1264.