{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# bst - Computer Science 211 Data Structures Mount Holyoke...

This preview shows pages 1–3. Sign up to view the full content.

Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree is a binary search tree (BST) iff it is empty or if the value of every node is both greater than or equal to every value in its left subtree and less than or equal to every value in its right subtree. Note that a BST is an ordered structure. That is, it maintains all of its elements in order. Like our other ordered structures, this restriction will allow us to build more efficient implementations of the most important operations on the tree. Binary Search Tree Implementation Because it stores items in order, our BST will implement the OrderedStructure interface. We also extend AbstractStructure , as we have done with most of our advanced data structures. See Structure Source: /home/jteresco/shared/cs211/src/structure5/BinarySearchTree.java First, we note that the implementation does not expose the tree nodes, as the BinaryTree im- plementation did. This is precisely because we need to be more restrictive to enforce the ordered nature of the structure. We do, however, make use of the existing BinaryTree implementation to take care of the details of storing the tree. The public methods of BinarySearchTree then make use of the underlying BinaryTree as appropriate. In addition to the underlying BinaryTree , whose root is stored as an instance variable, the implementation maintains a count of the number of nodes and remembers a Comparator (by default, the NaturalComparator ). Any interesting operation in our BinarySearchTree ( get , add , remove ) has to begin with a search for the correct place in the tree to do the operation. Our implementation has a helper method used by all of these to do just that. We want to find the BinaryTree whose root either contains the value, or, if the value is not to be found, the node whose non-existent child would contain the value. This is just a binary search, and is performed in the protected method locate . Next, let’s consdier the add method. We will be creating a new BinaryTree with the given value, and setting parent and child links in the tree to place this new value appropriately. We need to consider several cases: 1. We are adding to an empty binary tree, in which case we just make the new node the root of

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 211 Data Structures Fall 2009 the BST. 2. We add the new value as a new leaf at the appropriate place in the tree as the child of a current leaf. For example, add 5.5 to this tree: 1 8 10 7 9 5 4 2 3 6 to get 6 10 7 9 5 4 2 3 5.5 1 8 3. If the value is one that is already in the tree, the search may stop early, in which case we need to add the new item as a child of the predecessor of the node found.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern