Share
Embed
Transcript
52 slides
-
“Nothing worth having comes easy” Put in the hard work
once Consistent practice is key Utilize resources -
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 -
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 -
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 -
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 -
Binary Search Trees If we wanted to add 4 to
the tree, where can it go? 7 2 10 1 6 12 -
Binary Search Trees If we wanted to add 4 to
the tree, where can it go? 7 2 10 1 6 12 -
Binary Search Trees If we wanted to add 4 to
the tree, where can it go? 7 2 10 1 6 12 -
Binary Search Trees If we wanted to add 4 to
the tree, where can it go? 7 2 10 1 6 12 -
Binary Search Trees If we wanted to add 4 to
the tree, where can it go? 7 2 10 1 6 12 4 -
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 -
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 -
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 -
Prototyping / Pseudocoding Helps plan solution without getting into code
syntax intricacies Discover edge cases Easier to iterate / improve on -
7 2 10 1 6 12 What’s one of the
biggest hints we can use to our advantage in this problem? -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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) -
7 2 10 1 6 12 If we want to
delete (7), what are some possibilities of how the tree can look like afterwards? -
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 -
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) -
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 -
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 -
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 -
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 -
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? -
Delete Node from BST Node deleteNode(Node root, int valueToDelete) {
if current node != valueToDelete -
Delete Node from BST Node deleteNode(Node root, int valueToDelete) {
if current node != valueToDelete recursively call deleteNode on appropriate left/right subtree -
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 -
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 -
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 -
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 -
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 -
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 -
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. -
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 -
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 -
Binary Search Trees Pros Quick searching, adding, deleting Traversals Inorder,
postorder, preorder, level order