Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees."— Presentation transcript:

1 CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees

2 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

3 Dictionary ADT dic·tion·ar·y 1. 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 values Used wherever search is (e.g., everywhere) Implementation using Sequence takes O ( n ) time With good batch of hash, could get O (1) time

4 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 9 2 41 10

5 2 Search Recusive process starting from root If root’s key larger, recurse with left child If root’s key smaller, go with right child Return entry on match Return null when not found (leaf) Example: TreeSearch(4, root ) Algorithm TreeSearch(Key k, Position p) if isExternal(p) return p if c.compare(k, p.element().getKey()) < 0 return TreeSearch(k, left(p)) else if c.compare(k, p.element().getKey()) == 0 return p else // k  key(v) return TreeSearch(k, right(p)) 6 9 1 8    4

6 2 Search TreeSearch(5, root ) Algorithm TreeSearch(Key k, Position p) if isExternal(p) return p if c.compare(k, p.element().getKey()) < 0 return TreeSearch(k, left(p)) else if c.compare(k, p.element().getKey()) == 0 return p else // k  key(v) return TreeSearch(k, right(p)) 6 9 1 8    4

7 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

8  Removing an Entry Starts with search for k Simplest: k not found Simple: node has only 1 external child Replace node with child Example: remove(4) 2 6 9 1 8   4 5 5

9 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 8 4 8

10 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

11 BinaryTree Interface Tree methods: int size() boolean isEmpty() Iterator iterator() Iterable positions() Position root() Position parent(p) Iterable > 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 left(p) Position right(p) Rarely call children() on binary trees Much easier to use left() & right() But must be defined, since included in interface

12 Dictionary Interface Methods searching for Entrys with a given key Entry find(K k) Iterator > findAll(K k) Methods to add or remove data Entry insert(K k, V v) Entry remove(e) Iterator methods Iterator > entries() Iterator values() Collection methods int size() boolean isEmpty()

13 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?

14 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

15 In the Next Lecture Discuss even better ways of searching Long awaited AVL Trees Learn what self-balancing means Understand Greg’s love of this search tree (note: when you do let me know, please)


Download ppt "CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees."

Similar presentations


Ads by Google