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...

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

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

212 Chap. 6 Non-Binary TreesRWABXYZC D E FParent’s Index 0 0 1 1 1 2 7 7 7LabelRABCNode Index 0 1 2 3 4 5 6 7 8 9 10Figure 6.5 The parent pointer array implementation. Each node corresponds <strong>to</strong>a position in the node array s<strong>to</strong>res its value <strong>and</strong> a pointer <strong>to</strong> its parent. The parentpointers are represented by the position in the array of the parent. The root of anytree s<strong>to</strong>res ROOT, represented graphically by a slash in the “Parent’s Index” box.This figure shows two trees s<strong>to</strong>red in the same parent pointer array, one rootedat R, <strong>and</strong> the other rooted at W.DEIFWXYZABDFJCHEGFigure 6.6 A graph with two connected components.Equivalence classes can be managed efficiently with the UNION/FIND algorithm.Initially, each object is at the root of its own tree. An equivalence pair isprocessed by checking <strong>to</strong> see if both objects of the pair are in the same tree usingfunction differ. If they are in the same tree, then no change need be madebecause the objects are already in the same equivalence class. Otherwise, the twoequivalence classes should be merged by the UNION function.Example 6.2 As an example of solving the equivalence class problem,consider the graph of Figure 6.6. Initially, we assume that each node of thegraph is in a distinct equivalence class. This is represented by s<strong>to</strong>ring eachas the root of its own tree. Figure 6.7(a) shows this initial configuration

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

Saved successfully!

Ooh no, something went wrong!