Algorithms Notes – Implementing Trees

The simplest way to implement a tree would be something like

struct Tree_Node
{
    Element_Type Element;
    Tree_Node* Child_One;
    Tree_Node* Child_Two;
    ...
};

This approach has problems, though. To begin with, the number of children per node can vary greatly and is not known in advance, hence this causes a lot of space to be wasted. A more efficient way to implement a tree would be to store the children of each node in a linked list of nodes.

struct Tree_Node
{
    Element_Type Element;
    Tree_Node* Child_One;
    Tree_Node* Next_Sibling;
};
Tree implementation

Pictorial Representation of a tree implementation using the above-described method.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s