1 / 16

CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)

CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy). Steve Wolfman Winter Quarter 2000. Today’s Outline. Forming Teams Many Things Steve Didn’t Finish on Friday (BSTs) Binary Search Trees Continued. Successor. Find the next larger node in this node’s subtree.

ivana
Download Presentation

CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 326: Data StructuresLecture #8Pruning (and Pruning for the Lazy) Steve Wolfman Winter Quarter 2000

  2. Today’s Outline • Forming Teams • Many Things Steve Didn’t Finish on Friday (BSTs) • Binary Search Trees Continued

  3. Successor Find the next larger node in this node’s subtree. Node * succ(Node * root) { if (root->right == NULL) return NULL; else return min(root->right); } 10 5 15 2 9 20 17 7 30

  4. Predecessor Find the next smaller node in this node’s subtree. Node * pred(Node * root) { if (root->left == NULL) return NULL; else return max(root->left); } 10 5 15 2 9 20 17 7 30

  5. Deletion 10 5 15 2 9 20 17 7 30 Why might deletion be harder than insertion?

  6. Lazy Deletion • Instead of physically deleting nodes, just mark them as deleted • simpler • physical deletions done in batches • some adds just flip deleted flag • extra memory for deleted flag • many lazy deletions slow finds • some operations may have to be modified (e.g., min and max) 10 5 15 2 9 20 17 7 30

  7. Lazy Deletion Delete(17) Delete(15) Delete(5) Find(9) Find(16) Insert(5) Find(17) 10 5 15 2 9 20 17 7 30

  8. Deletion - Leaf Case Delete(17) 10 5 15 2 9 20 17 7 30

  9. Deletion - One Child Case Delete(15) 10 5 15 2 9 20 7 30

  10. Deletion - Two Child Case Delete(5) 10 5 20 2 9 30 7

  11. Finally… 10 7 20 2 9 30

  12. Delete Code void delete(Comparable key, Node *& root) { Node *& handle(find(key, root)); Node * toDelete = handle; if (handle != NULL) { if (handle->left == NULL) { // Leaf or one child handle = handle->right; delete toDelete; } else if (handle->right == NULL) { // One child handle = handle->left; delete toDelete; } else { // Two child case successor = succ(root); handle->data = successor->data; delete(successor->data, handle->right); } } }

  13. Thinking about Binary Search Trees • Observations • Each operation views two new elements at a time • Elements (even siblings) may be scattered in memory • Binary search trees are fast if they’re shallow • Realities • For large data sets, disk accesses dominate runtime • Some deep and some shallow BSTs exist for any data

  14. Solutions? • Reduce disk accesses? • Keep BSTs shallow?

  15. To Do • Finish Homework #3 • Continue Project II • Read chapter 4 in the book • Read chapters 1-3 and 6 if you haven’t

  16. Coming Up • B-Trees • Self-balancing trees • Third homework assignment due (January 26th) • Second project due (January 31st) • Midterm (February 4th)!

More Related