_seg_tree.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 22k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _seg_tree.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. // ------------------------------------------------------------------
  12. //
  13. // full dynamic Segment Trees
  14. //
  15. // Michael Wenzel     (1990)
  16. //
  17. // Implementation as described in
  18. // Kurt Mehlhorn: Data Structures and Algorithms 3
  19. //
  20. // ------------------------------------------------------------------
  21. #include <LEDA/impl/seg_tree.h>
  22. enum left_or_right {left = 0, right = 1};
  23. #undef TEST
  24. #undef DUMP
  25. #ifdef TEST
  26. #define DPRINT(x) cout << x
  27. #else
  28. #define DPRINT(x)
  29. #endif
  30. #ifdef DUMP
  31. #define DDUMP(x) cout << x
  32. #else
  33. #define DDUMP(x)
  34. #endif
  35. ostream& operator<<(ostream& s, h_segment& h) 
  36. { s << "(" << h._x0 << "," << h._x1 << ";" << h._y << ")";
  37.   return s;
  38. }
  39. //-------------------------------------------------------------------
  40. // member functions
  41. //-------------------------------------------------------------------
  42. int seg_node_tree::cmp(GenPtr x,GenPtr y) const
  43. { return father->seg_cmp((h_segment_p)x,(h_segment_p)y); }
  44. // ------------------------------------------------------------
  45. // query
  46. // returns a List of all items it with y0 <= y(it) <= y1
  47. list<seg_tree_item> seg_node_tree::query(GenPtr y0, GenPtr y1)
  48.   DPRINT(" query in nodelistn");
  49.   list<seg_tree_item> L;
  50.  
  51.   h_segment_p r = father->new_y_h_segment(y0);
  52.   seg_tree_item it = locate(r);
  53.   if (it)
  54.   { while ( it!=min() && father->cmp_dim2(key(it)->_y,y0)==0) it = pred(it);
  55.     while ( it && father->cmp_dim2(key(it)->_y,y1)<=0)
  56.     { L.append(it);
  57.       it = succ(it);
  58.      }
  59.    }
  60.   delete r;
  61.   return L;
  62. }
  63. //-------------------------------------------------------------------
  64. // all_items
  65. // returns all items in the nodelist
  66. list<seg_tree_item> seg_node_tree::all_items()
  67. {
  68.   list<seg_tree_item> L;
  69.   seg_tree_item z;
  70.   forall_seg_tree_items(z,*this)
  71.     L.append(z);
  72.   
  73.   return L;
  74. }
  75. //-------------------------------------------------------------------
  76. Segment_Tree::Segment_Tree() : r(this)  {} 
  77. Segment_Tree::~Segment_Tree() { clear(root); }
  78. int Segment_Tree::seg_cmp(h_segment_p p, h_segment_p q)
  79. { int a;
  80.   if (a = cmp_dim2(p->y(), q->y())) return a;
  81.   if (a = cmp_dim1(p->x0(),q->x0())) return a;
  82.   return cmp_dim1(p->x1(), q->x1());
  83. }
  84. // ------------------------------------------------------------
  85. // empty
  86. // returns true, iff for all seg_tree_items it in nodelist(it):
  87. //                 key(it) is not a start- or endoordinate of key(it)
  88. int Segment_Tree::empty(bb1_item it)
  89. {
  90.   DPRINT(" empty of "<<(int)key(it)<<"n");
  91.   int erg=true;
  92.   seg_tree_item i;
  93.   forall_seg_tree_items(i,*info(it))
  94.     if ( (cmp(key(it),x0(i))==0) || (cmp(key(it),x1(i))==0) )
  95.       erg = false;
  96.   DPRINT("empty ergibt "<<erg<<"n");
  97.   return erg;
  98. }   
  99. // ------------------------------------------------------------
  100. // lrot() , rrot() , ldrot() , rdrot()
  101. // Rotationen am Knoten p
  102. void Segment_Tree::lrot(bb1_item p, bb1_item q)
  103. {
  104.   bb1_item h = p->sohn[right];
  105.   DDUMP("lrot "<< (int)key(p)) ; 
  106.     if (q) DDUMP(" Vater " << (int)key(q));
  107.   DDUMP("n");
  108.   p->sohn[right] = h->sohn[left];
  109.   h->sohn[left] = p;
  110.   if (!q)
  111.     root=h;
  112.   else
  113.   { if (cmp(key(h),key(q))>0)
  114.       q->sohn[right]=h;
  115.     else
  116.       q->sohn[left]=h;
  117.   }
  118.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  119.   h->gr=p->groesse()+h->sohn[right]->groesse();
  120. // update nodelists
  121.  seg_tree_item i;
  122.  bb1_item re=p->sohn[right];
  123.  bb1_item s=p->sohn[left];
  124.  seg_node_list help = info(h);
  125.  info(h) = info(p);
  126.  info(p) = new seg_node_tree(this);
  127.  forall_seg_tree_items(i,*(info(re)))
  128.    if ( ( (!re->blatt()) 
  129.   ||((!start_coord(re,i))&&(!end_coord(re,i))) )
  130.       && ( (!s->blatt())
  131.    ||((!start_coord(s,i))&&(!end_coord(s,i))) ) 
  132.       && (info(s)->member(r.key(i))) )
  133.      info(p)->insert(r.key(i));
  134.  
  135.  forall_seg_tree_items(i,*(info(p)))
  136.  { info(re)->del(r.key(i));
  137.    info(s)->del(r.key(i));
  138.  }
  139.  forall_seg_tree_items(i,*help)
  140.  { info(re)->insert(r.key(i));
  141.    info(h->sohn[right])->insert(r.key(i));
  142.  } 
  143.  delete help;
  144. }
  145. void Segment_Tree::rrot(bb1_item p, bb1_item q)
  146. {
  147.   bb1_item h = p->sohn[left];
  148.   DDUMP("rrot "<< (int)key(p)) ; 
  149.     if (q) DDUMP(" Vater " << (int)key(q));
  150.   DDUMP("n");
  151.   p->sohn[left] = h->sohn[right];
  152.   h->sohn[right] = p;
  153.   if (!q) root=h;
  154.   else
  155.   { if (cmp(key(h),key(q))>0)
  156.       q->sohn[right] = h;
  157.     else
  158.       q->sohn[left] = h; }
  159.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  160.   h->gr=p->groesse()+h->sohn[left]->groesse();
  161. // update nodelists
  162.  seg_tree_item i;
  163.  bb1_item re=p->sohn[right];
  164.  bb1_item s=p->sohn[left];
  165.  seg_node_list help = info(h);
  166.  info(h) = info(p);
  167.  info(p) = new seg_node_tree(this);
  168.  /* forall_seg_tree_items(i,*(info(s)))
  169.     if (info(re)->member(r.key(i)))
  170.       info(p)->insert(r.key(i));    */
  171.  
  172.  forall_seg_tree_items(i,*(info(s)))
  173.    if ( ( (!s->blatt()) 
  174.   ||((!start_coord(s,i))&&(!end_coord(s,i))) )
  175.       && ( (!re->blatt())
  176.    ||((!start_coord(re,i))&&(!end_coord(re,i))) ) 
  177.       && (info(re)->member(r.key(i))) )
  178.      info(p)->insert(r.key(i));
  179.  forall_seg_tree_items(i,*(info(p)))
  180.  { info(re)->del(r.key(i));
  181.    info(s)->del(r.key(i));
  182.  }
  183.  forall_seg_tree_items(i,*help)
  184.  { info(s)->insert(r.key(i));
  185.    info(h->sohn[left])->insert(r.key(i));
  186.  } 
  187.  delete help;
  188. }
  189. void Segment_Tree::ldrot(bb1_item p, bb1_item q)
  190. {
  191.   bb1_item h = p->sohn[right];
  192.   bb1_item g = h->sohn[left];
  193.   DDUMP("ldrot "<< (int)key(p)) ; 
  194.     if (q) DDUMP(" Vater " << (int)key(q));
  195.   DDUMP("n");
  196.   rrot(h,p);
  197.   lrot(p,q);
  198.   /*
  199.   p->sohn[right] = g->sohn[left];
  200.   h->sohn[left] =  g->sohn[right];
  201.   g->sohn[left] = p;
  202.   g->sohn[right] = h;
  203.   if (!q) root=g;
  204.   else
  205.   { if (cmp(key(h),key(q))>0)
  206.       q->sohn[right] =g ;
  207.     else 
  208.       q->sohn[left] = g ; }
  209.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  210.   h->gr=h->sohn[left]->groesse()+h->sohn[right]->groesse();
  211.   g->gr=p->groesse()+h->groesse();
  212. // update nodelists
  213.   seg_tree_item i;
  214.   seg_node_list help = info(g);
  215.   info(g) = info(p);
  216.   info(p) = new seg_node_tree(this);
  217.   forall_seg_tree_items(i,*help)
  218.     info(p->sohn[right])->insert(r.key(i));
  219.   forall_seg_tree_items(i,*(info(p->sohn[left])))
  220.     if (info(p->sohn[right])->member(r.key(i)))
  221.       info(p)->insert(r.key(i));
  222.   forall_seg_tree_items(i,*(info(p)))
  223.   { info(p->sohn[left])->del(r.key(i));
  224.     info(p->sohn[right])->del(r.key(i));
  225.   }
  226.   
  227.   forall_seg_tree_items(i,*(info(h)))
  228.     info(p->sohn[right])->insert(r.key(i));
  229.   forall_seg_tree_items(i,*(info(h->sohn[left])))
  230.     if (info(h->sohn[right])->member(r.key(i)))
  231.       info(h)->insert(r.key(i));
  232.   seg_node_list help1 = info(h->sohn[right]);
  233.   info(h->sohn[right]) = new seg_node_tree(this);
  234.   forall_seg_tree_items(i,*help1)
  235.   { if (!((info(h->sohn[left]))->member(r.key(i))))
  236.       info(h->sohn[right])->insert(r.key(i));
  237.     else
  238.       info(h->sohn[left])->del(r.key(i));
  239.   }
  240.   forall_seg_tree_items(i,*help)
  241.     info(h->sohn[left])->insert(r.key(i));
  242.   delete help;
  243.   delete help1;
  244. */
  245.    
  246. }
  247. void Segment_Tree::rdrot(bb1_item p, bb1_item q)
  248. {
  249.   bb1_item h = p->sohn[left];
  250.   bb1_item g = h->sohn[right];
  251.   DDUMP("rdrot "<< (int)key(p)) ; 
  252.     if (q) DDUMP(" Vater " << (int)key(q));
  253.   DDUMP("n");
  254.   lrot(h,p);
  255.   rrot(p,q);
  256. /*  p->sohn[left] = g->sohn[right];
  257.   h->sohn[right] =  g->sohn[left];
  258.   g->sohn[right] = p;
  259.   g->sohn[left] = h;
  260.   if (!q) root=g;
  261.   else
  262.   { if (cmp(key(h),key(q))>0)
  263.       q->sohn[right] =g ;
  264.     else 
  265.       q->sohn[left] = g ; }
  266.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  267.   h->gr=h->sohn[left]->groesse()+h->sohn[right]->groesse();
  268.   g->gr=p->groesse()+h->groesse();
  269. // update nodelists
  270.   seg_tree_item i;
  271.   seg_node_list help = info(g);
  272.   info(g) = info(p);
  273.   info(p) = new seg_node_tree(this);
  274.   forall_seg_tree_items(i,*help)
  275.     info(p->sohn[left])->insert(r.key(i));
  276.   forall_seg_tree_items(i,*(info(p->sohn[right])))
  277.     if (info(p->sohn[left])->member(r.key(i)))
  278.       info(p)->insert(r.key(i));
  279.   forall_seg_tree_items(i,*(info(p)))
  280.   { info(p->sohn[left])->del(r.key(i));
  281.     info(p->sohn[right])->del(r.key(i));
  282.   }
  283.   
  284.   forall_seg_tree_items(i,*(info(h)))
  285.     info(p->sohn[left])->insert(r.key(i));
  286.   forall_seg_tree_items(i,*(info(h->sohn[right])))
  287.     if (info(h->sohn[left])->member(r.key(i)))
  288.       info(h)->insert(r.key(i));
  289.   seg_node_list help1 = info(h->sohn[left]);
  290.   info(h->sohn[left]) = new seg_node_tree(this);
  291.   forall_seg_tree_items(i,*help1)
  292.   { if (!((info(h->sohn[right]))->member(r.key(i))))
  293.       info(h->sohn[left])->insert(r.key(i));
  294.     else
  295.       info(h->sohn[right])->del(r.key(i));
  296.   }
  297.   forall_seg_tree_items(i,*help)
  298.     info(h->sohn[right])->insert(r.key(i));
  299.   delete help;
  300.   delete help1;
  301. */
  302.    
  303. }
  304.  
  305. //-------------------------------------------------------------------
  306. // insert
  307. // insert a segment into a Segment_tree:             
  308. // - insert x-coordinates in main tree (bb-alpha) 
  309. //          and rotate (if necessary)
  310. // - insert segment on nodes 
  311. //          immediately below the paths to x-coordinates
  312. // - insert segment into the tree of all segments
  313. seg_tree_item Segment_Tree::insert(GenPtr x0, GenPtr x1, GenPtr y, GenPtr inf)
  314. {
  315.   DPRINT(" insert segment " << (int)x0 << " " << (int)x1 << " " << (int)y << " in main tree n" );
  316.   seg_tree_item inserted,j;
  317.   if (!(cmp(x0,x1)))                         // empty segment
  318.     return 0;
  319.   copy_dim1(x0);
  320.   copy_dim1(x1);
  321.   copy_dim2(y);
  322.   copy_info(inf);
  323.   h_segment_p i = new h_segment(x0,x1,y,inf);
  324.   if (inserted=r.lookup(i)) 
  325.   { delete i;
  326.     r.info(inserted)=inf;
  327.     return inserted;
  328.   }
  329.   bb1_item t,father,p;
  330.   bb1_item start,end;
  331.   seg_node_list h;
  332. // insert start_coordinate
  333.   if (!(start=bb1_tree::lookup(x0)))        // new coordinate
  334.   { h=new seg_node_tree(this);
  335.     start = sinsert(x0,h);
  336.     t=start;
  337.   
  338.     if (!st.empty())
  339.     { father = st.pop();         // pop father of leaf and set nodelist
  340.       h=new seg_node_tree(this);
  341.       info(father) = h;
  342.       p = succ(t); 
  343.       if (p)
  344.       { list<seg_tree_item> L;
  345.         L=info(p)->all_items();
  346.         forall(j,L)
  347.    { DDUMP("Item "<<*(r.key(j))<<" in Knoten "<<(int)key(p)<<"n");
  348.           if (end_coord(p,j))
  349.             info(t)->insert(r.key(j));
  350.           else if (!start_coord(p,j))
  351.                { info(father)->insert(r.key(j));
  352.                  info(p)->del(r.key(j));
  353.                }
  354.         }
  355.       }
  356.     }
  357.     
  358.                                  // rebalancieren
  359.     while (!st.empty())
  360.     { t=st.pop();
  361.       father = st.empty() ? 0 : st.top();
  362.       t->gr++;  
  363.       float i = t->bal();
  364.       DDUMP("rebal cur=" << (int)key(t) << " groesse= " << t->groesse() << " bal= " << i << " in main treen");
  365.       if (i < alpha)
  366.         if (t->sohn[right]->bal()<=d) lrot(t,father);
  367.         else ldrot(t,father);
  368.       else if (i>1-alpha) 
  369.              if (t->sohn[left]->bal() > d ) rrot(t,father);
  370.              else rdrot(t,father);
  371.     }
  372.   }              // start coordinate inserted
  373. // insert end_coordinate
  374.   if (!(end=bb1_tree::lookup(x1)))   // new coordinate
  375.   { h=new seg_node_tree(this);
  376.     end = sinsert(x1,h);
  377.     t=end;
  378.     if (!st.empty())
  379.     { father = st.pop();         // pop father of leaf and set nodelist
  380.       h=new seg_node_tree(this);
  381.       info(father) = h;
  382.       p = succ(t); 
  383.       if (p)
  384.       { list<seg_tree_item> L;
  385.         L=info(p)->all_items();
  386.         forall(j,L)
  387.      if (end_coord(p,j))
  388.             info(t)->insert(r.key(j));
  389.           else if (!start_coord(p,j))
  390.                { info(father)->insert(r.key(j));
  391.                  info(p)->del(r.key(j));
  392.                }
  393.       }
  394.     }
  395.                                // rebalancieren
  396.     while (!st.empty())
  397.     { t=st.pop();
  398.       father = st.empty() ? 0 : st.top();
  399.       t->gr++;  
  400.       float i = t->bal();
  401.       DDUMP("rebal cur=" << (int)key(t) << " groesse= " << t->groesse() << " bal= " << i << " in main treen");
  402.       if (i < alpha)
  403.         if (t->sohn[right]->bal()<=d) lrot(t,father);
  404.         else ldrot(t,father);
  405.       else if (i>1-alpha) 
  406.              if (t->sohn[left]->bal() > d ) rrot(t,father);
  407.              else rdrot(t,father);
  408.     }
  409.   }              // end coordinate inserted
  410.      // insert segment into nodelists of leaves of coordinates
  411.   info(start)->insert(i);  
  412.   info(end)->insert(i);
  413.   p=t=root;
  414.   GenPtr x2,x3;
  415.                          // same path
  416.   while (p==t)  // start and end coordinate assigned to different leaves
  417.   { x2=key(p);
  418.     DDUMP(" same path " << (int)x0 << " " << (int)x1 << " " << (int)x2 << "n");
  419.     if ( cmp(x0,x2)<=0 ) p=p->sohn[left];
  420.       else p=p->sohn[right];
  421.     if ( cmp(x1,x2)<=0 ) t=t->sohn[left];
  422.       else t=t->sohn[right];
  423.   }
  424.      // now paths to lower and upper part
  425.   while (!p->blatt())                // follow lower path
  426.   { 
  427.     x2=key(p);
  428.     DDUMP(" lower path " << (int)x0 << " " << (int)x2 << "n");
  429.     if ( cmp(x0,x2)>0 ) 
  430.       p=p->sohn[right];
  431.     else
  432.     { info(p->sohn[right])->insert(i);   // insertion into nodelist
  433.       p=p->sohn[left];
  434.     } 
  435.   }
  436.   while (!t->blatt())               // follow upper path
  437.   { 
  438.     x3=key(t);
  439.     DDUMP(" upper path " << (int)x1 << " " << (int)x3 << "n");
  440.     if ( cmp(x1,x3)<=0 ) 
  441.       t=t->sohn[left];
  442.     else
  443.     { info(t->sohn[left])->insert(i);  // insertion into nodelist
  444.       t=t->sohn[right];
  445.     } 
  446.   }
  447. #ifdef DUMP
  448.   print_tree();
  449. #endif
  450.   return (r.insert(i)); 
  451. }
  452. //-------------------------------------------------------------------
  453. // delete
  454. // delete a segment in a Segment_tree:
  455. // - delete segment out of the tree of all segments
  456. // - delete segment on nodes 
  457. //          immediately below the paths to x-coordinates
  458. // - delete x-coordinates in main tree (bb-alpha) 
  459. //          and rotate (if necessary)
  460. void Segment_Tree::del(GenPtr x0, GenPtr x1, GenPtr y)
  461. {
  462.   DPRINT(" delete segment " << (int)x0 << " " << (int)x1 << " " << (int)y << " in main treen");
  463.   if (!(cmp(x0,x1)))                         // empty segment
  464.     return ;
  465.   bb1_item p,q,s,t,father;
  466.   seg_tree_item z;
  467.   seg_node_list help;
  468.   h_segment_p deleted;
  469.   h_segment_p i = new h_segment(x0,x1,y);
  470.   if (!(t=r.lookup(i)))          // segment not in tree
  471.   { delete i;
  472.     DDUMP("delete: segment not in treen");
  473.     return;
  474.   }
  475.   deleted = r.key(t);
  476.   r.del(i);                      // delete in tree of all segments
  477.   s=t=root;
  478.   GenPtr x2,x3;
  479.  // delete segment in nodelists
  480.   while ((s==t)&&(!s->blatt()))       // same path
  481.   { x2=key(s);
  482.     x3=key(t);
  483.     DDUMP(" same path " << (int)x2 << " groesse " << s->groesse() << "n");
  484.     if ( cmp(x0,x2)<=0 ) s=s->sohn[left];
  485.       else s=s->sohn[right];
  486.     if ( cmp(x1,x3)<=0 ) t=t->sohn[left];
  487.       else t=t->sohn[right];
  488.   }
  489.      // now paths to lower and upper part
  490.   while (!s->blatt())                // follow lower path
  491.   { 
  492.     x2=key(s);
  493.     DDUMP(" lower path " << (int)x2 << " groesse " << s->groesse() << "n");
  494.     if ( cmp(x0,x2)>0 ) 
  495.       s=s->sohn[right];
  496.     else
  497.     { info(s->sohn[right])->del(i);   // delete out of nodelist
  498.       s=s->sohn[left];
  499.     } 
  500.   }
  501.   info(s)->del(i);
  502.   while (!t->blatt())               // follow upper path
  503.   { 
  504.     x3=key(t);
  505.     DDUMP(" upper path " << (int)x3 << " groesse " << t->groesse() << "n");
  506.     if ( cmp(x1,x3)<=0 ) 
  507.       t=t->sohn[left];
  508.     else
  509.     { info(t->sohn[left])->del(i);   // delete out of nodelist
  510.       t=t->sohn[right];
  511.     } 
  512.   }
  513.   info(t)->del(i);
  514.     // delete in main tree if necessary
  515.   if(empty(s))                      // delete item of start coordinate
  516.   {
  517.     sdel(x0);
  518.     if (!st.empty())                // father exists 
  519.     { q = st.pop();                 // pop father of leaf and set nodelist
  520.       if (!st.empty())
  521.       { p = st.top();
  522. if (cmp(key(q),key(p))<=0)  // left son deleted
  523.           p = p->sohn[left];
  524.         else                        // right son deleted
  525.   p = p->sohn[right];
  526.       } 
  527.       else                          // root deleted 
  528. p = root;
  529.                                           // set nodelist
  530.       help = info(p);
  531.       info(p) = info(q);
  532.       if (p->blatt())
  533. forall_seg_tree_items(z,*help)
  534.   if ((start_coord(p,z))||(end_coord(p,z)))
  535.     info(p)->insert(r.key(z));
  536.       delete help;
  537.       delete q;
  538.     }
  539.     delete info(s);
  540.     delete s;
  541.                                // rebalancieren
  542.     while (!st.empty())
  543.     { p=st.pop();
  544.       father = st.empty() ? 0 : st.top();
  545.       p->gr--;  
  546.       float i = p->bal();
  547.       DDUMP("rebal cur=" << (int)key(p) << " groesse= " << p->groesse() << " bal= " << i << " in main tree n");
  548.       if (i < alpha)
  549.         if (p->sohn[right]->bal()<=d) lrot(p,father);
  550.         else ldrot(p,father);
  551.       else if (i>1-alpha) 
  552.              if (p->sohn[left]->bal() > d ) rrot(p,father);
  553.       else rdrot(p,father);
  554.     }
  555.   }
  556.   if(empty(t))                      // delete item of end coordinate
  557.   {
  558.     sdel(x1);
  559.     if (!st.empty())                // father exists 
  560.     { q = st.pop();                 // pop father of leaf and set nodelist
  561.       if (!st.empty())
  562.       { p = st.top();
  563. if (cmp(key(q),key(p))<=0)  // left son deleted
  564.           p = p->sohn[left];
  565.         else                        // right son deleted
  566.   p = p->sohn[right];
  567.       } 
  568.       else                          // root deleted 
  569. p = root;
  570.                                           // set nodelist
  571.       help = info(p);
  572.       info(p) = info(q);
  573.       if (p->blatt())
  574. forall_seg_tree_items(z,*help)
  575.   if ((start_coord(p,z))||(end_coord(p,z)))
  576.     info(p)->insert(r.key(z));
  577.       delete help;
  578.       delete q;
  579.     }
  580.     delete info(t);
  581.     delete t;
  582.                                // rebalancieren
  583.     while (!st.empty())
  584.     { p=st.pop();
  585.       father = st.empty() ? 0 : st.top();
  586.       p->gr--;  
  587.       float i = p->bal();
  588.       DDUMP("rebal cur=" << (int)key(p) << " groesse= " << p->groesse() << " bal= " << i << " in main tree n");
  589.       if (i < alpha)
  590.         if (p->sohn[right]->bal()<=d) lrot(p,father);
  591.         else ldrot(p,father);
  592.       else if (i>1-alpha) 
  593.              if (p->sohn[left]->bal() > d ) rrot(p,father);
  594.       else rdrot(p,father);
  595.     }
  596.   }
  597. #ifdef DUMP
  598.   print_tree();
  599. #endif
  600.   clear_dim1(x0);
  601.   clear_dim1(x1);
  602.   clear_dim2(y);
  603.   clear_info(inf(deleted));
  604.   delete i;
  605.   delete deleted;
  606. }
  607. //-------------------------------------------------------------------
  608. // query
  609. // returns all items it in the Segment_tree with
  610. //     x0(it) <= x <= x1(it) and y0 <= y(it) <= y1
  611. //    
  612. // by:
  613. // - look for x in the main tree   
  614. //   
  615. // - for all nodes on the path perform a query(y0,y1) on nodelists
  616. list<seg_tree_item> Segment_Tree::query(GenPtr x, GenPtr y0, GenPtr y1)
  617.   DPRINT("query in main tree " << (int)x << " " << (int)y0 << " " << (int)y1 << "n");
  618.   bb1_item it;
  619.   seg_tree_item z;
  620.   list<seg_tree_item> L,l;
  621.   search(x);
  622.   if (st.empty()) return L;
  623.   if (cmp(x,key(st.top()))==0)             // x-coordinate in tree
  624.     while (!st.empty())
  625.     { it = st.pop();
  626.       l = info(it)->query(y0,y1);
  627.       L.conc(l);
  628.     }
  629.   else                                     // x-coordinate not in tree
  630.   {
  631.      if ((cmp(x,key(bb1_tree::min()))<0) || (cmp(x,key(bb1_tree::max()))>0))
  632.        return L;
  633.    
  634.      list<seg_tree_item> L1;
  635.      while (!st.empty())
  636.      { it = st.pop();
  637.        L1 = info(it)->query(y0,y1);
  638.        forall(z,L1)
  639.  if ((cmp(x,x0(z))>=0) && (cmp(x,x1(z))<=0))
  640.    L.append(z);
  641.        L1.clear();
  642.      }
  643.   }
  644.   return L;
  645. }
  646. //-------------------------------------------------------------------
  647. // x_infinity_query
  648. // returns a List of all items it with y0 <= y0(it) <= y1
  649. list<seg_tree_item> Segment_Tree::x_infinity_query(GenPtr y0, GenPtr y1)
  650. {
  651.   return r.query(y0,y1);
  652. }
  653. //-------------------------------------------------------------------
  654. // y_infinity_query
  655. // returns a List of all items it with x0(it) <= x <= x1(it)
  656. list<seg_tree_item> Segment_Tree::y_infinity_query(GenPtr x)
  657. {
  658.   bb1_item it;
  659.   seg_tree_item z;
  660.   list<seg_tree_item> L;
  661.   search(x);
  662.   if (st.empty()) return L;
  663.   if (cmp(x,key(st.top()))==0)             // x-coordinate in tree
  664.     while (!st.empty())
  665.     { it = st.pop();
  666.       forall_seg_tree_items(z,*(info(it)))
  667.       L.append(z);
  668.     }
  669.   else                                     // x-coordinate not in tree
  670.   {
  671.      if ((cmp(x,key(bb1_tree::min()))<0) || (cmp(x,key(bb1_tree::max()))>0))
  672.        return L;
  673.    
  674.      list<seg_tree_item> L1;
  675.      while (!st.empty())
  676.      { it = st.pop();
  677.        forall_seg_tree_items(z,*(info(it)))
  678.  if ((cmp(x,x0(z))>=0) && (cmp(x,x1(z))<=0))
  679.    L.append(z);
  680.      }
  681.   }
  682.   return L;
  683. }
  684. //-------------------------------------------------------------------
  685. // all_items
  686. // returns all items in the tree
  687. list<seg_tree_item> Segment_Tree::all_items()
  688. {
  689.   return r.all_items();
  690. }
  691. //-------------------------------------------------------------------
  692. // lookup
  693. // returns a seg_tree_item it with key(it) = (x0,x1,y) if there is one
  694. seg_tree_item Segment_Tree::lookup(GenPtr x0, GenPtr x1, GenPtr y)
  695.   DPRINT(" lookup in main tree " << (int)x0 << " " << (int)x1 << " " << (int)y << "n");
  696.   h_segment_p z = new h_segment(x0,x1,y);
  697.   seg_tree_item i = r.lookup(z);   
  698.   delete z;
  699.   return i;
  700. }
  701. //-------------------------------------------------------------------
  702. // clear_tree
  703. // delete all Items and Tree of Items
  704. void Segment_Tree::clear_tree()
  705. {
  706.   if (!root) return;
  707.   clear(root);
  708.   seg_tree_item z;
  709.   h_segment_p q;
  710.   forall_seg_tree_items(z,r)
  711.   { 
  712.     q=r.key(z);
  713.     clear_dim1(x0(q));
  714.     clear_dim1(x1(q));
  715.     clear_dim2(y(q));
  716.     clear_info(inf(q));
  717.     delete q;
  718.   }
  719.   r.clear();
  720.   first = iterator = 0;
  721.   anzahl = 0;
  722. }
  723. //-------------------------------------------------------------------
  724. // clear
  725. // delete nodelists and nodes
  726. void Segment_Tree::clear(bb1_item& it)
  727. {
  728.   if (it == 0) return;
  729.   
  730.   if(!it->blatt())
  731.   { clear(it->sohn[left]);
  732.     clear(it->sohn[right]);
  733.   }
  734.   delete info(it);
  735.   delete it;
  736.   it = 0;
  737. }
  738. //-------------------------------------------------------------------
  739. // print     
  740. // prints the tree with nodelists
  741. void Segment_Tree::print(bb1_item it,string s)
  742. {
  743.   if (!it) return;
  744.   seg_tree_item i;
  745.   if (!it->blatt())
  746.     print(it->sohn[left],s + string("     "));
  747.   cout<< s << key(it) << "n";
  748.   cout<< s ; 
  749.   forall_seg_tree_items(i,*info(it))
  750.     cout << "[" << r.key(i) << "]:" << *(r.key(i)) << " ";
  751.   newline;
  752.   if (!it->blatt())
  753.     print(it->sohn[right],s + string("     "));
  754. }