Download presentation
Presentation is loading. Please wait.
Published byGarey Roberts Modified over 9 years ago
1
1 Joe Meehean
2
Important and common problem Given a collection, determine whether value v is a member Common variation given a collection of unique keys, each associated with a value find value v associated with the key k find the mapping
3
Dictionary key => word value => definition Phonebook key => name value => phone number Webpage key => address value => html files and pictures
4
Problem: given an array of N values, determine if v in one of them Two approaches sequential search binary search
5
Look at each value in turn (iterate) e.g., a[0], a[1], … quit when v is found or end of array reached worst case time: O(N) What if a is sorted? look at each value in turn quit when v is found or end of array reached OR, when current value is > v worst case time still O(N)
6
Array must be sorted (a[0] <= a[1] <= a[2] … <= a[n]) Algorithm like the Clock Game on Price is RightClock Game
7
Array must be sorted (a[0] <= a[1] <= a[2] … <= a[n]) Algorithm look at middle value x in array if x == v else eliminate ½ the array if v < x, eliminate the right half if v > x, eliminate the left half repeat until v is found or no remaining values
8
Array or vector must be sorted Data type must provide < operator if !(a < b) && !(b < a) then b == a Or Comparator
9
Number of times N can be divided by 2 In Big O it is O(logN) difference between log 2 N and log N is a constant Scales better than O(N) O(logN) algorithms are faster If N = 1024, log(N) is 10 9
11
11 ant bar Throws away half the entries at every compare bat cat dog elk fox owl rat ant bat cat dog elk fox owl rat ant bat cat dog elk fox owl rat ant bat cat dog elk fox owl rat
12
12 What if we made a special data structure that represents a binary search?
13
Special kind of binary tree Each node stores a key sometimes an associated value For each node n all keys in n’s left subtree are < key at n all keys in n’s right subtree are > key at n if duplicate keys allowed keys that equal n can go left XOR right (not both) 13
14
Insert a key (and associated data) Lookup a key (and associated data) Remove a key (…) Print all keys in sorted order using an inorder traversal 14
15
6 6 4 4 5 5 9 9 2 2 6 6 4 4 7 7 9 9 2 2 Yes In order traversal produces: 2 4 5 6 9 No: 7 is not < 6 4 2 13 5 15
16
// private inner class of BST class BinaryNode{ public: K key_; BinaryNode * left_; BinaryNode * right_; //constructors } 16
17
template > class BST { private: BinaryNode* root_; Compare isLessThan_; public: BST() {root_ = NULL;} bool insert(const K& key); bool lookup(const K& key); void delete(const K& key); } 17
18
18
19
Key is in BST if it is in the root the left subtree or the right subtree Don’t need to look in both subtrees just like binary search in an array 19
20
// public driver method // method of BST bool lookup(K& key){ // private recursive helper method // on next slide return lookup(root, key); } 20
21
// private method of BST bool lookup(Bnode* n, K& k){ if( n == NULL ) return false; else if( isLessThan(k, n->key) ) return lookup(n->left, k); else if( isLessThan(n->key, k)) return lookup(n->right, k); else return true; } 21
22
Cases empty (null) subtree value found next look left next look right Shout it out lookup(4) lookup(5) lookup(3) 6 6 4 4 5 5 9 9 2 2 22
23
Always follows path from root down Worst-case goes to a leaf along longest path proportional to tree height Height related to size given size, how can we know height? 23
24
Best case tree is balanced all non-leaf nodes have 2 children all leaves at the same depth height is log 2 N Worst case tree is linear all non-leaf nodes have a single child height is N 24
25
25 6 6 4 4 5 5 9 9 3 3 lookup(2 ) 7 7 15 2 2
26
26 6 6 4 4 5 5 9 9 3 3 7 7 15 lookup(2 ) 2 2 Eliminates half the nodes at every compare
27
27 6 6 4 4 5 5 9 9 3 3 7 7 15 lookup(2 ) 2 2 Eliminates half the nodes at every compare
28
28 6 6 4 4 5 5 9 9 3 3 7 7 15 lookup(2 ) 2 2 Eliminates half the nodes at every compare
29
Worst-case O(height of tree) Worst of worst height is N lookup is O(N) Best worst-case height is log 2 N lookup is O(logN) O(LogN) is waaaay better than O(N) 29
30
30
31
New values inserted as leaves Must choose position to respect BST ordering and to ensure we can find it with a lookup Duplicate keys are not allowed 31
32
Traverse the tree like a lookup If we find a duplicate return an error If we end up at a null (child of a leaf) make a new node with the key make it the child of the leaf Note the above two were our base cases for lookup too 32
33
// members of BST void insert(const K& key){ insert(root, k); } void insert( BinaryNode*& n, const K& key){ if( n == NULL ){ n = new BinaryNode(key); }else if( isLessThan(k, n->key_) ){ insert(k, n->left_); }else if( isLessThan(n->key_, k) ){ insert(k, n->right_); }else{ //duplicate, do nothing } 33
34
First names BST You add your names CLASS ACTIVITY 34
35
Similar to lookup worst-case follow path from root to leaf O(logN) for a balanced tree O(N) for a completely unbalanced tree 35
36
Find the node n w/ key to be deleted Different actions depending on n’s # of kids Case 1: n has 0 kids (it’s a leaf) set parent’s n-pointer (left or right) to null Case 2: n has 1 kid set parent’s n-pointer to point to n’s only kid Case 3: n has 2 kids replace n’s key with a key further down in the tree delete that node 36
37
What node value can replace n’s value? new value of n must be: > all values in left subtree < all values in right subtree Largest value from the left subtree Smallest value from the right subtree let’s choose this one (arbitrarily) use findMin on root of right subtree 37
38
8 8 20 16 15 18 17 … … … … delete(17) 38
39
8 8 20 16 15 18 … … … … 39 delete(17) 17
40
8 8 20 16 15 18 17 … … … … delete(16) 40
41
16 8 8 20 15 18 17 … … … … delete(16) 41
42
8 8 20 16 15 18 17 … … … … Smallest value in right subtree delete(15) 42
43
8 8 20 16 18 17 … … … … 43 Case 2: 1 kid Replace 16 with it’s only child delete(15)
44
8 8 20 16 18 17 … … … … 44 Case 2: 1 kid Replace 16 with it’s only child delete(15)
45
45
46
Find the node n w/ key to be deleted Different actions depending on n’s # of kids case 1: n has 0 kids (it’s a leaf) set parent’s n-pointer (left or right) to null case 2: n has 1 kid set parent’s n-pointer to point to n’s only kid case 3: n has 2 kids replace n’s key with a key further down in the tree delete that node 46
47
Case 1 (n is leaf) and case 2 (n has 1 kid) both need to update the parents pointer How? Pass a reference to that pointer 47
48
// publicly visible method void BST ::delete(const K& key){ delete(root_, key); } // private helper method void BST ::delete( Node*& n, const K& k){ // base case 1 (key not in tree) if( n == null ){ return; }... } 48
49
// private helper method void BST ::delete( Node*& n, const K& k){... if( isLessThan(k, n->key) ){ delete(n->left, k); }else if( isLessThan(n->key, k) ){ delete(n->right, k); }... } 49
50
// private helper method void BST ::delete( Node*& n, const K& k){... // case 3 (has two children) else if( n->left != NULL && n->right != NULL ){ Node** tmp = findMin(&n->right); n->key = (*tmp)->key; // handles cases 1 & 2 for tmp removeNodeSimple(*tmp); }... } 50
51
// private helper method void BST ::delete( Node*& n, const K& k){... else{ // handles cases 1 & 2 removeNodeSimple(n); }... } 51
52
// private helper method // should only be called on nodes with // 0 or 1 children void removeNodeSimple(Node*& n){ // left as an exercise } 52
53
Find the node to be deleted follow path from root to desired node Delete node worst-case: 2 kids find smallest key in right subtree 53
54
Worst-case (delete root) Root to leaf H = Height of Tree O(H) Balanced tree O(logN) where N is keys in tree O(N) for a completely unbalanced tree 54
55
Some BSTs have two data values a key: used to lookup a value: some data associated with that key used to implement maps 55
56
In this case: two generic types binary node has four member variables insert has two parameters lookup returns value for given key delete might return value too class BST K key_; V value_; Bnode* left_; Bnode* right_; insert( const K& key, const V& value); V& lookup(const K& key); V delete(const K& key); 56
57
BSTs store comparable keys and associated values if desired Lookup, insert, delete are easy to implement (sort of) All are worst-case O(Height of tree) O(N) for unbalanced O(logN) for balanced O(logN) is waaaaay better than O(N) If only we could guarantee a tree was balanced… 57
58
58
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.