csc 213 – large scale programming lecture 17: binary search trees
TRANSCRIPT
Today’s Goal
Review the Dictionary ADT Abstracts ideas needed for computer-based
searching (Briefly) examine ways we had implemented
Discuss BinarySearchTree (BST) Relationship to BinaryTree and Heap What BST looks like and how it works Effect on big-Oh notation BST Limitations
Dictionary ADT
dic·tion·ar·y1.Reference book containing collection of words,
with multiple definitions given for each word
2.Book providing many translations into other languages
Dictionary ADT maps key to 1 or more valuesUsed wherever search is (e.g., everywhere)Implementation using Sequence takes O(n) timeWith good batch of hash, could get O(1) time
Binary Search Trees
Store Entrys like Heap
May not be complete Use different ordering
Lower keys in left subtree
Higher keys in right subtree
Equal keys not specified
6
92
41 10
2
SearchRecusive process starting from rootIf root’s key larger,
recurse with left childIf root’s key smaller,
go with right childReturn entry on matchReturn null when not
found (leaf)
Example:TreeSearch(4, root)
Algorithm TreeSearch(Key k, Position p)if isExternal(p)
return pif c.compare(k, p.element().getKey()) < 0
return TreeSearch(k, left(p))else if c.compare(k, p.element().getKey()) == 0
return pelse // k key(v)
return TreeSearch(k, right(p))
6
9
1 8
4
2
SearchTreeSearch(5, root) Algorithm TreeSearch(Key k, Position p)
if isExternal(p)return p
if c.compare(k, p.element().getKey()) < 0return TreeSearch(k, left(p))
else if c.compare(k, p.element().getKey()) == 0return p
else // k key(v)return TreeSearch(k, right(p))
6
9
1 8
4
5
Inserting Into Tree
Search for k If k not in tree, expand
leaf where search ended If k already in tree,
Map: Replace node’s entry Dictionary: Keep
searching for next match; only add when external node found
Insert(5)
2
6
9
1 8
4
Removing an Entry
Starts with search for k Simplest: k not found Simple: node has only
1 external childReplace node with child
Example: remove(4)
2
6
9
1 8
4
5
5
5
Removing an Entry
Hard: Node has 2 kids Find next node in tree
Go to right child and then keep going left Copy Entry into node Replace descendant
with its right child Example: remove(6)
2
6
9
1 84
8
BST Performance
Space is linear: O(n) Dictionary methods use
O(h) time h is height of tree
Complete tree is O(log n)
Linked list is O(n) Sadly, this is common
BinaryTree Interface
Tree methods: int size() boolean isEmpty() Iterator<E> iterator() Iterable<E> positions()
Position<E> root() Position<E> parent(p) Iterable<Position<E>> children(p)
boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) E replace (p, e)
Child existence methods: boolean hasLeft(p) boolean hasRight(p)
Child accessor methods: Position<E> left(p) Position<E> right(p)
Rarely call children() on binary trees Much easier to use left()
& right() But must be defined, since
included in interface
Dictionary Interface
Methods searching for Entrys with a given key Entry<K,V> find(K k) Iterator<Entry<K,V>> findAll(K k)
Methods to add or remove data Entry<K,V> insert(K k, V v) Entry<K,V> remove(e)
Iterator methods Iterator<Entry<K,V>> entries() Iterator<V> values()
Collection methods int size() boolean isEmpty()
BST-based Dictionary
BST is a binary tree Should implement BinaryTree interface Could extend existing class or start from scratch Needs methods to insert, remove, & find data
BinaryTree is NOT a Dictionary No similarity between methods Most methods inappropriate for dictionary
So how do we design this?
Using BST for a Dictionary
For best design, implement 2 classes One class for BinaryTree & one for Dictionary Since no methods overlap, very little extra writing Minimum number of methods made public keeping
design simple and clean BST class has add, remove, & find methods
Should work with Entrys Can be used with any Entry-based interface
Dictionary class has field holding BST class Would be easy to modify for ANY search tree