Add solution for name removal (bst).
This commit is contained in:
parent
1c76a98186
commit
5a4a806ed4
@ -18,7 +18,7 @@ int main()
|
|||||||
printf("\nNames in archive:\n");
|
printf("\nNames in archive:\n");
|
||||||
printNames();
|
printNames();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
/*
|
|
||||||
for(int i = 0; i < lenNamesToRemove; i++)
|
for(int i = 0; i < lenNamesToRemove; i++)
|
||||||
{
|
{
|
||||||
printf("Removing %s ...\n", namesToRemove[i]);
|
printf("Removing %s ...\n", namesToRemove[i]);
|
||||||
@ -28,7 +28,7 @@ int main()
|
|||||||
printf("\nNames in archive:\n");
|
printf("\nNames in archive:\n");
|
||||||
printNames();
|
printNames();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
*/
|
|
||||||
printf("Clearing archive ...\n");
|
printf("Clearing archive ...\n");
|
||||||
clearArchive();
|
clearArchive();
|
||||||
|
|
||||||
|
|||||||
@ -68,29 +68,6 @@ void printNames()
|
|||||||
{
|
{
|
||||||
printNamesRec(root);
|
printNamesRec(root);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
|
||||||
static QueueElem *removeElemRek(QueueElem *elem, const char *name)
|
|
||||||
{
|
|
||||||
if(elem != NULL)
|
|
||||||
{
|
|
||||||
if(strcmp(elem->name, name) == 0)
|
|
||||||
{
|
|
||||||
QueueElem *elemToRemove = elem;
|
|
||||||
elem = elem->next;
|
|
||||||
disposeElem(elemToRemove);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
elem->next = removeElemRek(elem->next, name);
|
|
||||||
}
|
|
||||||
return elem;
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeName(const char *name)
|
|
||||||
{
|
|
||||||
head = removeElemRek(head, name);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static void disposeNode(Node *node)
|
static void disposeNode(Node *node)
|
||||||
{
|
{
|
||||||
@ -99,6 +76,62 @@ static void disposeNode(Node *node)
|
|||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Node *removeNameRec(Node *root, const char *name)
|
||||||
|
{
|
||||||
|
if(root != NULL)
|
||||||
|
{
|
||||||
|
if(strcmp(name, root->name) < 0)
|
||||||
|
root->left = removeNameRec(root->left, name);
|
||||||
|
else if(strcmp(name, root->name) > 0)
|
||||||
|
root->right = removeNameRec(root->right, name);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(root->left == NULL)
|
||||||
|
{
|
||||||
|
Node *nodeToRemove = root;
|
||||||
|
root = root->right;
|
||||||
|
disposeNode(nodeToRemove);
|
||||||
|
}
|
||||||
|
else if(root->right == NULL)
|
||||||
|
{
|
||||||
|
Node *nodeToRemove = root;
|
||||||
|
root = root->left;
|
||||||
|
disposeNode(nodeToRemove);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Node *succParent = root;
|
||||||
|
Node *succ = succParent->right;
|
||||||
|
|
||||||
|
while(succ->left != NULL)
|
||||||
|
{
|
||||||
|
succParent = succ;
|
||||||
|
succ = succ->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(succParent == root)
|
||||||
|
{
|
||||||
|
succParent->right = succ->right;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
succParent->left = succ->right;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(root->name);
|
||||||
|
root->name = succ->name;
|
||||||
|
free(succ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeName(const char *name)
|
||||||
|
{
|
||||||
|
root = removeNameRec(root, name);
|
||||||
|
}
|
||||||
|
|
||||||
static void clearNodeRec(Node *node)
|
static void clearNodeRec(Node *node)
|
||||||
{
|
{
|
||||||
if(node != NULL)
|
if(node != NULL)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user