Skip to content
2 changes: 1 addition & 1 deletion src/main/java/algorithms/TreeSearch.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package algorithms;

import utils.TreeNode;
import datastructs.adt.utils.TreeNode;
import utils.IPredicate;

/**
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/datastructs/adt/AVLTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package datastructs.adt;


import datastructs.adt.utils.ITreeBalance;

import java.util.Comparator;

/**
* Models an AVL Binary Search Tree
*/
public class AVLTree<E> extends BinarySearchTree<E>{

public AVLTree(Comparator<E> comparator, ITreeBalance balancer){

super(comparator);
this.treeBalance = balancer;
}


/**
* Push a new element in the ADT
*/
@Override
public void push(E element){

// call the base class function to do the insertion
super.push(element);

// now re-balance the tree
treeBalance.balance(this.getRoot());
}

private ITreeBalance treeBalance;
}
12 changes: 4 additions & 8 deletions src/main/java/datastructs/adt/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package datastructs.adt;

import utils.BSTInsertStratergy;
import utils.IPredicate;
import utils.TreeNode;
import utils.TreeNodeCreator;
import datastructs.adt.utils.BSTInsertStratergy;

import java.util.Comparator;

Expand All @@ -26,18 +23,17 @@ public class BinarySearchTree<E> extends BinaryTree<E> {
@Override
public void push(E element){

if(super.root_ == null){
if(super.root == null){

this.createRoot(element);
}
else {

boolean rslt = super.getInsertStrategy().insert(super.root_, super.root_, element, null);
boolean rslt = super.getInsertStrategy().insert(super.root, super.root, element, null);

if(rslt) {
super.nNodes_++;
super.nNodes++;
}
}

}
}
13 changes: 8 additions & 5 deletions src/main/java/datastructs/adt/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package datastructs.adt;

import datastructs.adt.utils.ITreeInsertStrategy;
import datastructs.adt.utils.TreeNode;
import datastructs.adt.utils.TreeNodeCreator;
import utils.*;

public class BinaryTree<E> extends Tree<E> {
Expand All @@ -18,21 +21,21 @@ public BinaryTree(ITreeInsertStrategy insertStrategy){
*/
public void push(E element){

if(super.root_ == null){
if(super.root == null){

this.createRoot(element);
}
else {

boolean rslt = super.insertStrategy_.insert(super.root_, null, element, new IPredicate<TreeNode<E>>() {
boolean rslt = super.treeInsertStrategy.insert(super.root, null, element, new IPredicate<TreeNode<E>>() {
@Override
public boolean satisfies(TreeNode<E> data) {
return (data == null);
}
});

if(rslt) {
super.nNodes_++;
super.nNodes++;
}
}

Expand All @@ -45,8 +48,8 @@ public boolean satisfies(TreeNode<E> data) {
protected void createRoot(E element){

TreeNodeCreator<E> creator = new TreeNodeCreator<>();
super.root_ = creator.create(element, null, 0, 2);
super.nNodes_++;
super.root = creator.create(element, null, 0, 2);
super.nNodes++;
}


Expand Down
54 changes: 54 additions & 0 deletions src/main/java/datastructs/adt/RingBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package datastructs.adt;

import java.lang.reflect.Array;

/**
* A ring buffer is an array together with read and write operations that wrap around. That is, when the
* last position of the array is reached, writing continues at the begin of the array, thereby erasing the oldest
* entries. The read operation starts at the oldest entry in the array.
*/
public class RingBuffer<T> {


/**
* Constructor
*/
public RingBuffer(int capacity){

if(capacity <=0 ){
throw new IllegalArgumentException("RingBuffer capacity should be greater than 0");
}

this.capacity = capacity;
this.elements = new Object[capacity];
}


public long capacity(){
return this.capacity;
}

/**
* Returns the number of the number of elements in the buffer
* @return
*/
public long size(){
return this.size;
}


/**
* The items of the buffer
*/
Object[] elements;

/**
* How many elements the buffer can store
*/
long capacity;

/**
* How many entries are currently in the buffer
*/
long size;
}
23 changes: 13 additions & 10 deletions src/main/java/datastructs/adt/Tree.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datastructs.adt;


import utils.ITreeInsertStrategy;
import utils.TreeNode;
import datastructs.adt.utils.ITreeInsertStrategy;
import datastructs.adt.utils.TreeNode;

/**
* Base class for trees
Expand All @@ -21,49 +21,52 @@ public abstract class Tree<E> implements IAdt<E> {
* Returns how many elements the ADT has
*/
@Override
public final int size(){return this.nNodes_;}
public final int size(){return this.nNodes;}


/**
* Return the insertion strategy for the node
*/
public final ITreeInsertStrategy getInsertStrategy() {
return insertStrategy_;
return this.treeInsertStrategy;
}

/**
* The insertion strategy to be used
*/
public final void setInsertStrategy(ITreeInsertStrategy insertStrategy) {
this.insertStrategy_ = insertStrategy;
this.treeInsertStrategy = insertStrategy;
}


/**
* Returns the root node of the tree
*/
public TreeNode<E> getRoot() { return root_; }
public TreeNode<E> getRoot() { return this.root; }

/**
* Constructor
*/
protected Tree(ITreeInsertStrategy insertStrategy)
{
this.insertStrategy_ = insertStrategy;
this.treeInsertStrategy = insertStrategy;
}


/**
* The root of the tree
*/
protected TreeNode<E> root_ = null;
protected TreeNode<E> root = null;


/**
* How many nodes the Tree has
*/
protected int nNodes_ = 0;
protected int nNodes = 0;


/**
* Insert strategy for the tree
*/
protected ITreeInsertStrategy insertStrategy_;
protected ITreeInsertStrategy treeInsertStrategy;
}
8 changes: 8 additions & 0 deletions src/main/java/datastructs/adt/utils/AVLTreeBalancer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package datastructs.adt.utils;

public class AVLTreeBalancer implements ITreeBalance {

public <NodeType> void balance(NodeType nodeType){

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package utils;
package datastructs.adt.utils;

import utils.IPredicate;

import java.util.Comparator;

Expand All @@ -21,7 +23,7 @@ public BSTInsertStratergy(Comparator<E> comparator){
* the calling site the newly created node
*/
public boolean insert(TreeNode<E> root, TreeNode<E> parent, E data,
IPredicate<TreeNode<E>> insertPosPredicate){
IPredicate<TreeNode<E>> insertPosPredicate){

if(comparator.compare(data, parent.getData()) == 1){

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils;
package datastructs.adt.utils;


import utils.*;

public class DfsInsertStrategy<E> implements ITreeInsertStrategy<E> {

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/datastructs/adt/utils/ITreeBalance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package datastructs.adt.utils;

/**
* General interface for providing balancing algorithms for trees
*/
public interface ITreeBalance {

<NodeType> void balance(NodeType node);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package utils;
package datastructs.adt.utils;

import utils.IPredicate;

/**
* Insertion strategy for trees
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils;
package datastructs.adt.utils;

public enum TreeInsertMethod {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils;
package datastructs.adt.utils;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package utils;
package datastructs.adt.utils;

import datastructs.adt.utils.TreeNode;

public class TreeNodeCreator<E> {

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/datastructs/AllDataStructsTestsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public static void run(String[] args){
ArrayQueueTestRunner.run(args);
SingleLinkedListTestRunner.run(args);
BinaryTreeTestRunner.run(args);
AVLTreeTestRunner.run(args);
MatrixDataSetTestRunner.run(args);

RingBufferTestRunner.run(args);
}

public static void main(String[] args) {
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/datastructs/adt/AVLTreeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package datastructs.adt;

import datastructs.adt.utils.AVLTreeBalancer;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Comparator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class AVLTreeTest {

static Comparator<Integer> getComparator(){
return (Integer a, Integer b)->{
if( a.equals(b)){
return 0;
}
else if( a.intValue() > b.intValue()){
return 1;
}

return -1;
};
}

/**
* Test Scenario: Application inserts a new node that violates the AVL property
* Expected Output: AVL property should be restored with single rotatio
*/
@Test
@Ignore
public void testSingleRotation(){

AVLTree<Integer> tree = new AVLTree<Integer>(AVLTreeTest.getComparator(), new AVLTreeBalancer());
tree.push(3);

assertNotNull("Root node was not created", tree.getRoot());
assertEquals("Invalid root node data", tree.getRoot().getData().intValue(), 3);

//add a second node
tree.push(2);

assertNotNull("Child node was not created", tree.getRoot().getChild(0));
assertEquals("Invalid root node data", tree.getRoot().getChild(0).getData().intValue(), 2);

//this insertion should cause re-balancing
//add a second node
tree.push(1);

assertNotNull("Root node was not created", tree.getRoot());
assertEquals("Invalid root node data", tree.getRoot().getData().intValue(), 2);
assertNotNull("Left Child node was not created", tree.getRoot().getChild(0));
assertEquals("Invalid left child data", tree.getRoot().getChild(0).getData().intValue(), 1);
assertNotNull("Right child node was not created", tree.getRoot().getChild(1));
assertEquals("Invalid right child data", tree.getRoot().getChild(0).getData().intValue(), 3);

}
}
Loading