I need to create an unsorted binary tree (one requirement is unsorted) that contains String as the value. My class diagram looks like this:
public class Node { private String desc; private Node leftNode = null; private Node rightNode = null; public Node(String desc) { this.desc = desc; } public String getDesc() { return desc; } public Node getLeftNode() { return leftNode; } public Node getRightNode() { return rightNode; } }
In the end, I want to replace any node that matches the String description with a new node that has a new description (including duplicates with the old description).
So my question is: what's the best way to handle a Node insert when creating an unsorted binary tree?
I thought of two ways. The first would be to simply have two methods: setLeftNode(Node root, String desc) and setRightNode(Node root, String desc) , which someone could call using Node of their choice as the root. If there is already a left / right Node , then it will simply move down until it hits a node that has no left Node left. But this can introduce problems producing super large heights.
The second way, which I thought was to have a dedicated root Node , in this case create the first Node , and then just create a new Node in order.
So what is the best way to create an unsorted binary tree?
source share