diff --git a/08-labBST/Assignment.java b/08-labBST/Assignment.java index bb44eee..cb3551e 100644 --- a/08-labBST/Assignment.java +++ b/08-labBST/Assignment.java @@ -24,11 +24,96 @@ public class Assignment { } public void insert(int toInsert) { - // Insert the provided element into the tree + // Insert the provided element into the tree + Node newNode = new Node(); + newNode.val = toInsert; + if (root == null) { + root = newNode; + return; + } + Node parent = root; + while (newNode.parent == null) { + if (toInsert < parent.val) { + if (parent.left == null) { + parent.left = newNode; + newNode.parent = parent; + } else { + parent = parent.left; + } + } else { + if (parent.right == null) { + parent.right = newNode; + newNode.parent = parent; + } else { + parent = parent.right; + } + } + } } public void delete(int toDelete) { - // Delete the provided element from the tree + // Delete the provided element from the tree + Node toDeleteNode = root; + + while (toDeleteNode != null && toDeleteNode.val != toDelete) { + if (toDelete < toDeleteNode.val) { + toDeleteNode = toDeleteNode.left; + } else { + toDeleteNode = toDeleteNode.right; + } + } + // case 0: Node not found or tree is empty + if (toDeleteNode == null) { + return; + } + + // case 1: Node has no children + if (toDeleteNode.left == null && toDeleteNode.right == null) { + if (toDeleteNode.parent == null) { + root = null; + } else if (toDeleteNode.parent.left == toDeleteNode) { + toDeleteNode.parent.left = null; + } else { + toDeleteNode.parent.right = null; + } + return; + } + + // case 2: Node has one child + if (toDeleteNode.left == null || toDeleteNode.right == null) { + Node child = toDeleteNode.left == null ? toDeleteNode.right : toDeleteNode.left; + if (toDeleteNode.parent == null) { + root = child; + } else if (toDeleteNode.parent.left == toDeleteNode) { + toDeleteNode.parent.left = child; + } else { + toDeleteNode.parent.right = child; + } + return; + } + + // case 3: Node has two children + Node successor = toDeleteNode.right; + while (successor.left != null) { + successor = successor.left; + } + // remove successor from its current position + if (successor.parent.left == successor) { + successor.parent.left = successor.right; + } else { + successor.parent.right = successor.right; + } + // replace toDeleteNode with successor + successor.parent = toDeleteNode.parent; + successor.left = toDeleteNode.left; + successor.right = toDeleteNode.right; + if (toDeleteNode.parent == null) { + root = successor; + } else if (toDeleteNode.parent.left == toDeleteNode) { + toDeleteNode.parent.left = successor; + } else { + toDeleteNode.parent.right = successor; + } } @@ -47,17 +132,32 @@ public class Assignment { } private void inOrder(Node curr) { - // Print the elements in the tree one one each line + // Print the elements in the tree one on each line + if (curr == null) { + return; + } + inOrder(curr.left); System.out.println(curr.val); + inOrder(curr.right); } private void preOrder(Node curr) { - // Print the elements in the tree one one each line + // Print the elements in the tree one on each line + if (curr == null) { + return; + } System.out.println(curr.val); + preOrder(curr.left); + preOrder(curr.right); } private void postOrder(Node curr) { - // Print the elements in the tree one one each line + // Print the elements in the tree one on each line + if (curr == null) { + return; + } + postOrder(curr.left); + postOrder(curr.right); System.out.println(curr.val); } }