lecture14

# lecture14 - Monday March 2 Binary Tre Re w e vie Binary S...

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

Monday, March 2 nd Binary Tree Review Binary Search Tree Node Deletion Uses for Binary Search Trees Huffman Encoding Balanced Trees

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

View Full Document
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. }
Binary Search Tree Insertion Review Question #1 : Show where you would insert “Cathy” Cathy Question #2 : How would you go about inserting “Dan”.

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

View Full Document
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….
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!

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

View Full Document
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!
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.

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

View Full Document
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
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

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

View Full Document
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; Since this is case #2, is
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 51

lecture14 - Monday March 2 Binary Tre Re w e vie Binary S...

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

View Full Document
Ask a homework question - tutors are online