0

Okey, so I have some problems with my way of thinking again (this time I'm sick). I need to implement a .txt file into a 2-3 tree wich I have made the foundations of.

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[] key1Values = new int[2]; //every key contains 2 values
    int[] key2Values = new int[2]; //every key contains 2 values
    Node[] subtrees = new Node[3]; //every node can contain pointers to 3 different nodes
    Node[] parent = new Node[1]; //every node contains a pointer to a parent node, if root parent      is null

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

My Tree 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;
        root.parent = null;
    }
    //Return the values of the key if we find it
    public int[] get(Node n, String key){
        if(n.isLeaf){
            if(n.keys[0].equals(key)){
                return(n.key1Values);
            }
            else if(n.keys[1].equals(key)){
                return(n.key2Values);
            }
            else{
                return null;
            }
        }
        else if(key.length() < n.keys[0].length()){
            return(get(n.subtrees[0],key));
        }
        else if(n.numberOfKeys == 2 && key.length() < n.keys[1].length()){
            return(get(n.subtrees[1],key));
        }
        else{
            return(get(n.subtrees[2],key));
        }
    }
    //put new values to the key
    public void put(Node n, int[] value, String key){
        if(n.keys[0].equals(key)){
            n.key1Values = value;
        }
        else if(n.keys[1].equals(key)){
            n.key2Values = value;
        }
        else if(n.keys[0].length() < key.length()){
            put(n.subtrees[0],value,key);
        }
        else if(n.numberOfKeys == 2 && n.keys[1].length() < key.length()){
            put(n.subtrees[1],value,key);
        }
        else{
            put(n.subtrees[2],value,key);
        }
    }

    public int size(){
        return(this.n);
    }
}

And my Driver class

package kth.id2010.lab.lab04;


import edu.princeton.cs.introcs.In;
import java.net.URL;

public class Driver {

    public static void main(String[] args) {
        URL url = Driver.class.getResource("/kap1.txt");
        System.out.println(System.getProperty("user.dir"));
        In input = new In(url);
        String[] usedWords;

        while(!input.isEmpty()){
            String line = input.readLine().trim();
            String[] words = line.split(" ");
            for (String word : words) {
                System.out.println(word);
            }
        }

    }
}

So for my assignment each word in words is 1 key, keyValue1[0]is the occurrence number = index in words and keyValue1[1] is the occurrence count. So my thought was that for each word in wordsI add it to a node and add the index number as the first value, and then I add the word to usedWords and check how many times that word is in words. The only thing i'm stuck with is how to add it as a Node and try to sort it, and put the bigger Node that came befor as it´s parent.

Necrozze
  • 61
  • 1
  • 7
  • Please outline examples from your .txt file. Also: where is value1[0] or value1[1]? It's just falling from the sky in your description. – SME_Dev Oct 10 '14 at 15:22
  • @SME_Dev my .txt file contains the first chapter of Tale of Two Cities, and then each word is being put in `words`so for example `words[0]`= It , `words[1]`= was , `words[2]` = the And `value1[0]` and `value1[1]`are in the Node class as `keyValue1` forgot that I named them that, fixed it now – Necrozze Oct 10 '14 at 15:27

0 Answers0