HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Saving and restoring RadixTree object

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
restoringradixtreeandobjectsaving

Problem

I have a large word-file which is fixed with over 240 000 words. I need to check if a specific word exists in this list. I thought that it would be a good idea to create a RadixTree for this problem.

The text file consists of 240 000 lines, each of which contains one word, which is read into a String. Each string is then parsed into a char[] to access the individual characters, each char is then placed into the tree separately.

Android puts a limit of 16-26MB of RAM usage per application if I am correct. When my application runs it consistently hits this mark during the initialization of the large Vocabulary object. It takes around 47 seconds to initialize the Vocabulary object and about 1/10 of a ms to see if a word exists in this Vocabulary. Right now I am afraid that I made a mistake by implementing the RadixTree data-structure since it costs too much RAM and takes way too long with 47 seconds...

Would this every be possible to use on a real device? E.g. that the speed would be under 0.5 seconds instead of 47.

My idea was to load the object to later restore it. However this is even slower, loading an object took about 5x longer. Oh dear.

Vocabulary Class

package nl.mprog.ghost.datastructure;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import java.io.Serializable;

import nl.mprog.ghost.enumeration.Language;

public class Vocabulary implements Serializable {
    RadixTree mRadixTree;
    Language mLanguage;

    public Vocabulary(Context context, Language language) {
        this.mLanguage = language;
        mRadixTree = new RadixTree(context, language);
    }

    public boolean isWord(String word) {
        return mRadixTree.isWord(word);
    }


RadixTree class

```
package nl.mprog.ghost.datastructure;

import android.content.Context;
import android.util.Log;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

import nl.mprog.ghost.R;
import nl.m

Solution

Instead of using a raw array to store the children you can instead use a HashMap. This will better optimize the allocations that you do.

Using a map will also remove all the for loops for searching a key in a layer.

Map mChildren = new HashMap();

public void insert(char character) {

    if(mChildren.containsKey(character)) {
        return;
    }

    addChild(new RadixTreeNode(character));
}

private void addChild(RadixTreeNode node) {
    mChildren.put(node.mCharacter, node);
}


Also you can get the individual characters in a String by using charAt, no need to get the char array.

Code Snippets

Map<Character, RadixTreeNode> mChildren = new HashMap<Character, RadixTreeNode>();



public void insert(char character) {

    if(mChildren.containsKey(character)) {
        return;
    }

    addChild(new RadixTreeNode(character));
}

private void addChild(RadixTreeNode node) {
    mChildren.put(node.mCharacter, node);
}

Context

StackExchange Code Review Q#88143, answer score: 2

Revisions (0)

No revisions yet.