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.

352 Chap. 10 Indexingpriv<strong>at</strong>e E findhelp(TTNode root, Key k) {if (root == null) return null;// val not foundif (k.compareTo(root.lkey()) == 0) return root.lval();if ((root.rkey() != null) && (k.compareTo(root.rkey())== 0))return root.rval();if (k.compareTo(root.lkey()) < 0) // Search leftreturn findhelp(root.lchild(), k);else if (root.rkey() == null)// Search centerreturn findhelp(root.cchild(), k);else if (k.compareTo(root.rkey()) < 0) // Search centerreturn findhelp(root.cchild(), k);else return findhelp(root.rchild(), k); // Search right}Figure 10.11 Implement<strong>at</strong>ion for the 2-3 tree search method.18331223 304810 15 15 20 211424 31 45 47 50 52Figure 10.12 Simple insert into the 2-3 tree of Figure 10.9. The value 14 isinserted into the tree <strong>at</strong> the leaf node containing 15. Because there is room in thenode for a second key, it is simply added to the left position with 15 moved to theright position.again to search the root node. Because 15 is less than 18, the first (left) branch istaken. At the next level, we take the second branch to the leaf node containing 15.If the search key were 16, then upon encountering the leaf containing 15 we wouldfind th<strong>at</strong> the search key is not in the tree. Figure 10.11 is an implement<strong>at</strong>ion for the2-3 tree search method.Insertion into a 2-3 tree is similar to insertion into a BST to the extent th<strong>at</strong> thenew record is placed in the appropri<strong>at</strong>e leaf node. Unlike BST insertion, a newchild is not cre<strong>at</strong>ed to hold the record being inserted, th<strong>at</strong> is, the 2-3 tree does notgrow downward. The first step is to find the leaf node th<strong>at</strong> would contain the recordif it were in the tree. If this leaf node contains only one value, then the new recordcan be added to th<strong>at</strong> node with no further modific<strong>at</strong>ion to the tree, as illustr<strong>at</strong>ed inFigure 10.12. In this example, a record with key value 14 is inserted. Searchingfrom the root, we come to the leaf node th<strong>at</strong> stores 15. We add 14 as the left value(pushing the record with key 15 to the rightmost position).If we insert the new record into a leaf node L th<strong>at</strong> already contains two records,then more space must be cre<strong>at</strong>ed. Consider the two records of node L <strong>and</strong> the

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

Saved successfully!

Ooh no, something went wrong!