Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinarySearchTree Node Removal problem solving #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/main/java/com/company/sample/tree/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@
import java.util.List;

public class Node {
boolean isRoot = false;
public boolean isRoot = false;

public Node(String value) {
this.value = value;
}
public int depth= 0;

public int numericValue;

public int depth = 0;

public List<Node> children = new LinkedList<>();
public Node leftChild;
public Node rightChild;
public String value;

public Node(int numericValue) {
this.numericValue = numericValue;
}

public void addChild(Node child) {
children.add(child);
}

public void addChildren(Node ...values) {
public void addChildren(Node... values) {
children.addAll(Arrays.asList(values));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.company.sample.tree.btree;

import com.company.sample.tree.Node;

public class BinaryTreeNodeRemoval {

public Node removeRootNodeOfBSearchTre(Node binarySearchTree) {
Node prevLeft = binarySearchTree.leftChild;
binarySearchTree.numericValue = prevLeft.numericValue;
binarySearchTree.rightChild = mergeBTree(binarySearchTree.rightChild, prevLeft.rightChild);
binarySearchTree.leftChild = prevLeft.leftChild;
return binarySearchTree;
}

public Node mergeBTree(Node bigger, Node smaller) {
if (bigger.leftChild == null) {
bigger.leftChild = smaller;
return bigger;
}
bigger.leftChild = mergeBTree(bigger.leftChild, smaller);
return bigger;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.company.sample.tree.btree;

import com.company.sample.tree.Node;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;

import static org.assertj.core.api.Assertions.assertThat;

class BinaryTreeNodeRemovalTest {
private static BinaryTreeNodeRemoval binaryTreeNodeRemoval = new BinaryTreeNodeRemoval();
private static Node testTree;


// 31
// / \
// 17 72
// / \
// 55 82
// / \
// 49 69
@BeforeAll
public static void buildFullBinarySearchTree() {
//Root level
Node root = new Node(31);
root.isRoot = true;
testTree = root;

//2nd
Node leftChildOfRoot = new Node(17);
Node rightChildOfRoot = new Node(72);
root.leftChild = leftChildOfRoot;
root.rightChild = rightChildOfRoot;

//3rd
Node leftOfRightRootChild = new Node(55);
Node rightOfRightRootChild = new Node(82);
rightChildOfRoot.leftChild = leftOfRightRootChild;
rightChildOfRoot.rightChild = rightOfRightRootChild;

//4th
Node leftChildOfLeftChildOfRightRootChild = new Node(49);
Node rightChildOfLeftChildOfRightRootChild = new Node(69);
leftOfRightRootChild.leftChild = leftChildOfLeftChildOfRightRootChild;
leftOfRightRootChild.rightChild = rightChildOfLeftChildOfRightRootChild;
}

@Test
public void removeRightChildNode_thenReturnedTreeShouldBeBinarySearchTree() {
// Given

// When
testTree.rightChild = binaryTreeNodeRemoval.removeRootNodeOfBSearchTre(testTree.rightChild);

// Then
assertThat(verifyBtree(testTree)).isTrue();
}

@Test
public void testVerifyBtree_whenValidBinarySearchTreeIsGiven_thenReturningTrue() {
// Given

// When
boolean validBtree = verifyBtree(testTree);

// Then
assertThat(validBtree).isTrue();
}

private boolean verifyBtree(Node tree) {
LinkedList<Node> ascending = traverseInOrder(tree);
Node prev = null;

for (Node node : ascending) {
System.out.println(node.numericValue);
if (prev == null) {
prev = node;
} else {
if (node.numericValue < prev.numericValue) {
return false;
}
}
}
return true;
}

private LinkedList<Node> traverseInOrder(Node parent) {
LinkedList<Node> sorted = new LinkedList<>();
if (parent.leftChild != null) {
sorted.addAll(traverseInOrder(parent.leftChild));
}
sorted.add(parent);
if (parent.rightChild != null) {
sorted.addAll(traverseInOrder(parent.rightChild));
}
return sorted;
}
}