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

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

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #29: More on Binary Search Trees 23 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
More on Binary Search Trees • Implementing an Ordered List • Comparing Linked and Tree Implementations • How to Keep a Tree Balanced • Ordinary Right and Left Rotations • Compound Rotations: RightLeft and LeftRight • AVL Trees (Basic Idea Only) • Red/Black Trees (Basic Idea Only)
Background image of page 2
Implementing an Ordered List • This is actually pretty anticlimactic. For each of the adding, removing, and ±nding list operations we have a corresponding operation already in LinkedBinarySearchTree<T> . The operations contains , isEmpty , and size are just inherited directly from LinkedBinarySearchTree<T> or its ancestor classes with the same names, so they don’t appear in the code. public class BSTList<T> extends LinkedBinarySearchTree<T> implements ListADT<T>, OrderedListADT<T>, Iterable<T> { public BSTList ( ) {super( );} public void add (T element) {addElement (element);} public T removeFirst ( ) {return removeMin( );} public T removeLast ( ) {return removeMax( );} public T remove (T element) {return removeElement (element);} public T first ( ) {return findMin( );} public T last ( ) return findMax( );} public Iterator<T> iterator( ) {return iteratorInOrder( );}}
Background image of page 3

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

View Full DocumentRight Arrow Icon
Comparing Linked and Tree Implementations • The linked list implementation of an ordered list did some things in O(1) time: removeFirst , first , isEmpty , and size . (With a doubly linked list we could also perform removeLast and last in O(1) time.) The other operations were all O(n), because in the worst case they involved a linear search of the whole list. • If
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

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

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

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