Skip to content

Instantly share code, notes, and snippets.

@paplorinc
Created February 26, 2017 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paplorinc/128dcaad0f407610a2d23a9988623929 to your computer and use it in GitHub Desktop.
Save paplorinc/128dcaad0f407610a2d23a9988623929 to your computer and use it in GitHub Desktop.
package com.crossover.trial;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Node<T extends Comparable<? super T>> {
T value;
Node<T> left, right;
public Node(T value) {
this.value = value;
}
public Node(T value, Node<T> left) {
this(value);
this.left = left;
}
public Node(T value, Node<T> left, Node<T> right) {
this(value, left);
this.right = right;
}
public boolean contains(T value) {
for (Node<T> curr = this; curr != null; ) {
final int compareTo = value.compareTo(curr.value);
if (compareTo > 0) curr = curr.right;
else if (compareTo < 0) curr = curr.left;
else return true;
}
return false;
}
public void add(T value) {
Objects.requireNonNull(value);
insertNode(this, value);
}
private void insertNode(Node<T> curr, T value) {
if (value.compareTo(curr.value) < 0) {
if (curr.left != null) insertNode(curr.left, value);
else curr.left = new Node<>(value);
} else {
if (curr.right != null) insertNode(curr.right, value);
else curr.right = new Node<>(value);
}
}
@Override
public String toString() {
final List<T> list = toList(this, new ArrayList<>());
return list.toString();
}
private List<T> toList(Node<T> node, List<T> results) {
if (node != null) {
toList(node.left, results);
results.add(node.value);
toList(node.right, results);
}
return results;
}
}
package com.crossover.trial;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.TreeSet;
import static org.junit.Assert.*;
public class NodeTest {
private Node<Integer> bst;
private TreeSet<Integer> treeSet;
@Before
public void setUp() {
bst = new Node<>(10);
bst.right = new Node<>(15);
bst.right.left = new Node<>(12);
bst.left = new Node<>(5);
bst.left.left = new Node<>(3);
bst.left.right = new Node<>(6);
bst.left.left.left = new Node<>(2);
bst.left.left.left.left = new Node<>(1);
treeSet = new TreeSet<>(Arrays.asList(10, 15, 12, 5, 3, 6, 2, 1));
}
@Test
public void shouldConstructFromNodesValue() {
assertEquals(bst.toString(), treeSet.toString());
}
@Test
public void shouldConstructFromValue() {
Node<Integer> bst = new Node<>(10, new Node<>(5, new Node<>(3, new Node<>(2, new Node<>(1))), new Node<>(6)), new Node<>(15, new Node<>(12)));
assertEquals(this.bst.toString(), bst.toString());
}
@Test
public void shouldAddValue() {
bst.add(20);
bst.add(9);
treeSet.add(20);
treeSet.add(9);
assertEquals(bst.toString(), treeSet.toString());
}
@Test(expected = NullPointerException.class)
public void shouldThrowWhenAddNull() {
bst.add(null);
}
@Test
public void shouldFindElement() {
assertFalse(bst.contains(8));
assertTrue(bst.contains(10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment