Skip to content

Commit

Permalink
Splay tree refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfiset committed May 30, 2019
1 parent e862047 commit ab7c8a4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 71 deletions.
123 changes: 60 additions & 63 deletions com/williamfiset/datastructures/binarysearchtree/SplayTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,79 @@
import java.util.Scanner;

/**
* Standard Splay Tree Implementation,supports generic data(must implement Comparable)
* Standard Splay Tree Implementation, supports generic data(must implement Comparable)
*
* <p>The Basic Concept of SplayTree is to keep frequently used nodes close to the root of the tree
* It performs basic operations such as insertion,search,delete,findMin,findMax in O(log n)
* amortized time Having frequently-used nodes near to the root can be useful in implementing many
* algorithms. e.g: Implementing caches, garbage collection algorithms etc Primary disadvantage of
* the splay tree can be the fact that its height can go linear. This causes the worst case running
* times to go O(n) However, the amortized costs of this worst case situation is logarithmic, O(log
* n)
*
* @author Ashiqur Rahman,https://github.com/ashiqursuperfly
*/
public class SplayTree<T extends Comparable<T>> {

/*The Basic Concept of SplayTree is to keep frequently used nodes close to the root of the tree
* It performs basic operations such as insertion,search,delete,findMin,findMax in O(log n) amortized time
* Having frequently-used nodes near to the root can be useful in implementing many algorithms.
* e.g: Implementing caches,garbage collection algorithms etc
* Primary disadvantage of the splay tree can be the fact that its height can go linear.
* This causes the worst case running times to go O(n)
* However, the amortized costs of this worst case situation is logarithmic, O(log n)
* */

class BinaryTree<T extends Comparable<T>> implements TreePrinter.PrintableNode {

private BinaryTree<T> leftChild, rightChild;
private T data;

public BinaryTree(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
}

@Override
public BinaryTree<T> getLeft() {
return leftChild;
}
private BinaryTree<T> root;

public void setLeft(BinaryTree<T> leftChild) {
this.leftChild = leftChild;
}
public static class BinaryTree<T extends Comparable<T>> implements TreePrinter.PrintableNode {
private T data;
private BinaryTree<T> leftChild, rightChild;

public BinaryTree(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
}

@Override
public BinaryTree<T> getRight() {
return rightChild;
}
@Override
public BinaryTree<T> getLeft() {
return leftChild;
}

public void setRight(BinaryTree<T> rightChild) {
this.rightChild = rightChild;
}
public void setLeft(BinaryTree<T> leftChild) {
this.leftChild = leftChild;
}

@Override
public String getText() {
return data.toString();
}
@Override
public BinaryTree<T> getRight() {
return rightChild;
}

public T getData() {
return data;
}
public void setRight(BinaryTree<T> rightChild) {
this.rightChild = rightChild;
}

public void setData(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
}
@Override
public String getText() {
return data.toString();
}

@Override
public String toString() {
public T getData() {
return data;
}

return TreePrinter.getTreeDisplay(this);
}
}
public void setData(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
}

public class SplayTree<T extends Comparable<T>> {
@Override
public String toString() {

private BinaryTree<T> root;
return TreePrinter.getTreeDisplay(this);
}
}

/** Public Methods * */
public SplayTree() {
Expand All @@ -104,7 +101,7 @@ public BinaryTree<T> search(T node) {
return this.root.getData().compareTo(node) == 0 ? this.root : null;
}

/** Inserts a node into the tree and splays it on top,returns the new root* */
/** Inserts a node into the tree and splays it on top, returns the new root* */
public BinaryTree<T> insert(T node) {
if (root == null) {
root = new BinaryTree<>(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import static org.junit.jupiter.api.Assertions.*;

import com.williamfiset.datastructures.binarysearchtree.SplayTree;
import com.williamfiset.datastructures.utils.TestUtils;
import java.util.*;
import org.junit.jupiter.api.Test;

Expand All @@ -10,8 +12,7 @@ class SplayTreeTest {
@Test
void getRoot() {
SplayTree<Integer> splayTree = new SplayTree<>();
List<Integer> data =
com.williamfiset.datastructures.utils.TestUtils.randomIntegerList(100, MIN, MAX);
List<Integer> data = TestUtils.randomIntegerList(100, MIN, MAX);
for (int i : data) {
splayTree.insert(i);
assertEquals(i, splayTree.getRoot().getData());
Expand All @@ -22,7 +23,7 @@ void getRoot() {
void splayInsertDeleteSearch() {
SplayTree<Integer> splayTree = new SplayTree<>();
List<Integer> data =
com.williamfiset.datastructures.utils.TestUtils.randomUniformUniqueIntegerList(
TestUtils.randomUniformUniqueIntegerList(
100); // Note : we dont want duplicate values here to test "search" after "delete"
// should assertNull
for (int i : data) {
Expand All @@ -41,8 +42,7 @@ void splayInsertDeleteSearch() {
@Test
void insertSearch() {
SplayTree<Integer> splayTree = new SplayTree<>();
List<Integer> data =
com.williamfiset.datastructures.utils.TestUtils.randomIntegerList(100, MIN, MAX);
List<Integer> data = TestUtils.randomIntegerList(100, MIN, MAX);
for (int i : data) {
splayTree.insert(i);
assertEquals(i, splayTree.getRoot().getData());
Expand All @@ -52,7 +52,7 @@ void insertSearch() {
@Test
void findMax() {
SplayTree<Integer> splayTree = new SplayTree<>();
List<Integer> data = com.williamfiset.datastructures.utils.TestUtils.sortedIntegerList(-50, 50);
List<Integer> data = TestUtils.sortedIntegerList(-50, 50);
for (int i : data) {
splayTree.insert(i);
assertEquals(i, splayTree.findMax(splayTree.getRoot()));
Expand All @@ -63,8 +63,7 @@ void findMax() {
@Test
void splayTreePriorityQueueConsistencyTest() {
SplayTree<Integer> splayTree = new SplayTree<>();
List<Integer> data =
com.williamfiset.datastructures.utils.TestUtils.randomUniformUniqueIntegerList(100);
List<Integer> data = TestUtils.randomUniformUniqueIntegerList(100);
Queue<Integer> pq = new PriorityQueue<>(100, Collections.reverseOrder());

// insertion
Expand Down

0 comments on commit ab7c8a4

Please sign in to comment.