Skip to content

Instantly share code, notes, and snippets.

@sfyn sfyn/gist:7622365
Created Nov 24, 2013

Embed
What would you like to do?
Ongoing work on a java imp of a B+tree
import java.util.*;
/**
* B+ Tree generic type
*
* @author Sofian Benaissa
*/
public class BplusTree<K extends Comparable<K>,V>
{
private static int maxKeys = 10;
private Node<K> root;
private Node<K> currentLeaf;
/**
* Default Constructor
*/
public BplusTree()
{
root = new Node<K>(null, 0);
currentLeaf = root;
}
/**
* Put method
*/
public V put(K key, V val)
{
int check = currentLeaf.add(key, val);
return val;
}
/**
* toString method
*/
public String toString()
{
return "";
}
/**
* Nested entry class
*/
public class Entry<K extends Comparable<K>,V> implements Map.Entry<K,V>
{
private K key;
private V value;
private Node<K> left;
private Node<K> right;
private Node<K> container;
/**
* Constructor
*/
public Entry(K k, V v, Node<K> c)
{
key = k;
value = v;
container = c;
}
/**
* Accessor for the key
*/
public K getKey()
{
return key;
}
/**
* Accessor for the value
*/
public V getValue()
{
return value;
}
/**
* Mutator for the value
*/
public V setValue(V val)
{
V old = value;
value = val;
return old;
}
/**
* Generate a hashcode
*/
public int hashCode()
{
String str = key + "";
return str.hashCode();
}
/**
* toString simply converts the value to a string
*/
public String toString()
{
return value + "";
}
}
/**
* Nested Node class
*/
private class Node<K extends Comparable<K>>
{
private Node<K> parent;
private int depth;
private ArrayList<Entry<K,V>> entries;
/**
* Constructor
*/
private Node(Node<K> p, int d)
{
parent = p;
entries = new ArrayList<Entry<K,V>>(maxKeys);
depth = d;
}
/**
* Add just a key
*
* @return 0 or less if successful, positive numbers indicate overflow
*/
private int add(K key)
{
return add(key, null);
}
/**
* Add a key and value
*
* @return 0 or less if successful, positive numbers indicate overflow
*/
private int add(K key, V val)
{
entries.add(new Entry<K,V>(key, val, this));
return (entries.size() - maxKeys);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.