A little background, I have three classes here in a project: Abstract super class called Tree. sub-class of Tree, called NotEmptyTree. sub-class of Tree, called EmptyTree.
All three classes follow a map like structure, where it takes in a generic any type K Key which implements (extends) comparable and a generic V value.
This is an untraditional BST because it holds other trees not nodes, which I could really use help understanding the whole concept of that, it confuses me a lot, I am so use to the Node structure of BST's.
Right now there is a method which I had help on creating. What it does is make a new copy of a BST without modifying the current object, so this copy is structurally independent. It's only one line but what's happening in the recursion confuses me a lot, I don't understand recursion all that well. I know it's a method that calls itself, while repetitively refining itself until it hits a base case, then it stops and cascades backward collecting all the stuff that its traversed previously. But this method I don't see a base case or the typical recursive structure that I'd see in say, fibonacci. The bulk of the work is in the NotEmptyTree class, the EmptyTree follows the singleton patten and has only that one instance variable for a static object of the class. I think the idea of the EmptyTree is to be a tree without any children at all, where the NotEmptyTree is one which has at least one or two children. But those children are other Trees not nodes.
Here is the method that is inside of the NonEmptyTree class:
@Override
public Tree<K, V> treeCopy() {
return new NotEmptyTree<K, V>(key, value, leftSubTree.treeCopy(), rightSubTree.treeCopy());
}
I am very lost at what is happening here.
Why the "new"?
why are we creating a new NotEmptyTree?
why the K,V ?
why the (key, value ?
why the leftSubTree.treeCopy() ? what happens in the recursive call? why the rightSubTree.treeCopy() ? what happens in the recursive call? Where is the base case?
This class has four instance variables:
private K key;
private V value;
private Tree<K,V> leftSubTree; // what are these doing?
private Tree<K,V> rightSubTree; // same
I don't understand the purpose of the Tree instance variables here. It confuses me how we have data structures from the parent inside of the child class. Not sure what is going on here, or why we need them. Please I need help understanding, I am so lost here.
Thank you