From 76d17d75a8fe3227bcba5776dff4eccc56cca28c Mon Sep 17 00:00:00 2001 From: Imre Balassa Date: Sat, 7 Jul 2018 01:38:35 +0200 Subject: [PATCH] Delete the root element from the tree. --- .../src/main/java/com/baeldung/tree/BinaryTree.java | 2 +- ...{BinaryTreeTest.java => BinaryTreeUnitTest.java} | 13 ++++++++++++- .../trie/{TrieTest.java => TrieUnitTest.java} | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) rename data-structures/src/test/java/com/baeldung/tree/{BinaryTreeTest.java => BinaryTreeUnitTest.java} (90%) rename data-structures/src/test/java/com/baeldung/trie/{TrieTest.java => TrieUnitTest.java} (98%) diff --git a/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java b/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java index e3179dca32..f435e41afa 100644 --- a/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java +++ b/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java @@ -57,7 +57,7 @@ public class BinaryTree { } public void delete(int value) { - deleteRecursive(root, value); + root = deleteRecursive(root, value); } private Node deleteRecursive(Node current, int value) { diff --git a/data-structures/src/test/java/com/baeldung/tree/BinaryTreeTest.java b/data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java similarity index 90% rename from data-structures/src/test/java/com/baeldung/tree/BinaryTreeTest.java rename to data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java index 99e656fe28..f81247b74d 100644 --- a/data-structures/src/test/java/com/baeldung/tree/BinaryTreeTest.java +++ b/data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java @@ -6,7 +6,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; -public class BinaryTreeTest { +public class BinaryTreeUnitTest { @Test public void givenABinaryTree_WhenAddingElements_ThenTreeNotEmpty() { @@ -70,6 +70,17 @@ public class BinaryTreeTest { assertEquals(initialSize, bt.getSize()); } + @Test + public void it_deletes_the_root() { + int value = 12; + BinaryTree bt = new BinaryTree(); + bt.add(value); + + assertTrue(bt.containsNode(value)); + bt.delete(value); + assertFalse(bt.containsNode(value)); + } + @Test public void givenABinaryTree_WhenTraversingInOrder_ThenPrintValues() { diff --git a/data-structures/src/test/java/com/baeldung/trie/TrieTest.java b/data-structures/src/test/java/com/baeldung/trie/TrieUnitTest.java similarity index 98% rename from data-structures/src/test/java/com/baeldung/trie/TrieTest.java rename to data-structures/src/test/java/com/baeldung/trie/TrieUnitTest.java index be7e5575d8..3dd3f27a60 100644 --- a/data-structures/src/test/java/com/baeldung/trie/TrieTest.java +++ b/data-structures/src/test/java/com/baeldung/trie/TrieUnitTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -public class TrieTest { +public class TrieUnitTest { @Test public void whenEmptyTrie_thenNoElements() {