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.

166 Chap. 5 Binary Trees37244273240 42235120Figure 5.15 An example of BST insertion. A record with value 35 is insertedinto the BST of Figure 5.13(a). The node with value 32 becomes the parent of thenew node containing 35.Inserting a record with key value k requires th<strong>at</strong> we first find where th<strong>at</strong> recordwould have been if it were in the tree. This takes us to either a leaf node, or to aninternal node with no child in the appropri<strong>at</strong>e direction. 3 Call this node R ′ . We thenadd a new node containing the new record as a child of R ′ . Figure 5.15 illustr<strong>at</strong>esthis oper<strong>at</strong>ion. The value 35 is added as the right child of the node with value 32.Here is the implement<strong>at</strong>ion for inserthelp:/** @return The current subtree, modified to containthe new item */priv<strong>at</strong>e BSTNode inserthelp(BSTNode rt,Key k, E e) {if (rt == null) return new BSTNode(k, e);if (rt.key().compareTo(k) > 0)rt.setLeft(inserthelp(rt.left(), k, e));elsert.setRight(inserthelp(rt.right(), k, e));return rt;}You should pay careful <strong>at</strong>tention to the implement<strong>at</strong>ion for inserthelp.Note th<strong>at</strong> inserthelp returns a pointer to a BSTNode. Wh<strong>at</strong> is being returnedis a subtree identical to the old subtree, except th<strong>at</strong> it has been modified to containthe new record being inserted. Each node along a p<strong>at</strong>h from the root to the parentof the new node added to the tree will have its appropri<strong>at</strong>e child pointer assignedto it. Except for the last node in the p<strong>at</strong>h, none of these nodes will actually changetheir child’s pointer value. In th<strong>at</strong> sense, many of the assignments seem redundant.However, the cost of these additional assignments is worth paying to keep the insertionprocess simple. The altern<strong>at</strong>ive is to check if a given assignment is necessary,which is probably more expensive than the assignment!3 This assumes th<strong>at</strong> no node has a key value equal to the one being inserted. If we find a node th<strong>at</strong>duplic<strong>at</strong>es the key value to be inserted, we have two options. If the applic<strong>at</strong>ion does not allow nodeswith equal keys, then this insertion should be tre<strong>at</strong>ed as an error (or ignored). If duplic<strong>at</strong>e keys areallowed, our convention will be to insert the duplic<strong>at</strong>e in the right subtree.

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

Saved successfully!

Ooh no, something went wrong!