lecture14

lecture14 - 1 We sday, March 2 dne Binary Tre Re w e vie...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Wednesday, March 2 nd Binary Tree Review Binary Search Tree Node Deletion Uses for Binary Search Trees Huffman Encoding Balanced Trees
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Binary Tree Review Question #1 : What’s the post- order traversal for the following tree? Question #2 : Is the above tree a valid binary search tree? Question #3 : How about now? Max void PostOrder(Node *cur) { if (cur == NULL) return; PostOrder(cur->left); // Process nodes in left sub-tree . PostOrder(cur-> right); right sub-tree . cout << cur->value; Process the current node. }
Background image of page 2
3 Binary Search Tree Insertion Review Question #1 : Show where you would insert “Cathy” Cathy Question #2 : How would you go about inserting “Dan”.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Deleting a Node from a Binary Search Tree Now, let’s learn how to delete an item from a BST. It’s not as easy as you might think! Let’s say we want to delete Darren from our tree… X Now how do I re-link the nodes back together? Can I just move Arissa into Darren’s old slot? Arissa Hmm. . It seems OK, but is our tree still a valid binary search tree ? NO! By simply moving an arbitrary node into Darren’s slot, we violate our Binary Search Tree ordering requirement ! Carey is NOT less than Arissa! Next we’ll see how to do this properly….
Background image of page 4
5 Deleting a Node from a Binary Search Tree Here’s a high-level algorithm to delete a node from a Binary Search Tree: Given a value V to delete from the tree: 1. Find the value V in the tree, with a standard BST search. - Use two pointers: a cur pointer & a parent pointer 1. If the node was found, delete it from the tree, making sure to preserve its ordering! - There are three cases , so be careful!
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 BST Deletion: Step #1 Step 1 : Searching for value V 1. parent = NULL 2. cur = root 3. While cur != NULL A. If V == cur->value, then we’re done. B. If (V < cur->value) parent = cur; cur = cur->left; C. Else if (V > cur->value) cur = cur->right; Let’s delete Casey cur parent NULL Casey < Mel? < Darren? < Carey? And we’ve found our node!
Background image of page 6
7 BST Deletion: Step #2 Once we’ve found our node, we have to delete it. There are 3 cases. Case 1: Our node is a leaf. cur parent Case 2: Our node has one child Case 3: Our node has two children.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 BST Deletion: Step #2 Case 1 and 2 are trivial (if you already know linked lists): Case 1: Our node is a leaf. cur parent if (parent->left == cur) parent->left = NULL; else // if (parent->right == cur) parent->right = NULL; delete cur; ptr Hmm. Our target node “cur” isn’t the parent’s left child… X
Background image of page 8
9 BST Deletion: Step #2 Case 1 continued… A special case! Mel Case 1 (special): We’re deleting the root of a tree that has 1 child. ptr parent = NULL cur If the node to delete is: a. the root node , and b. it’s a leaf then just a. delete cur node, and set the root pointer to NULL X NULL
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 BST Deletion: Step #2 Let’s look at case #2 now… There are 3 sub-steps! // get a pointer to cur’s only child Node *grandchild; if (cur->left != NULL) grandchild = cur->left; else grandchild = cur->right; Case 2: Our node has one child parent cur X // Link parent to grandchild if (parent->left == cur) parent->left = grandchild; else if (parent->right == cur) parent->right = grandchild; // free the memory for cur delete cur;
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 51

lecture14 - 1 We sday, March 2 dne Binary Tre Re w e vie...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online