Chapter14 - Chapter 14 More Trees Multi-way Trees Trees...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 14 – More Trees Multi-way Trees Trees have a multitude of uses. In many cases, nodes will have a variable number of children. Consider, for example, the problem of maintaining directory of files. In the tree below, rectangular nodes represent “folders”, and oval nodes represent “files”. File nodes never have any children, but folder nodes can have any number of them. Multi-way trees can be implemented using arrays of child pointers (by having each node contain an array of child pointers). This is clumsy. If fixed arrays are used, the number of children a node can have is limited by the array size, and space is wasted whenever a node less than the maximum possible number of children. Dynamically allocated arrays are better, but still less than ideal. There is a better way. This involves using what is in effect a linked list. The parent node refers only to its first child, which refers to the second child, and so on. The diagram on the next page show how the tree above would be implemented using this approach. To find all the children of "root", we first follow the "first child" pointer (solid arrow) for this node. Then we follow the "sibling" pointers (dashed arrows) until we come to the end of the linked list. root ex1 ex2 results problem solution results answer junk question
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The big advantage of this approach is that nodes need only contain two pointers - a “first child pointer” and a “next sibling pointer”. Note that, because file nodes never have any children, they always have a NULL “first child pointer”. class TNode { public: …. data; // can be anything TNode *firstChild, *nextSibling; } In effect, each node contains 1/. the head pointer for a linked list of its own children (firstChild), 2/. a next pointer for a linked list of its parent’s children (nextSibling). These lists may be sorted (as they are in the example tree). root ex1 ex2 results problem solution results answer junk question pointer to next child for the same parent (next “sibling”) pointer to first child
Background image of page 2
The class declaration might appear as follows. class Directory { private: // node type values. enum nodeType { FOLDER = 0, FILE = 1 }; class TNode { public: string name; nodeType type; // FOLDER or FILE TNode *firstChild, *nextSibling; } TNode *root; public: // constructors, methods // return true if and only if the file exists in the directory bool fileExists (string name); } The “enum” declaration in this example goes beyond just defining constants FOLDER
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 9

Chapter14 - Chapter 14 More Trees Multi-way Trees Trees...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online