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

Slides:



Advertisements
Similar presentations
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Advertisements

CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000.
CSE332: Data Abstractions Lecture 7: AVL Trees
Instructor: Lilian de Greef Quarter: Summer 2017
TCSS 342, Winter 2006 Lecture Notes
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Btrees Deletion.
Binary search tree. Removing a node
CPSC 221: Data Structures Lecture #5 Branching Out
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 332 Data Abstractions B-Trees
Binary Search Trees.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Multiway search trees and the (2,4)-tree
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
October 30th – Priority QUeues
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Trees Why this is a useful data structure. Terminology
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Wednesday, April 18, 2018 Announcements… For Today…
Lecture 26 Multiway Search Trees Chapter 11 of textbook
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
B-Tree Insertions, Intro to Heaps
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 332: Data Abstractions Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 326: Data Structures Lecture #5 Political Heaps
A Robust Data Structure
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE 332: Data Abstractions AVL Trees
CPSC 221: Data Structures Lecture #5 Branching Out
Lecture 9: Self Balancing Trees
CSE 373: Data Structures and Algorithms
CSE 326: Data Structures Lecture #8 Balanced Dendrology
CSE 326: Data Structures Lecture #7 Binary Search Trees
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #9 AVL II
CSE 373: Data Structures and Algorithms
Richard Anderson Spring 2016
Heaps By JJ Shepherd.
CSE 326: Data Structures Lecture #5 Heaps More
Sorted Binary Trees.
B-Trees.
CSE 326: Data Structures Lecture #10 B-Trees
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Trees in Data Structures
CSE 326: Data Structures Lecture #14
326 Lecture 9 Henry Kautz Winter Quarter 2002
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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.

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.

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

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

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?

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

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

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

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

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

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

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.

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.

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.

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

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