ece 242 data structures
play

ECE 242 Data Structures Lecture 22 More Binary Search Trees and - PDF document

ECE 242 Data Structures Lecture 22 More Binary Search Trees and Hash Tables October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Overview Problem: How do I represent data so that no data value is present more than once?


  1. ECE 242 Data Structures Lecture 22 More Binary Search Trees and Hash Tables October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Overview ° Problem: How do I represent data so that no data value is present more than once? ° Binary Search Tree • Insert • Search • Remove ° Methods to perform operations can be a little complicated • We need recursive methods ° Remove operation • Follows a standard order of actions October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  2. Remove Method ° Remove a node from BST ° Three cases • Case 1: node is a leaf • Case 2: node has one child • Case 3: node has two children 10 5 14 1 16 7 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Remove Method: Case 1 ° The node is a leaf ° Assume we want to remove node 7 • just modify node 7’s parent child pointer 10 5 14 1 16 7 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  3. Remove Method: Case 2 ° The node has one child ° Assume we want to remove node 14 • just lift up node 14’s subtree 10 5 14 1 16 7 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Remove Method: Case 3 ° The node has two children ° Assume we want to remove node 10 3 1 10 5 14 4 16 7 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  4. Remove Method: Case 3 (cont.) ° After removing node 10, there are two subtree for node 10 ° We can not simply lift left/right subtree up 3 1 10 ° But we know, all nodes in left subtree 5 14 is less than all nodes in right subtree. 4 7 16 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Remove Method: Case 3(cont.) ° How to merge two split subtree into one tree? ° Find the maximum node in leftsubtree ° Joint the right subtree into that node 3 3 3 1 10 1 10 1 5 5 7 4 5 14 14 7 4 4 7 16 14 16 15 20 16 15 20 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  5. Summary For Remove Method prev ° Step 1: 3 node • find the node which needs to be deleted • prev is the parent of node to be deleted 1 10 5 14 4 7 16 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Summary For Remove Method ° Step 2: • find the maximum node tmp of left subtree • merge the node ’s right subtree to tmp ’s right child prev 3 node prev 1 10 3 node 5 1 10 4 7 5 14 14 tmp 4 16 16 7 tmp 15 15 20 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  6. Summary For Remove Method ° Step 3: • lift node ’s left child up prev prev 3 node 3 node 1 5 1 10 4 7 14 5 16 4 7 14 15 20 16 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Summary For Remove Method ° Step 4: • if we remove the root , then set node as root prev • if we remove prev ’s left child, then set node as prev ’s left child 3 • if we remove prev ’s right child, then set node as node prev ’s right child 1 5 4 7 14 16 15 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  7. Code For Remove Method public void remove(int id) { BSTNode tmp, node, p=root, prev = null; // find the node p which needs to be removed while( p!=null && p.id!=id ) { prev prev = p; 3 if( p.id<id ) node p = p.right; 1 10 p else 5 14 p = p.left; 4 16 7 } 15 node = p; 20 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Code For Remove Method(cont.) if( p!=null && p.id==id ) { // case (1)/(2): node has no right child: // its left child ( if any ) is attached to its parent if( node.right==null ) node = node.left; // case (1)/(2): node has no left child: // its right child ( if any ) is attached to its parent else if( node.left==null ) node = node.right; October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  8. Code For Remove Method(cont.) else { // find the maximum node in the left subtree, // store the node to tmp tmp = node.left; while( tmp.right!=null ) tmp = tmp.right; // merge the right subtree to tmp's right child tmp.right = node.right; // lift node.left up node = node.left; } October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Code For Remove Method(cont.) if( p==root ) root = node; else if( prev.left == p ) prev.left = node; else prev.right = node; } else if( root!=null ) System.out.println( "ID " + id + " is not in the database"); else System.out.println( "The database is empty" ); October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  9. Find/Add Running Time ° How long do the find and add take? ° We do one or two comparisons and go either right or left in the tree -- so running time depends on depth of tree. ° Let’s analyze binary search trees: • what is the worst-case depth? • what the best-case depth? October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Binary Trees Given a set of n keys, the tree we construct depends on insertion order. 1, 2, 3, 4, 5, 6, 7 4, 2, 1, 3, 6, 5, 7 1 4 2 3 2 6 4 5 1 3 5 7 6 7 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  10. Bounds on Tree-Depth ° The depth of a binary search tree (or any tree), is the maximum number of links between the root and any leaf. ° What is the maximum depth of a tree containing n elements? ° What is the minimum depth of a tree containing n elements? October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Running Time of Find/Insert ° The number of recursive calls depends on the tree- depth. ° Each recursive call just makes a constant number of comparisons, so the running time of both is ° What about removing an element? October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  11. Hash Table ° Problem: Is there a more efficient way to add/store values for a set • Previously seen ordered list and binary tree ° Hash Table • Involves a mathematical function called a “hash function” • Collision in storing/accessing data • Complexity ° Can be somewhat difficult to implement October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Motivation For Hash Table ° We have to store some records and perform the following: • add new record • delete record • search a record by key ° Find a way to do these efficiently! What are some of the techniques we have seen so far? October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  12. Unsorted Array ° Use an array to store the records, in unsorted order • add - add the records as the last entry fast, O(1) • delete a target - slow to delete a record because we need to find the target, O(n) • search - sequential search slow, O(n) October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Sorted Array ° Use an array to store the records, keeping them in sorted order • add - insert the record in proper position. much record movement slow O(n) • delete a target - how to handle the hole after deletion? Much record movement slow O(n) • search - binary search fast O(log n) October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  13. Linked List ° Store the records in a linked list (sorted / unsorted) • add - fast if one can insert node anywhere O(1) • delete a target - fast at disposing the node, but slow at finding the target O(n) • search - sequential search slow O(n) (if we only use linked list, we cannot use binary search even if the list is sorted.) October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Tree ° Better performance but are more complex ° Tree • What sort of complexity can we expect from BST? • What types of data don’t support a BST? October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  14. Array As Table ID NAME SCORE 0012345 andy 81.5 0033333 betty 90 0056789 david 56.8 ... 9801010 peter 20 9802020 mary 100 ... 9903030 tom 73 9908080 bill 49 Consider this problem. We want to store 1000 student records and search them by student ID. October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables Array As Table (Cont.) ID NAME SCORE 0 One ‘ID’ way is to store the : : : records in a huge array 12345 andy 81.5 : : : (index 0…9999999). 33333 betty 90 : : : The index is used as the 56789 david 56.8 : : : student id, i.e. the record of : : : the student with ID 0012345 9908080 bill 49 : : : is stored at A[12345] 9999999 October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

  15. Array As Table --- Not Good ° Store the records in a huge array where the index corresponds to the key • add - very fast O(1) • delete - very fast O(1) • search - very fast O(1) ° But it wastes a lot of memory! Not feasible. We need to find a technique that efficiently uses available memory October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables New Function For Key int Hash(key) ---- return an integer value Imagine that we have such a magic H(‘0012345’) = 134 function Hash. It maps the key (ID) H(‘0033333’) = 67 of the 1000 records into the integers H(‘0056789’) = 764 0…999, one to one. No two … different keys maps to the same H(‘9908080’) = 3 number. October 30, 2009 ECE242 L25: More Binary Search Trees and Hash Tables

Recommend


More recommend