Cover BSTRecursive with tests (#4180)
This commit is contained in:
parent
89b7ee42e6
commit
3a593d5d3c
@ -1,15 +1,17 @@
|
|||||||
package com.thealgorithms.datastructures.trees;
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
|
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* <h1>Binary Search Tree (Recursive)</h1>
|
* <h1>Binary Search Tree (Recursive)</h1>
|
||||||
*
|
*
|
||||||
* An implementation of BST recursively. In recursive implementation the checks
|
* An implementation of BST recursively. In recursive implementation the checks
|
||||||
* are down the tree First root is checked if not found then its childs are
|
* are down the tree First root is checked if not found then its children are
|
||||||
* checked Binary Search Tree is a binary tree which satisfies three properties:
|
* checked Binary Search Tree is a binary tree which satisfies three properties:
|
||||||
* left child is less than root node, right child is grater than root node, both
|
* left child is less than root node, right child is grater than root node, both
|
||||||
* left and right childs must themselves be a BST.
|
* left and right children must themselves be a BST.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* I have made public functions as methods and to actually implement recursive
|
* I have made public functions as methods and to actually implement recursive
|
||||||
@ -31,30 +33,8 @@ public class BSTRecursive {
|
|||||||
root = null;
|
root = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public Node getRoot() {
|
||||||
* main function for tests
|
return root;
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
BSTRecursive tree = new BSTRecursive();
|
|
||||||
tree.add(5);
|
|
||||||
tree.add(10);
|
|
||||||
tree.add(9);
|
|
||||||
assert !tree.find(4) : "4 is not yet present in BST";
|
|
||||||
assert tree.find(10) : "10 should be present in BST";
|
|
||||||
tree.remove(9);
|
|
||||||
assert !tree.find(9) : "9 was just deleted from BST";
|
|
||||||
tree.remove(1);
|
|
||||||
assert !tree.find(
|
|
||||||
1
|
|
||||||
) : "Since 1 was not present so find deleting would do no change";
|
|
||||||
tree.add(20);
|
|
||||||
tree.add(70);
|
|
||||||
assert tree.find(70) : "70 was inserted but not found";
|
|
||||||
/*
|
|
||||||
Will print in following order
|
|
||||||
5 10 20 70
|
|
||||||
*/
|
|
||||||
tree.inorder();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -82,7 +62,7 @@ public class BSTRecursive {
|
|||||||
Node temp = node.left;
|
Node temp = node.left;
|
||||||
node.left = null;
|
node.left = null;
|
||||||
node = temp;
|
node = temp;
|
||||||
} else { // both child are present
|
} else { // both children are present
|
||||||
Node temp = node.right;
|
Node temp = node.right;
|
||||||
// Find leftmost child of right subtree
|
// Find leftmost child of right subtree
|
||||||
while (temp.left != null) {
|
while (temp.left != null) {
|
||||||
@ -114,60 +94,6 @@ public class BSTRecursive {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively print Preorder traversal of the BST
|
|
||||||
*
|
|
||||||
* @param node the root node
|
|
||||||
*/
|
|
||||||
private void preOrder(Node node) {
|
|
||||||
if (node == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
System.out.print(node.data + " ");
|
|
||||||
if (node.left != null) {
|
|
||||||
preOrder(node.left);
|
|
||||||
}
|
|
||||||
if (node.right != null) {
|
|
||||||
preOrder(node.right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively print Postorder travesal of BST.
|
|
||||||
*
|
|
||||||
* @param node the root node
|
|
||||||
*/
|
|
||||||
private void postOrder(Node node) {
|
|
||||||
if (node == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (node.left != null) {
|
|
||||||
postOrder(node.left);
|
|
||||||
}
|
|
||||||
if (node.right != null) {
|
|
||||||
postOrder(node.right);
|
|
||||||
}
|
|
||||||
System.out.print(node.data + " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively print Inorder traversal of BST.
|
|
||||||
*
|
|
||||||
* @param node the root node
|
|
||||||
*/
|
|
||||||
private void inOrder(Node node) {
|
|
||||||
if (node == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (node.left != null) {
|
|
||||||
inOrder(node.left);
|
|
||||||
}
|
|
||||||
System.out.print(node.data + " ");
|
|
||||||
if (node.right != null) {
|
|
||||||
inOrder(node.right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serach recursively if the given value is present in BST or not.
|
* Serach recursively if the given value is present in BST or not.
|
||||||
*
|
*
|
||||||
@ -206,33 +132,6 @@ public class BSTRecursive {
|
|||||||
this.root = delete(this.root, data);
|
this.root = delete(this.root, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* To call inorder traversal on tree
|
|
||||||
*/
|
|
||||||
public void inorder() {
|
|
||||||
System.out.println("Inorder traversal of this tree is:");
|
|
||||||
inOrder(this.root);
|
|
||||||
System.out.println(); // for next line
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* To call postorder traversal on tree
|
|
||||||
*/
|
|
||||||
public void postorder() {
|
|
||||||
System.out.println("Postorder traversal of this tree is:");
|
|
||||||
postOrder(this.root);
|
|
||||||
System.out.println(); // for next li
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* To call preorder traversal on tree.
|
|
||||||
*/
|
|
||||||
public void preorder() {
|
|
||||||
System.out.println("Preorder traversal of this tree is:");
|
|
||||||
preOrder(this.root);
|
|
||||||
System.out.println(); // for next li
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To check if given value is present in tree or not.
|
* To check if given value is present in tree or not.
|
||||||
*
|
*
|
||||||
@ -246,23 +145,4 @@ public class BSTRecursive {
|
|||||||
System.out.println(data + " not found.");
|
System.out.println(data + " not found.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The Node class used for building binary search tree
|
|
||||||
*/
|
|
||||||
private static class Node {
|
|
||||||
|
|
||||||
int data;
|
|
||||||
Node left;
|
|
||||||
Node right;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor with data as parameter
|
|
||||||
*/
|
|
||||||
Node(int d) {
|
|
||||||
data = d;
|
|
||||||
left = null;
|
|
||||||
right = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Albina Gimaletdinova on 06/05/2023
|
||||||
|
*/
|
||||||
|
public class BSTRecursiveTest {
|
||||||
|
@Test
|
||||||
|
public void testBSTIsCorrectlyConstructedFromOneNode() {
|
||||||
|
BSTRecursive tree = new BSTRecursive();
|
||||||
|
tree.add(6);
|
||||||
|
|
||||||
|
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(tree.getRoot()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBSTIsCorrectlyCleanedAndEmpty() {
|
||||||
|
BSTRecursive tree = new BSTRecursive();
|
||||||
|
|
||||||
|
tree.add(6);
|
||||||
|
tree.remove(6);
|
||||||
|
|
||||||
|
tree.add(12);
|
||||||
|
tree.add(1);
|
||||||
|
tree.add(2);
|
||||||
|
|
||||||
|
tree.remove(1);
|
||||||
|
tree.remove(2);
|
||||||
|
tree.remove(12);
|
||||||
|
|
||||||
|
Assertions.assertNull(tree.getRoot());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBSTIsCorrectlyCleanedAndNonEmpty() {
|
||||||
|
BSTRecursive tree = new BSTRecursive();
|
||||||
|
|
||||||
|
tree.add(6);
|
||||||
|
tree.remove(6);
|
||||||
|
|
||||||
|
tree.add(12);
|
||||||
|
tree.add(1);
|
||||||
|
tree.add(2);
|
||||||
|
|
||||||
|
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(tree.getRoot()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBSTIsCorrectlyConstructedFromMultipleNodes() {
|
||||||
|
BSTRecursive tree = new BSTRecursive();
|
||||||
|
tree.add(7);
|
||||||
|
tree.add(1);
|
||||||
|
tree.add(5);
|
||||||
|
tree.add(100);
|
||||||
|
tree.add(50);
|
||||||
|
|
||||||
|
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(tree.getRoot()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user