Created
November 11, 2016 01:11
-
-
Save jjlumagbas/41b7e27b2defa3e76b6e2bd984ac040d to your computer and use it in GitHub Desktop.
Starter code for remove
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyBinarySearchTree { | |
| private MyTreeNode root; | |
| public MyBinarySearchTree() { | |
| this.root = null; | |
| } | |
| public void add(Integer data) throws Exception { | |
| this.root = add(root, data); | |
| } | |
| private MyTreeNode add(MyTreeNode node, Integer data) throws Exception { | |
| if (node == null) { | |
| return new MyTreeNode(data); | |
| } else if (node.getData().equals(data)) { | |
| throw new Exception("Item is already in the tree"); | |
| } | |
| if (data < node.getData()) { | |
| node.setLeft(add(node.getLeft(), data)); | |
| } else if (data > node.getData()) { | |
| node.setRight(add(node.getRight(), data)); | |
| } | |
| return node; | |
| } | |
| public void remove(Integer data) { | |
| root = remove(root, data); | |
| } | |
| public MyTreeNode remove(MyTreeNode node, Integer data) { | |
| } | |
| private MyTreeNode succ(MyTreeNode node) { | |
| if (node == null) { | |
| return null; | |
| } | |
| return min(node.getRight()); | |
| } | |
| private MyTreeNode min(MyTreeNode node) { | |
| if (node == null) { | |
| return null; | |
| } else if (node.getLeft() == null) { | |
| return node; | |
| } else { | |
| return min(node.getLeft()); | |
| } | |
| } | |
| public boolean contains(Integer data) { | |
| return contains(root, data); | |
| } | |
| private boolean contains(MyTreeNode root, Integer data) { | |
| if (root == null) { | |
| return false; | |
| } else if (root.getData() < data) { | |
| return contains(root.getLeft(), data); | |
| } else if(root.getData() > data) { | |
| return contains(root.getRight(), data); | |
| } else { | |
| return true; | |
| } | |
| } | |
| public String toString() { | |
| return toString(root); | |
| } | |
| private String toString(MyTreeNode root) { | |
| if (root == null) { | |
| return ""; | |
| } else { | |
| return toString(root.getLeft()) + root.getData() + " " + toString(root.getRight()); | |
| } | |
| } | |
| private class MyTreeNode { | |
| private Integer data; | |
| private MyTreeNode left; | |
| private MyTreeNode right; | |
| public MyTreeNode(Integer data) { | |
| this.data = data; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| public MyTreeNode(Integer data, MyTreeNode left, MyTreeNode right) { | |
| this.data = data; | |
| this.left = left; | |
| this.right = right; | |
| } | |
| public Integer getData() { | |
| return data; | |
| } | |
| public void setData(Integer data) { | |
| this.data = data; | |
| } | |
| public MyTreeNode getLeft() { | |
| return left; | |
| } | |
| public void setLeft(MyTreeNode left) { | |
| this.left = left; | |
| } | |
| public MyTreeNode getRight() { | |
| return right; | |
| } | |
| public void setRight(MyTreeNode right) { | |
| this.right = right; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import junit.framework.TestCase; | |
| import org.junit.Test; | |
| public class MyBinarySearchTreeTest extends TestCase { | |
| MyBinarySearchTree tree; | |
| @Override | |
| protected void setUp() throws Exception { | |
| tree = new MyBinarySearchTree(); | |
| } | |
| @Test | |
| public void testAdd() throws Exception { | |
| tree.add(15); | |
| assertTrue(tree.contains(15)); | |
| } | |
| @Test | |
| public void testAddMany() throws Exception { | |
| tree.add(5); | |
| tree.add(10); | |
| tree.add(30); | |
| tree.add(20); | |
| tree.add(40); | |
| assertEquals(tree.toString(), "5 10 20 30 40 "); | |
| } | |
| @Test | |
| public void testRemove() throws Exception { | |
| tree.add(15); | |
| tree.add(20); | |
| tree.remove(15); | |
| assertFalse(tree.contains(15)); | |
| assertTrue(tree.contains(20)); | |
| } | |
| @Test | |
| public void testContains() throws Exception { | |
| tree.add(5); | |
| tree.add(10); | |
| tree.add(30); | |
| tree.add(20); | |
| tree.add(40); | |
| assertTrue(tree.contains(5)); | |
| assertFalse(tree.contains(16)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment