What if there are same data of element is present multiple times.
2. in case of 2 child then when we call of delete in best on right of tree with giving replace->data what if we find that data before reaching to the one we want to delete.
What if there are same data of element is present multiple times
Hello @anubhavb11,
As we are talking about BST, so, it won’t make a difference.
Reason:
The leftmost of the right subtree will be the same element.
You can try executing your code for a test case like this.
yea got it for that
but what if we want to delete 2 elements it only delete element it encounter first
Hello @anubhavb11,
Then, make some changes in this to ensure that will traverse all the nodes.
The code explained in the video will only delete the first occurance.
any hint how can i let it traverse whole tree
Hello @anubhavb11,
You need not to iterate the BST completely.
Reason:
Because it is BST, so all the elements with same value will be placed consecutively or one after another.
Make sure while building the BST, you must insert all the repeating elements to it’s right subtree.
You can do the follow:
-
if node to be deleted has no child: return NULL
-
if node to be delete do not have right sub-tree: do the same as you were doing earlier.
Reason:
Repeating elements are on right substree -
if node to be delete do not have right sub-tree: after deleting the current element, call the delete function again passing root->right as root in it.
then return the value returned from this recursive call. -
For node with two children: do the same you were doing earlier before returning, check if the new root is equal to the deleting element(this is the repeating element) and do the replace logic again. this should be repeated unless root after swapping becomes other than the deleting element.
This way you will delete all the occurances.
hii you explain so amazing could you help me with one thing
i have to explain – Use push_back for vector pair in C++
can you please give me a good write up like this so if anyone read can understand this topic please please it will be a huge help for me
like just give me a intro or basic important stuff i will elaborate it add code and all in it
i have written the code also - https://ide.codingblocks.com/s/254300
Hey @anubhavb11,
Please, raise a separate doubt for it and mark this one as resolved.
Also, i didn’t understood what exactly you want me to do.
So, give a proper explanation about what do you want me to do.
Waiting for your doubt.
it is not a doubt i am asking for a help
i asked you to explain — Use push_back for vector pair in C++
Okay @anubhavb11,
So, it’s the same as you do for vector.
vector_name.push_back(element_of_the_type_that_vector_stores)
In the case of vector of pair stores pairs in it.
So, you are required to insert a pair rather than just an number.
How would you create a pair?
-
make_pair() function as you have done in your code like
v.push_back(make_pair(a,b)) -
just use curly braces and place the pair of value inside it like
v.push_back({a,b})
Hope, this would help.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.