lecture14 - BST deletion

lecture14 - BST deletion - Monday, March 2 nd Binary Tree...

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

View Full Document Right Arrow Icon
Monday, 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
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);  //  Process nodes in  right sub-tree .     cout << cur->value;          //  Process the  current  node. }
Background image of page 2
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
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
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
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)            parent = cur;             cur = cur->right; Let’s delete  Casey cur parent NULL Casey  <  Mel? parent cur parent Casey  <  Darren? parent cur Casey  <  Carey? parent cur And we’ve found our node!
Background image of page 6
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 parent cur 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
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 51

lecture14 - BST deletion - Monday, March 2 nd Binary Tree...

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

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