Lecture 13 - Binary Search Trees Continued

Lecture 13 - Binary Search Trees Continued -...

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

View Full Document Right Arrow Icon
Monday, March 5th  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 ? N O ! 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 parent pointer 2. 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
Once we’ve found our node, we have to delete it.   There are  3  cases. Case 1: Our node is a leaf. cur
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.

This note was uploaded on 04/02/2012 for the course COMPUTER S 32 taught by Professor Smallberg during the Winter '12 term at UCLA.

Page1 / 51

Lecture 13 - Binary Search Trees Continued -...

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