when to use certain traversals (preorder, postorder, inorder, level order) Modifying trees adding and deleting nodes modifying nodes Binary Search Trees
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
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
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)
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
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
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
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
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?
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 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
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
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
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.
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