15,123,118 members
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
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