static PropPtr remove_propnode(char *key, PropPtr * root) { PropPtr save; PropPtr tmp; PropPtr avl = *root; int cmpval; save = avl; if (avl) { cmpval = Comparator(key, PropName(avl)); if (cmpval < 0) { save = remove_propnode(key, &AVL_LF(avl)); } else if (cmpval > 0) { save = remove_propnode(key, &AVL_RT(avl)); } else if (!(AVL_LF(avl))) { avl = AVL_RT(avl); } else if (!(AVL_RT(avl))) { avl = AVL_LF(avl); } else { tmp = remove_propnode(PropName(getmax(AVL_LF(avl))), &AVL_LF(avl)); if (!tmp) abort(); /* this shouldn't be possible. */ AVL_LF(tmp) = AVL_LF(avl); AVL_RT(tmp) = AVL_RT(avl); avl = tmp; } if (save) { AVL_LF(save) = NULL; AVL_RT(save) = NULL; } *root = balance_node(avl); } return save; }
/** * Remove a propnode named 'key' from the AVL root 'root'. This function * is recursive. * * The node (removed from the AVL structure) is returned, or NULL if it * was not found. * * @private * @param key the prop name to remove * @param root the root node to start the removal process from * * @return the removed node - you should probably free it with free_propnode */ static PropPtr remove_propnode(char *key, PropPtr * root) { PropPtr save; PropPtr tmp; PropPtr avl = *root; int cmpval; save = avl; if (avl) { cmpval = strcasecmp(key, PropName(avl)); if (cmpval < 0) { save = remove_propnode(key, &(avl->left)); } else if (cmpval > 0) { save = remove_propnode(key, &(avl->right)); } else if (!(avl->left)) { avl = avl->right; } else if (!(avl->right)) { avl = avl->left; } else { tmp = remove_propnode(PropName(getmax(avl->left)), &(avl->left)); if (!tmp) { /* this shouldn't be possible. */ panic("remove_propnode() returned NULL !"); } tmp->left = avl->left; tmp->right = avl->right; avl = tmp; } if (save) { save->left = NULL; save->right = NULL; } *root = balance_node(avl); } return save; }
static PropPtr delnode(char *key, PropPtr avl) { PropPtr save; save = remove_propnode(key, &avl); if (save) free_propnode(save); return avl; }