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

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

View Full Document Right Arrow Icon
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/ 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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. For example, adding 9 to the tree above:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online