11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

206 Chap. 6 Non-Binary Treeslence oper<strong>at</strong>ion. This is an example of amortized analysis, discussed further inSection 14.3.6.3 General Tree Implement<strong>at</strong>ionsWe now tackle the problem of devising an implement<strong>at</strong>ion for general trees th<strong>at</strong>allows efficient processing for all member functions of the ADTs shown in Figure6.2. This section presents several approaches to implementing general trees.Each implement<strong>at</strong>ion yields advantages <strong>and</strong> disadvantages in the amount of spacerequired to store a node <strong>and</strong> the rel<strong>at</strong>ive ease with which key oper<strong>at</strong>ions can beperformed. General tree implement<strong>at</strong>ions should place no restriction on how manychildren a node may have. In some applic<strong>at</strong>ions, once a node is cre<strong>at</strong>ed the numberof children never changes. In such cases, a fixed amount of space can be alloc<strong>at</strong>edfor the node when it is cre<strong>at</strong>ed, based on the number of children for the node. M<strong>at</strong>tersbecome more complic<strong>at</strong>ed if children can be added to or deleted from a node,requiring th<strong>at</strong> the node’s space alloc<strong>at</strong>ion be adjusted accordingly.6.3.1 List of ChildrenOur first <strong>at</strong>tempt to cre<strong>at</strong>e a general tree implement<strong>at</strong>ion is called the “list of children”implement<strong>at</strong>ion for general trees. It simply stores with each internal node alinked list of its children. This is illustr<strong>at</strong>ed by Figure 6.9.The “list of children” implement<strong>at</strong>ion stores the tree nodes in an array. Eachnode contains a value, a pointer (or index) to its parent, <strong>and</strong> a pointer to a linked listof the node’s children, stored in order from left to right. Each linked list elementcontains a pointer to one child. Thus, the leftmost child of a node can be founddirectly because it is the first element in the linked list. However, to find the rightsibling for a node is more difficult. Consider the case of a node M <strong>and</strong> its parent P.To find M’s right sibling, we must move down the child list of P until the linked listelement storing the pointer to M has been found. Going one step further takes us tothe linked list element th<strong>at</strong> stores a pointer to M’s right sibling. Thus, in the worstcase, to find M’s right sibling requires th<strong>at</strong> all children of M’s parent be searched.Combining trees using this represent<strong>at</strong>ion is difficult if each tree is stored in asepar<strong>at</strong>e node array. If the nodes of both trees are stored in a single node array, thenadding tree T as a subtree of node R is done by simply adding the root of T to R’slist of children.6.3.2 The Left-Child/Right-Sibling Implement<strong>at</strong>ionWith the “list of children” implement<strong>at</strong>ion, it is difficult to access a node’s rightsibling. Figure 6.10 presents an improvement. Here, each node stores its value<strong>and</strong> pointers to its parent, leftmost child, <strong>and</strong> right sibling. Thus, each of the basic

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!