12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

220 Chap. 6 Non-Binary TreesRVal SizeR 2ABA 3 B 1CDEFC 0 D 0 E 0 F 0(a)(b)Figure 6.12 A dynamic general tree representation with fixed-size arrays for thechild pointers. (a) The general tree. (b) The tree representation. For each node,the first field s<strong>to</strong>res the node value while the second field s<strong>to</strong>res the size of thechild pointer array.space, <strong>and</strong> the old space is then returned <strong>to</strong> free s<strong>to</strong>re. As an alternative <strong>to</strong> relyingon the system’s garbage collec<strong>to</strong>r, a memory manager for variable size s<strong>to</strong>rage unitscan be implemented, as described in Section 12.3. Another possibility is <strong>to</strong> use acollection of free lists, one for each array size, as described in Section 4.1.2. Notein Figure 6.12 that the current number of children for each node is s<strong>to</strong>red explicitlyin a size field. The child pointers are s<strong>to</strong>red in an array with size elements.Another approach that is more flexible, but which requires more space, is <strong>to</strong>s<strong>to</strong>re a linked list of child pointers with each node as illustrated by Figure 6.13.This implementation is essentially the same as the “list of children” implementationof Section 6.3.1, but with dynamically allocated nodes rather than s<strong>to</strong>ring the nodesin an array.6.3.4 Dynamic “Left-Child/Right-Sibling” ImplementationThe “left-child/right-sibling” implementation of Section 6.3.2 s<strong>to</strong>res a fixed numberof pointers with each node. This can be readily adapted <strong>to</strong> a dynamic implementation.In essence, we substitute a binary tree for a general tree. Each node of the“left-child/right-sibling” implementation points <strong>to</strong> two “children” in a new binarytree structure. The left child of this new structure is the node’s first child in thegeneral tree. The right child is the node’s right sibling. We can easily extend thisconversion <strong>to</strong> a forest of general trees, because the roots of the trees can be consideredsiblings. Converting from a forest of general trees <strong>to</strong> a single binary treeis illustrated by Figure 6.14. Here we simply include links from each node <strong>to</strong> itsright sibling <strong>and</strong> remove links <strong>to</strong> all children except the leftmost child. Figure 6.15shows how this might look in an implementation with two pointers at each node.

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

Saved successfully!

Ooh no, something went wrong!