Contents    Page-10    Prev    Next    Page+10    Index   

First-Child / Next-Sibling Tree

A node may not have a fixed number of children. Dedicating a large number of links would be wasteful if the average number of children is much smaller than the maximum, and it would still limit the possible number of children.

Luckily, we can use the same structure as the binary tree, with just two links, and have unlimited children with no wasted space.

public class Tree {
    private Object contents;
    private Tree first;
    private Tree next;

    public Tree(Object stuff, Tree child,
                              Tree sibling)
      { contents = stuff;
        first = child;
        next = sibling; }