Published on

TRIE Data Structure

A very simple implementation of TRIE data structure
public class TRIE implements Retrievable {

	private int size;
	private Node root;
	private ArrayList<Node> strings = new ArrayList<Node>();

	TRIE(){
		size = 0;
		root = new Node('*');
	}

	public Node getRoot() {
		return root;
	}
	@Override
	public int size() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public void insertString(String str, int it, Node node) {
		if(it == str.length()) {
			node.setFinal();
			size++;
			return;
		}
		if(node.isParent(str.charAt(it))) {
			insertString(str,it+1,node.getChild(str.charAt(it)));
		}
		else {
			Node temp = new Node(str.charAt(it));
			node.setChild(temp);
			insertString(str,it+1,temp);
		}
	}

	@Override
	public boolean searchString(String key,int it, Node node) {
		boolean flag = false;
		if(it == key.length()) {
			if(node.isFinal()) {
				flag = true;
				return flag;
			}
			else {
				flag = false;
				return flag;
			}
		}
		if(node.isParent(key.charAt(it))) {
			flag = searchString(key,it+1,node.getChild(key.charAt(it)));
		}
		return flag;
	}

	public void printStrings(Node node) {
		if(node == root) {
			System.out.println("The following strings have been stored in the TRIE : ");
		}
		strings.add(node);
		if(node.isExternal() || node.isFinal()) {
			int i;
			for(i = 1; i < strings.size(); ++i) {
				System.out.print(strings.get(i).getCharacter());
			}
			System.out.println();
		}
		for(int i = 0; i < 26; ++i) {
			if(node.hasThisChild(i)) {
				printStrings(node.getChild(i));
			}
		}
		strings.remove(strings.size() - 1);
	}

}
public interface Retrievable {
	int size();
	boolean isEmpty();
	void insertString(String str, int it, Node node);
	boolean searchString(String key,int it, Node node);
	void printStrings(Node node);
}
public class Node {
	public static final int ALPHABETS = 26;
	private Node[] children = new Node[ALPHABETS];
	private char character;
	private boolean isFinal;

	Node(char ch) {
		character = ch;
		for(int i = 0; i < ALPHABETS; ++i) {
			children[i] = null;
		}
		isFinal = false;
	}

	public boolean isParent(char ch) {
		return children[ch - 'a'] != null;
	}

	public char getCharacter() {
		return character;
	}

	public void setChild(Node node) {
		int index = node.getCharacter() - 'a';
		children[index] = node;
	}

	public Node getChild(char ch) {
		int index = ch - 'a';
		return children[index];
	}

	public Node getChild(int i) {
		return children[i];
	}

	public void setFinal() {
		isFinal = true;
	}

	public boolean isFinal() {
		return isFinal;
	}

	public boolean isExternal() {
		boolean flag = true;
		for(int i = 0; i < ALPHABETS; ++i) {
			if(children[i] != null) {
				flag = false;
			}
		}
		return flag;
	}

	public Node getNextChild(int it) {
		for(int i = it+1; i < ALPHABETS; ++i) {
			if(children[i] != null) {
				return children[i];
			}
		}
		return null;
	}

	public boolean hasThisChild(int i) {
		return children[i] != null;
	}

}