Problem

Revise Node and LinkedBag as described in Segment 3.29.Searching a binary search tree. The...

Revise Node and LinkedBag as described in Segment 3.29.

Searching a binary search tree. The organization of the nodes in a binary search tree enables us to search the tree for a particular data object, given its search key. For example, suppose that we search the tree in Figure 23-18 for the string Jim. Beginning at the root of the tree, we compare Jim with Jared. Since the string Jim is greater than the string Jared, we search the right subtree of the root. Comparing Jim to Megan, we find that Jim is less than Megan. We search Megan’s left subtree next and find Jim.

To search for Laura, we would compare Laura with Jared, then with Megan, and then with Jim. Since Laura is greater than Jim, we would search Jim’s right subtree. But this subtree is empty, so we conclude that Laura does not occur in the tree.

We can express our search algorithm recursively: To search a binary search tree, we search one of its two subtrees. The search ends when either we find the item we seek or we encounter an empty subtree. We can formalize this search by writing the following pseudocode:

Algorithm bstSearch(binarySearchTree, desiredObject)// Searches a binary search tree for a given object.// Returns true if the object is found.if (binarySearchTree is empty)return falseelse if (desiredObject == object in the root of binarySearchTree)return trueelse if (desiredObject

This algorithm is somewhat like a binary search of an array. Here we search one of two subtrees; a binary search searches one half of an array. You will see how to implement this algorithm in Chapter 25.

If you think that you could implement the ADT dictionary by using a binary search tree, you would be right. Chapter 25 will show you how.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 3
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT