Upgrade to Pro — share decks privately, control downloads, hide ads and more …

binary_trees_session__1.pdf

Caren
June 27, 2018
210

 binary_trees_session__1.pdf

Caren

June 27, 2018
Tweet

Transcript

  1. “Nothing worth having comes easy” Put in the hard work

    once Consistent practice is key Utilize resources
  2. What should we know about trees Traversing trees
 how and

    when to use certain traversals
 (preorder, postorder, inorder, level order) Modifying trees
 adding and deleting nodes
 modifying nodes Binary Search Trees
  3. Binary Search Trees Special type of binary trees every element

    on a node’s left subtree is smaller every element on a node’s right subtree is bigger
  4. Binary Search Trees Pros: Fast lookup if balanced logN if

    balanced Easy addition/deletion of nodes logN for both operations Cons: More complex to maintain and create
  5. Binary Search Trees Pros: Fast lookup if balanced logN if

    balanced Easy addition/deletion of nodes logN for both operations Cons: More complex to maintain and create
  6. Binary Search Trees If we wanted to add 4 to

    the tree, where can it go? 7 2 10 1 6 12
  7. Binary Search Trees If we wanted to add 4 to

    the tree, where can it go? 7 2 10 1 6 12
  8. Binary Search Trees If we wanted to add 4 to

    the tree, where can it go? 7 2 10 1 6 12
  9. Binary Search Trees If we wanted to add 4 to

    the tree, where can it go? 7 2 10 1 6 12
  10. Binary Search Trees If we wanted to add 4 to

    the tree, where can it go? 7 2 10 1 6 12 4
  11. Binary Search Trees If we wanted to add 4 to

    the tree, where can it go? Start from root:
 
 If node we want to add
 is less than node we’re looking
 at, go left. Otherwise, go right
 
 when there are no more
 subtrees to traverse, add
 the node 7 2 10 1 6 12 4
  12. Binary Search Trees If we wanted to delete 7 from

    the tree,
 how can our tree end up looking? 7 2 10 1 6 12
  13. Binary Search Trees If we wanted to delete 7 from

    the tree,
 how can our tree end up looking? Let’s prototype and pseudocode
 it! 7 2 10 1 6 12
  14. Prototyping / Pseudocoding Helps plan solution without getting into code

    syntax intricacies Discover edge cases Easier to iterate / improve on
  15. 7 2 10 1 6 12 What’s one of the

    biggest hints we can use to our advantage in this problem?
  16. 7 2 10 1 6 12 What’s one of the

    biggest hints we can use to our advantage in this problem? It’s a Binary Search Tree
  17. 7 2 10 1 6 12 What’s one of the

    biggest hints we can use to our advantage in this problem? It’s a Binary Search Tree For any node that we want to delete, the node can either have 0, 1, or 2 children
  18. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 one child
 two children
  19. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 one child
 two children
  20. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child (ie, 12)
 one child
 two children
  21. 7 2 10 1 6 For any node that we

    want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child (ie, 12)
 set node to null
 one child
 two children null
  22. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 two children
  23. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child (ie 10)
 two children
  24. 7 2 12 1 6 For any node that we

    want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child (ie 10)
 replace with child
 two children
  25. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children
  26. 7 2 10 1 6 12 For any node that

    we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children (ie 7)
  27. 7 2 10 1 6 12 If we want to

    delete (7), what are some possibilities of how the tree can look like afterwards?
  28. 7 2 10 1 6 12 If we want to

    delete (7), what are some possibilities of how the tree can look like afterwards? 6 2 10 1 12 10 2 12 1 6 Original
  29. 7 2 10 1 6 12 delete (7) For any

    node that we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children (ie 7)
 

  30. 7 2 10 1 6 12 delete (7) For any

    node that we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children (ie 7)
 a) find min in right subtree
 
 

  31. 10 2 10 1 6 12 delete (7) For any

    node that we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children (ie 7)
 a) find min in right subtree
 b) replace node with min
 

  32. 10 2 12 1 6 delete (7) For any node

    that we want to delete, the node can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children (ie 7)
 a) find min in right subtree
 b) replace node with min
 c) remove duplicated node

  33. For any node that we want to delete, the node

    can either have 0, 1, or 2 children Cases : 
 no child
 set node to null
 one child
 replace with child
 two children
 a) find min in right subtree
 b) replace node with min
 c) remove duplicated node
 7 2 10 1 6 12
  34. Delete Node from BST Cases : 
 no child
 set

    node to null
 one child
 replace with child
 two children
 a) find min in right subtree
 b) replace node with min
 c) remove duplicated node What are some useful helper methods we can create?
 

  35. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 
 
 
 

  36. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 recursively call deleteNode on appropriate left/right subtree
 
 
 
 

  37. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 recursively call deleteNode on appropriate left/right subtree
 else // we are at the node we want to delete
 
 
 
 

  38. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 recursively call deleteNode on appropriate left/right subtree
 else // we are at the node we want to delete
 if node has no children
 set node to null
 
 
 
 

  39. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 recursively call deleteNode on appropriate left/right subtree
 else // we are at the node we want to delete
 if node has no children
 set node to null
 if node has only one child
 replace node with child
 
 
 
 

  40. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 recursively call deleteNode on appropriate left/right subtree
 else // we are at the node we want to delete
 if node has no children
 set node to null
 if node has only one child
 replace node with child
 else
 minValue = findMinInRightSubtree(root)
 root.value = minValue
 removeDuplicateNode(root)
 return root
 
 
 

  41. Delete Node from BST Node deleteNode(Node root, int valueToDelete) {


    if current node != valueToDelete 
 recursively call deleteNode on appropriate left/right subtree
 else // we are at the node we want to delete
 if node has no children
 set node to null
 if node has only one child
 replace node with child
 else
 minValue = findMinInRightSubtree(root)
 root.value = minValue
 removeDuplicateNode(root)
 return root
 
 
 

  42. Pseudocode Practice Use collabedit or codepad. Since it’s pseudocode it

    doesn’t need to run Try to spend 15-20 minutes on each problem
  43. Pseudocode Practice 1. Understand the problem. Besides the given example(s)

    try to come up with some of your own examples for the problem. 2. Match. Does this problem match any other patterns you've seen before? 3. Prototype and pseudocode. Using one of the examples you came up with in Step 1 walk through a high level implementation.
  44. Pseudocode Practice Goals : 
 
 For each problem, you

    and your partner should aim to write a pseudocode solution Trace through your pseudocode solution with a couple of your own examples / test cases to convince yourself that the solution will work Finish by talking through the space and time complexity of the solution
  45. Interactive Session Reminders ‘send help’ feature try to think out

    loud as much as possible, just like in a real interview don’t jump into (pseudo)coding too quickly