mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-02-04 05:44:35 +00:00
the getMax function in binary search tree may not work correctly #50
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @bayegy on GitHub (Jul 12, 2020).
Hi there,
I am learning data structure. it's really helpful to find such a excellent project. Thanks for the excellent work.
However, I found this code may not work correctly.
08415b0f16/data_structures/binary_trees/binary_search_tree.c (L65)let's say we have a tree like this
node *root = NULL;root = insert(root, 100);root = insert(root, 50);root = insert(root, 25);root = insert(root, 12);root = insert(root, 35);root = insert(root, 75);root = insert(root, 58);root = insert(root, 87);root = insert(root, 150);root = insert(root, 125);root = insert(root, 175);root = insert(root, 168);root = insert(root, 180);When I want to delete 50 from this tree, getMax function returns 25 for the max data of the left subtree of node 50, which should have to be 35. And after I delete 50 from this tree, I used inOrder function to check the updated tree. I got this:
As you can see, the sequence is not correctly ordered.
I think the correct code for getMax should be:
node *getMax(node *root){// If there's no leaf to the right, then this is the maximum key valueif (root->right == NULL)return root;else// root->right = getMax(root->right);return getMax(root->right);}Correct me if I was wrong, please.