28 - CMPSCI 187: Programming With Data Structures Lecture...

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

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #28: Binary Search Trees 21 November 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Binary Search Trees • The Idea of a Binary Search Tree • The BinarySearchTreeADT Interface • The LinkedBinarySearchTree Class • Adding an Element • Removing An Element • Array Implementations of Binary Search Trees
Background image of page 2
The Idea of a Binary Search Tree • We want to be able to determine quickly (ideally in O(log n) time) whether a particular element is in our collection. Binary search did this for an array. But at the same time, we would like to be able to insert and delete elements without time-consuming shifts. • In a binary search tree , every node has an element from an ordered base type. If a node n has element x, every element in the left subtree of n is less than x and every element in the right subtree is greater than x. • Finding an element (or determining that it is not there) means following a path from the root to a leaf in the worst case. The time to do this is proportional to the depth of the tree. If the tree is balanced, this will be O(log n). In CMPSCI 311 (and a bit next lecture) we’ll see ways to keep a tree balanced. • But there is no reason that inserts and deletes should leave a balanced tree.
Background image of page 3

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

View Full DocumentRight Arrow Icon
The BinarySearchTreeADT Interface • Once we know that our binary tree is going to store elements from an ordered type, and use the binary search tree rules, we can set up the interface. • We don’t restrict T here, but we will cast T elements into Comparable<T> . If T only implements Comparable<? super T> , this won’t work. • The balanced binary tree classes will implement this same interface. public interface BinarySearchTreeADT<T> extends BinaryTreeADT<T> { public void addElement (T element); public T removeElement (T targetElement); public void removeAllOccurrrences (T targetElement); public T removeMin( ); public T removeMax( ); public T findMin( ); public T findMax( );
Background image of page 4
The LinkedBinarySearchTree Class • We will inherit from the LinkedBinaryTree class, so that we have Felds count and
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/22/2011 for the course COMPSCI 187 taught by Professor Barrington during the Fall '11 term at UMass (Amherst).

Page1 / 13

28 - CMPSCI 187: Programming With Data Structures Lecture...

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

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