0

I have been given an assignment to create a 2-3 search tree that is supposed to support a few different operations each divided in to different stages of the assignment. For stage 1 I'm supposed to suport the operations get, put and size. I'm curently trying to implement the get operation but I'm stuck and I can't wrap my head around how to continue so I'm questioning all of my code I have written and felt like a need someone elses input.

I have looked around how to develop a 2-3 search tree but what I found was alot of code that made no sence to me or it just did not do what I needed it to do, and I wanted to try and make it for my self from scratch and here we are now.

My Node class

package kth.id2010.lab.lab04;

public class Node {
    boolean isLeaf = false; 
    int numberOfKeys;
    String[] keys = new String[2]; //each node can contain up to 2 keys
    int[] values = new int[2]; //every key contains 2 values
    Node[] subtrees = new Node[3]; //every node can contain pointers to 3 different nodes




    Node(Node n) {
        n.numberOfKeys = 0;
        n.isLeaf = true;
    }

}

My Tree creating class

package kth.id2010.lab.lab04;

public class Tree {

    Node root; // root node of the tree
    int n; // number of elements in the tree

    private Tree(){
        root = new Node(root);
        n = 0;       
    }
    //Return the values of the key if we find it
    public int[] get(String key){
        //if the root has no subtrees check if it contain the right key
        if(this.root.subtrees.length == 0){
            if(this.root.keys[0] == key){
                return(this.root.keys[0].values);
            }
            else if(this.root.keys[1] == key){
                return(this.root.keys[1].values);
            }
        }
        //if noot in root, check its subtree nodes
        //and here I can't get my head around how to traverse down the tree
        else{
            for(int i = 0; i < this.root.subtrees.length; i++){
                for(int j = 0; j < this.root.subtrees[i].keys.length; j++){
                    if(this.root.subtrees[i].keys[j] == key){
                        return(this.root.subtrees[i].keys[j].values);
                    }
                }
            } 
        }
        return null;
    }
}

What I can tell for my self is that I need to find a way to bind values[] to each key but I can't figure out a way how. Might be the lack of sleep or that I'm stuck in this way of thinking.

Necrozze
  • 61
  • 1
  • 7

1 Answers1

2

bind values[] to each key

It might make more sense to use a HashMap to do that mapping for you, since that's what it's for. Beyond that, if you have two keys and each key has two values, you have 4 values, not 2 ;)

In general, the get method in a tree structure is almost always implementable recursively. Here is a very general implementation of a get algorithm for a 2-3 tree in psudo-code.

V get<K, V>(Node<K, V> node, K key)
{
    if(node.is_leaf())
    {
        return node.keys.get(key); // This will either return the value, or null if the key isn't in the leaf and thus not in the tree
    }
    if(key < node.left_key)
    {
        return get(node.left_child, key); // If our key goes to the left, go left recursively
    }
    else if(node.two_node && key <= node.right_key)
    {
        return get(node.center_child, key) // If we have two keys, and we're less than the second one, we go down the center recursively
    }
    else
    {
        return get(node.right_child, key); // If we've gotten here, we know we're going right, go down that path recursively
    }
}

That should get you started in the right direction. Insertion/deletion for 2-3 trees is a bit more complicated, but this should at least get your head around how to think about it. Hint; Your Node class needs to be doubly-linked, that is each node/leaf needs to reference its parent node as well as its children, and the root is simply a node whose parent is null.

aruisdante
  • 8,875
  • 2
  • 30
  • 37
  • Great, I'll give it a try. I'm just wondering about what the `` `T key`stands for? I have seen it in some other solutions but never understod what it is. – Necrozze Oct 08 '14 at 17:47
  • It's called a [generic](http://docs.oracle.com/javase/tutorial/extra/generics/intro.html) in Java (C++ has a related thing called a ``template``, as do many other statically typed languages). It's essentially a way of saying 'this method can work on arbitrary types that will be specified later'. In this case, it's stating that the type of ``key`` will be specified by whatever's inside the ``<>`` brackets. So for example ``get(...)`` says that you're searching using an integer key. In this case, that ``T`` is going to be whatever you define your ``Node`` type to use for keys. – aruisdante Oct 08 '14 at 18:18
  • I.E. they will be ``Node`` where ``K`` is the type of the keys and ``V`` is the type of the value. Note that there is an implicit assumption here that ``T < T`` and the other mathematical operators are legal. Java has a specific [``Comparable``](http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) interface that can be used instead, and you can specify that ``T`` must implement this interface by saying ````. But that's getting into really specific Java stuff, this was more meant to be a general pseduocode. I've edited my answer to make that clearer. – aruisdante Oct 08 '14 at 18:20