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

Create successful ePaper yourself

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

Sec. 6.3 General Tree Implement<strong>at</strong>ions 209RVal SizeR 2ABA 3 B 1CDEFC 0 D 0 E 0 F 0(a)(b)Figure 6.12 A dynamic general tree represent<strong>at</strong>ion with fixed-size arrays for thechild pointers. (a) The general tree. (b) The tree represent<strong>at</strong>ion. For each node,the first field stores the node value while the second field stores the size of thechild pointer array.The altern<strong>at</strong>ive is to alloc<strong>at</strong>e variable space for each node. There are two basicapproaches. One is to alloc<strong>at</strong>e an array of child pointers as part of the node. Inessence, each node stores an array-based list of child pointers. Figure 6.12 illustr<strong>at</strong>esthe concept. This approach assumes th<strong>at</strong> the number of children is knownwhen the node is cre<strong>at</strong>ed, which is true for some applic<strong>at</strong>ions but not for others.It also works best if the number of children does not change. If the number ofchildren does change (especially if it increases), then some special recovery mechanismmust be provided to support a change in the size of the child pointer array.One possibility is to alloc<strong>at</strong>e a new node of the correct size from free store <strong>and</strong> returnthe old copy of the node to free store for l<strong>at</strong>er reuse. This works especially wellin a language with built-in garbage collection such as Java. For example, assumeth<strong>at</strong> a node M initially has two children, <strong>and</strong> th<strong>at</strong> space for two child pointers is alloc<strong>at</strong>edwhen M is cre<strong>at</strong>ed. If a third child is added to M, space for a new node withthree child pointers can be alloc<strong>at</strong>ed, the contents of M is copied over to the newspace, <strong>and</strong> the old space is then returned to free store. As an altern<strong>at</strong>ive to relyingon the system’s garbage collector, a memory manager for variable size storage unitscan be implemented, as described in Section 12.3. Another possibility is to use acollection of free lists, one for each array size, as described in Section 4.1.2. Notein Figure 6.12 th<strong>at</strong> the current number of children for each node is stored explicitlyin a size field. The child pointers are stored in an array with size elements.Another approach th<strong>at</strong> is more flexible, but which requires more space, is tostore a linked list of child pointers with each node as illustr<strong>at</strong>ed by Figure 6.13.This implement<strong>at</strong>ion is essentially the same as the “list of children” implement<strong>at</strong>ionof Section 6.3.1, but with dynamically alloc<strong>at</strong>ed nodes r<strong>at</strong>her than storing the nodesin an array.

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

Saved successfully!

Ooh no, something went wrong!