Re: Non-recursive chop and clone for Binary Search Tree - Solutions
- From: moi <root@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 24 Nov 2007 14:21:22 +0100
On Fri, 23 Nov 2007 17:14:39 -0800, Ben Pfaff wrote:
"jehugaleahsa@xxxxxxxxx" <jehugaleahsa@xxxxxxxxx> writes:
At the time, I wasn't in the mood; however, I was recently REQUIRED to
figure it out. The problem: I had an unbalanced tree that needed
destroyed. You can usually get away with a recursive (naive/trivial)
implementation, but you're leaving yourself open for a stack overflow
at some point down the line.
There's no need to use level order for that. I would do it like this:
http://adtinfo.org/libavl.html/Destroying-a-BST-by-Rotation.html
To honor Ben Pfaff's grape-vine, here is my implementation with handles:
struct node {
struct node *nul,*one;
void *payload;
};
void node_eat_tree_by_vinification(struct node **root)
{
struct node *this,*temp;
while (*root) {
this = *root;
if (!this->nul) { *root=this->one;
free(this->payload); free(this); continue; }
temp = this->nul;
this->nul = temp->one;
temp->one = this;
*root = temp;
}
}
AvK
.
- References:
- Non-recursive chop and clone for Binary Search Tree - Solutions
- From: jehugaleahsa@xxxxxxxxx
- Re: Non-recursive chop and clone for Binary Search Tree - Solutions
- From: Ben Pfaff
- Non-recursive chop and clone for Binary Search Tree - Solutions
- Prev by Date: help
- Next by Date: Re: help
- Previous by thread: Re: Non-recursive chop and clone for Binary Search Tree - Solutions
- Next by thread: Re: Non-recursive chop and clone for Binary Search Tree - Solutions
- Index(es):
Relevant Pages
|
|