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.2 The Parent Pointer Implement<strong>at</strong>ion 1996.2 The Parent Pointer Implement<strong>at</strong>ionPerhaps the simplest general tree implement<strong>at</strong>ion is to store for each node only apointer to th<strong>at</strong> node’s parent. We will call this the parent pointer implement<strong>at</strong>ion.Clearly this implement<strong>at</strong>ion is not general purpose, because it is inadequ<strong>at</strong>e forsuch important oper<strong>at</strong>ions as finding the leftmost child or the right sibling for anode. Thus, it may seem to be a poor idea to implement a general tree in thisway. However, the parent pointer implement<strong>at</strong>ion stores precisely the inform<strong>at</strong>ionrequired to answer the following, useful question: “Given two nodes, are they inthe same tree?” To answer the question, we need only follow the series of parentpointers from each node to its respective root. If both nodes reach the same root,then they must be in the same tree. If the roots are different, then the two nodes arenot in the same tree. The process of finding the ultim<strong>at</strong>e root for a given node wewill call FIND.The parent pointer represent<strong>at</strong>ion is most often used to maintain a collection ofdisjoint sets. Two disjoint sets share no members in common (their intersection isempty). A collection of disjoint sets partitions some objects such th<strong>at</strong> every objectis in exactly one of the disjoint sets. There are two basic oper<strong>at</strong>ions th<strong>at</strong> we wish tosupport:(1) determine if two objects are in the same set, <strong>and</strong>(2) merge two sets together.Because two merged sets are united, the merging oper<strong>at</strong>ion is called UNION <strong>and</strong>the whole process of determining if two objects are in the same set <strong>and</strong> then mergingthe sets goes by the name “UNION/FIND.”To implement UNION/FIND, we represent each disjoint set with a separ<strong>at</strong>egeneral tree. Two objects are in the same disjoint set if they are in the same tree.Every node of the tree (except for the root) has precisely one parent. Thus, eachnode requires the same space to represent it. The collection of objects is typicallystored in an array, where each element of the array corresponds to one object, <strong>and</strong>each element stores the object’s value. The objects also correspond to nodes inthe various disjoint trees (one tree for each disjoint set), so we also store the parentvalue with each object in the array. Those nodes th<strong>at</strong> are the roots of their respectivetrees store an appropri<strong>at</strong>e indic<strong>at</strong>or. Note th<strong>at</strong> this represent<strong>at</strong>ion means th<strong>at</strong> a singlearray is being used to implement a collection of trees. This makes it easy to mergetrees together with UNION oper<strong>at</strong>ions.Figure 6.4 shows the parent pointer implement<strong>at</strong>ion for the general tree, calledParPtrTree. This class is gre<strong>at</strong>ly simplified from the declar<strong>at</strong>ions of Figure 6.2because we need only a subset of the general tree oper<strong>at</strong>ions. Instead of implementinga separ<strong>at</strong>e node class, ParPtrTree simply stores an array where each arrayelement corresponds to a node of the tree. Each position i of the array stores thevalue for node i <strong>and</strong> the array position for the parent of node i. Class ParPtrTree

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

Saved successfully!

Ooh no, something went wrong!