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

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

CMPSCI 187: Programming With Data Structures Lecture #28: Binary Search Trees 21 November 2011

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

View Full Document
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
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.

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

View Full Document
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( );
The LinkedBinarySearchTree Class • We will inherit from the LinkedBinaryTree class, so that we have Felds count and

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

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

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

View Full Document
Ask a homework question - tutors are online