Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we replace an element A with element B in a binary search tree where duplications of element A are added to element B? For example, if I replace 11(1) with 14(4) the result will be 14(5).


What I have tried:

I used this method to replace the key, but I have no idea how to add the number of duplicates.

public void ReplaceDuplicatesOfElement(Node root, int A, int B) {

if (root == null) {

return;

}

ReplaceDuplicatesOfElement(root.left, A, B);

if (root.k == A) {

System.out.print(B + "(" + ( root.data ) + ") ");

} else if (root.k != B) {

System.out.print(root.k + "(" + root.data + ") ");

}

ReplaceDuplicatesOfElement(root.right, A, B);

}

the output
Find and replace all duplicates of an element A with element B 
  Replace 11 with 14 
- Before replace: 7(3) 8(3) 11(1) 14(4) 18(1) 
- After replace: 7(3) 8(3) 14(1) 18(1) 

this function replace only the key not duplicates.

*************************************************************

Also tried to use another way by deleting the first value (A) and doing the second value insert (B). But this function deletes the value of (A) from the tree and this should not happen in future assignment points (because this is just one point from the whole assignment )

the output
* Find and replace all duplicates of an element A with element B 
  Replace 11 with 14 
- Before replace: 7(3) 8(3) 11(1) 14(4) 18(1) 
- After replace: 7(3) 8(3) 14(5) 18(1) 
everything here is okay but.. 

+ Point 3 
* Show all the tree together with the duplicates (pre-order, in-order, post-order, level-order)
  In-order Traversal 
7(3) 8(3) 14(5) 18(1) 


here in point three, 11 is missing.
Can you help me?
Posted
Comments
Maciej Los 11-Feb-21 6:35am    
I do not see duplicates!
Member 15069835 11-Feb-21 6:51am    
In binary search tree duplicates are not allowed, so we count how many times it has been used. The numbers in parentheses indicate how many times it has been used a number

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900