Caren
June 27, 2018
190

# binary_trees_session__1.pdf

June 27, 2018

## Transcript

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

once Consistent practice is key Utilize resources

4. ### 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
5. ### 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
6. ### 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
7. ### 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
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
11. ### Binary Search Trees If we wanted to add 4 to

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

the tree, where can it go? 7 2 10 1 6 12 4
13. ### 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
14. ### 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
15. ### 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
16. ### Prototyping / Pseudocoding Helps plan solution without getting into code

syntax intricacies Discover edge cases Easier to iterate / improve on

10 1 6 12
19. ### 7 2 10 1 6 12 What’s one of the

biggest hints we can use to our advantage in this problem?
20. ### 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
21. ### 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
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  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  one child  two children
24. ### 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
25. ### 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
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  two children
27. ### 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
28. ### 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
29. ### 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
30. ### 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)
31. ### 7 2 10 1 6 12 If we want to

delete (7), what are some possibilities of how the tree can look like afterwards?
32. ### 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
33. ### 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)
34. ### 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) ﬁnd min in right subtree
35. ### 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) ﬁnd min in right subtree  b) replace node with min
36. ### 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) ﬁnd min in right subtree  b) replace node with min  c) remove duplicated node
37. ### 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) ﬁnd min in right subtree  b) replace node with min  c) remove duplicated node  7 2 10 1 6 12
38. ### Delete Node from BST Cases :   no child  set

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

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

if current node != valueToDelete
41. ### Delete Node from BST Node deleteNode(Node root, int valueToDelete) {

if current node != valueToDelete   recursively call deleteNode on appropriate left/right subtree
42. ### 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
43. ### 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
44. ### 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
45. ### 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 = ﬁndMinInRightSubtree(root)  root.value = minValue  removeDuplicateNode(root)  return root
46. ### 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 = ﬁndMinInRightSubtree(root)  root.value = minValue  removeDuplicateNode(root)  return root

48. ### 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
49. ### 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.
50. ### 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
51. ### 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
52. ### Binary Search Trees Pros Quick searching, adding, deleting Traversals Inorder,

postorder, preorder, level order