8.6 Copying |
We can use the tree copy function for TBSTs almost verbatim here. The one necessary change is that copy_node() must copy node balance factors. Here's the new version:
328. <TAVL node copy function 328> = static int
copy_node (struct tavl_table *tree,
struct tavl_node *dst, int dir, const struct tavl_node *src, tavl_copy_func *copy)
{ struct tavl_node *new =
tree->tavl_alloc->libavl_malloc (tree->tavl_alloc, sizeof *new); if (new == NULL) return 0; new->tavl_link[dir] = dst->tavl_link[dir]; new->tavl_tag[dir] = TAVL_THREAD; new->tavl_link[!dir] = dst; new->tavl_tag[!dir] = TAVL_THREAD; dst->tavl_link[dir] = new; dst->tavl_tag[dir] = TAVL_CHILD; new->tavl_balance = src->tavl_balance; if (copy == NULL) new->tavl_data = src->tavl_data; else
{ new->tavl_data = copy (src->tavl_data, tree->tavl_param); if (new->tavl_data == NULL) return 0; } return 1; }
This code is included in 329.
329. <TAVL copy function 329> = <TAVL node copy function 328> <TBST copy error helper function; tbst => tavl 280> <TBST main copy function; tbst => tavl 279>