views:

152

answers:

2

I have to solve the following constructor for a BinaryTree class in java:

BinaryTree(GeneralTree<T> aTree)

This method should create a BinaryTree (bt) from a General Tree (gt) as follows:

Every Vertex from gt will be represented as a leaf in bt.

  • If gt is a leaf, then bt will be a leaf with the same value as gt
  • If gt is not a leaf, then bt will be constructed as an empty root, a left subTree (lt) and a right subTree (lr). Lt is a stric binary tree created from the oldest subtree of gt (the left-most subtree) and lr is a stric binary tree created from gt without its left-most subtree.

The frist part is trivial enough, but the second one is giving me some trouble. I've gotten this far:

public BinaryTree(GeneralTree<T> aTree){
        if (aTree.isLeaf()){
            root= new BinaryNode<T>(aTree.getRootData());
        }else{
            root= new BinaryNode<T>(null); // empty root
            LinkedList<GeneralTree<T>> childs = aTree.getChilds(); // Childs of the GT are implemented as a LinkedList of SubTrees
            child.begin(); //start iteration trough list
            BinaryTree<T> lt = new BinaryTree<T>(childs.element(0)); // first element = left-most child
            this.addLeftChild(lt);
            aTree.DeleteChild(hijos.elemento(0));
            BinaryTree<T> lr = new BinaryTree<T>(aTree);
            this.addRightChild(lr);
        }
    }

Is this the right way? If not, can you think of a better way to solve this? This solution, for example, gives me a bunch of nodes with no data at all, I don't know if this is an issue of the problem itself or mine.

Thank you!

+2  A: 

The problem is that most trees cannot be validly reduced to a binary tree. Reading your comment you are fully aware of that. Taking for example a tree with a root node with 3 children. There is no direct way to make a binary tree out of this without sacrificing connectivity. That's where those empty nodes come from. With them, the structure of the general tree is preserved. You can reconstruct it, deleting the empty nodes and reassembling the tree from the two subtrees.

I have not debugged your code. If it does what you said it would do, it is a good solution. Empty nodes sort of store the connectivity information of the general tree. They are allowed to be there.

AndreasT
Thanks for the reply! Seeing the empty nodes as a way to retain the structure of the general tree does make sense.
mno4k
+1  A: 

There is another, widely known, way to make a binary tree from a general tree, with no "connectivity nodes". This method can be best understood like this:

Node{                Node{
 data;                data;
 first_child;   =>    left;
 next_sibling;        right; 
}                    }

This basically represents the list of children of the general tree as a linked list, with the addition of each node having a reference to the linked list of it's children. As you can see, this is structurally equivalent to a binary tree.

So, in pseudocode (with edge cases omitted for simplicity)

BinaryTree(gtree){
    root=BinaryNode(gtree.data,BinaryNode(gtree.children),null);
}

BinaryNode(List<gnode> sibs){
    BinaryNode(sibs.first.data,BinaryNode(sibs.first.children),BinaryTree(sibs.rest));
}

BinaryNode(data,left,right){
    data=data;
    left=left;
    right=right;
}

Of course, if you need to have the structure you described, this will be useless, but in general, this is a fairly good way to create a binary tree from a general tree.

Mitten.O
Thanks for the answer, Mitten! Actually I do need yo have the structre I described, but this is a good way to go from GT to BT, I'll keep it in mind.
mno4k