I'm trying to create a function that searches through a binary tree looking for duplicate nodes and stores the number of times each unique node appears in the tree into a hashmap.
Here's a more specific version of the question -
"Create a public class called YourBinaryTree that extends BinaryTree. Override protected Map<Object, Integer> valueCount() and have it return a Map mapping each object in the tree to the number of times that it appears. If the tree is empty (root is null) you should return an empty Map. Do not declare any fields in your class"
I'm tried recursively searching through the tree but I can't seem to get it to work because duplicate nodes are creating new mappings and not replace the values of old ones.
Here's the code I've written so far:
import java.util.Map;
import java.util.HashMap;
public class YourBinaryTree extends BinaryTree
{
protected Map<Object, Integer> valueCount()
{
Map<Object, Integer> treeMap = new HashMap<>();
if(root == null)
{
return treeMap;
}
if(root.left == null && root.right == null)
{
treeMap.put(root.value , 1);
return treeMap;
}
return valueCount(root);
}
private Map<Object, Integer> valueCount(Node current)
{
Map<Object, Integer> treeMap = new HashMap<>();
if(current == null)
{
return treeMap;
}
if(treeMap.containsKey(current.value))
{
treeMap.put(current.value, treeMap.get(current) + 1);
}
else
{
treeMap.put(current.value, 1);
}
/*
Map<Object, Integer> treeMapLeft = new HashMap<>();
Map<Object, Integer> treeMapRight = new HashMap<>();
treeMapLeft.putAll(valueCount(current.left));
treeMapRight.putAll(valueCount(current.right));
treeMapRight.forEach(
(key, value)
-> treeMapLeft.merge(key, value, (v1, v2) -> v1+v2));
treeMap = treeMapLeft;
*/
treeMap.putAll(valueCount(current.left));
treeMap.putAll(valueCount(current.right));
return treeMap;
}
}
Here's the code for the class that creates the Binary Tree:
public class BinaryTree
{
protected class Node
{
protected Object value;
protected Node left;
protected Node right;
Node(Object setValue)
{
value = setValue;
}
}
protected Node root;
}
I've tried using the merge function but I'm really sure how to implement it. Any help would be very much appreciated, thank you! :)