http://qs321.pair.com?node_id=252412


in reply to Deleting a branch from a tree structure

sub rec_delete { my $id=shift; foreach my $id (select id where parent=$id) { rec_delete($id); } delete($id); }

---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Replies are listed 'Best First'.
Re: Re: Deleting a branch from a tree structure
by hv (Prior) on Apr 23, 2003 at 16:58 UTC

    This deletes referenced categories before the category that refers to them, which I think leaves open a window of brokenness, at least of referential integrity.

    It might be better to do it the other way round:

    sub rec_delete { my $id = shift; my @kids = (... select id where parent = $id ...); delete($id); rec_delete($_) for @kids; }

    Update: this is wrong - I was thinking "delete the categories this category refers to" rather than "delete the categories that refer to this category".

    Hugo

      Hmm. If the child refrences the parent, then we can't delete the parent until we remove the childs reference to it, or remove the child itself of course. So we have to do a postorder traversal of the implicit tree. A preorder traversal as you have here would try to delete the parent while the children point at it.

      Im assuming that the OP was using using the only sane way I know of to represent an nary tree in a single table form, that is using a parent pointer list representation. Now these may have been asumptions too far but... :-)


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...