Giter Site home page Giter Site logo

java-trie's Introduction

java-trie

Try the demo, or read more about the file format.

There are two main components to this project.

Java Implementation

A Java Trie / DAWG implementation, focusing on memory efficiency for ASCII trees (letters a-z, lowercase).

This implementation uses a sparse array to store children, and an int bit set to indicate which children a given node has, and whether a node is a valid end-of-word.

It includes a compress() function which greatly reduces the Trie's memory footprint.

The Java implementation includes a way to dump the Trie structure to an efficient binary format. You can then load this binary file into a JavaScript array, and have fast and low-memory Trie lookup operations on the client.

Creating a Trie in Java

trie = new MemoryEfficientTrie(Arrays.asList("cat", "zap"));
trie.add("bat");
trie.compress(); // merges shared suffixes, trims arrays
trie.containsWord("cat"); // true
trie.containsWord("cap"); // false

JavaScript Implementation

The JavaScript implementation loads a pre-generated binary file containing an optimal representation of the Trie data.

Loading a Trie

var oReq = new XMLHttpRequest();
oReq.open("GET", "trie.data", true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
	var arrayBuffer = oReq.response; // Note: not oReq.responseText
	if (arrayBuffer) {
		var byteArray = new Int32Array(arrayBuffer);
		trie = new Trie(byteArray);
		trie.containsWord('yurt'); // true
		trie.containsWord('yur'); // false
		trie.containsPrefix('yur'); // true
		trie.containsPrefix('yuq'); // false
	}
};
oReq.send(null);

Binary File Format

The binary format is an array of int objects.

Each node in the Trie is written to the array, first with the bit set value, followed by zero or more reference addresses for the node's children. These reference addresses point to other indices in the data array.

java-trie's People

Contributors

shmert avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.