Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)"— Presentation transcript:

1 CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
Steve Wolfman Winter Quarter 2000 How’s the homework going? Anyone having trouble? OK, everyone with their hand up is having trouble, and by the law of the excluded middle, the rest of you are not having trouble. So, those who are having trouble, let’s vote on which problem is hardest: <VOTE> Alright, I’ll solve that problem.

2 Today’s Outline Forming Teams
Many Things Steve Didn’t Finish on Friday (BSTs) Binary Search Trees Continued Now, whoever doesn’t have a team, stand up. Everyone who doesn’t have a team, make eyebrow motions at each other to indicate that you’ll get together after class and form a team. Good! Now, on to everything I forgot on Friday.

3 Successor Find the next larger node in this node’s subtree. 10 5 15 2
Node * succ(Node * root) { if (root->right == NULL) return NULL; else return min(root->right); } 10 5 15 2 9 20 Here’s a little digression. Maybe it’ll even have an application at some point. Find the next larger node in 10’s subtree. Can we define it in terms of min and max? It’s the min of the right subtree! 7 17 30

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

5 Deletion 10 5 15 2 9 20 And now for something completely different. Let’s say I want to delete a node. Why might it be harder than insertion? Might happen in the middle of the tree instead of at leaf. Then, I have to fix the BST. 7 17 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 Now, before we move on to all the pains of true deletion, let’s do it the easy way. We’ll just pretend we delete deleted nodes. This has some real advantages: 2 9 20 7 17 30

7 Lazy Deletion Delete(17) Delete(15) Delete(5) Find(9) Find(16)
Insert(5) Find(17) 10 5 15 2 9 20 OK, let’s do some lazy deletions. Everybody yawn, stretch, and say “Mmmm… doughnut” to get in the mood. Those of you who are already asleep have the advantage. 7 17 30

8 Deletion - Leaf Case Delete(17) 10 5 15 2 9 20 7 17 30
Alright, we did it the easy way, but what about real deletions? Leaves are easy; we just prune them. 7 17 30

9 Deletion - One Child Case
Delete(15) 10 5 15 2 9 20 Single child nodes we remove and… Do what? We can just pull up their children. Is the search tree property intact? Yes. 7 30

10 Deletion - Two Child Case
Delete(5) 10 5 20 2 9 30 Ah, now the hard case. How do we delete a two child node? We remove it and replace it with what? It has all these left and right children that need to be greater and less than the new value (respectively). Is there any value that is guaranteed to be between the two subtrees? Two of them: the successor and predecessor! So, let’s just replace the node’s value with it’s successor and then delete the succ. 7

11 Finally… 10 7 20 2 9 30 This slide is just for closure.

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; } else { // Two child case successor = succ(root); handle->data = successor->data; delete(successor->data, handle->right); } Here’s the code for deletion using lots of confusing reference pointers BUT no leaders, fake nodes. The iterative version of this can get somewhat messy, but it’s not really any big deal.

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 OK, let’s think about BSTs in the same way we thought about heaps. Indeed, some of the same ideas come up.

14 Solutions? Reduce disk accesses? Keep BSTs shallow?
How might we solve these problems? Reduce disk accesses: we need to have a bigger branching factor, just like with heaps. BUT what does the search tree property mean when the branching factor is above 2? To keep BSTs shallow, we can insist on one of the better arrangements.

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)!


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

Similar presentations


Ads by Google