Remoção de Elementos de uma BST

From Wiki**3

Revision as of 21:03, 18 May 2005 by Root (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A função de remoção actua recursivamente. Quanto um elemento é removido, as suas duas sub-árvores são ligadas e ancoradas na árvore principal.

 link deleteR(link h, Key v) {
   Key t = key(h->item);
   if (h == z) return z;
   if (less(v, t)) { h->l = deleteR(h->l, v); }
   if (less(t, v)) { h->r = deleteR(h->r, v); }
   if (eq(t, v))   {
     link x = h; h = joinLR(h->l, h->r); free(x);
   }
   return h;
 }
 
 void STdelete(Key v) { head = deleteR(head, v); }