package Autocomplete;

import java.util.*;


public class Tree extends Main{
	Node origin = null;
	public ArrayList<ArrayList<Node>> layers = new ArrayList<ArrayList<Node>>();
	public ArrayList<String> trainingData = new ArrayList<String>();
	
	public  Tree(){


	}

	// search character tree for possible completions of a given string
	public void charSearch(String s){
		Node n = origin;
		boolean lowestNodeFound = false;

		// for every character in string
		for (int c = 0; c < s.length(); c++) {
			// for every child of corresponding node for current character
			for (int child = 0; child <= n.children.size(); child++) {

				// if no matching children found, break
				if (child == n.children.size()) {
					lowestNodeFound = true;
					break;

				// if child found, move to that node	
				} else if (n.children.get(child).content == s.charAt(c)) {	
					n = n.children.get(child);
					break;
				}
			}

			if (lowestNodeFound) {
				break;
			}
		}

		// search for all completed children of lowest found node
		searchCompletedChildren(n);
	}

	// depth first search from given node to find every completed-word child beneath it
	public void searchCompletedChildren(Node n) {
		for (int i = 0; i < n.children.size(); i++) {
			if (((CharNode) n.children.get(i)).isWord) {
				super.completionsFromCharSearch.add(n.children.get(i));
			}
			searchCompletedChildren(n.children.get(i));
		}
	}

	// search word tree for possible completions of a given ngram
	public ArrayList<Node> wordSearch(String[] ngram){
		Node n = origin;
		boolean lowestNodeFound = false;

		// for every word in ngram
		for (int word = 0; word < ngram.length; word++) {
			// for every child of corresponding node for current character
			for (int child = 0; child <= n.children.size(); child++) {

				// if no matching children found, break
				if (child == n.children.size()) {
					lowestNodeFound = true;
					break;

				// if child found, move to that node	
				} else if (n.children.get(child).content.equals(ngram[word])) {	
					n = n.children.get(child);
					break;
				}
			}

			if (lowestNodeFound) {
				break;
			}
		}

		// return all children of ngram
		return n.children;
	}

//	public ArrayList<Node> intersect(ArrayList<Node> a, ArrayList<Node> b){
//		
//	}
//	
//	public ArrayList<Node> relativeComplement(ArrayList<Node> a, ArrayList<Node> b){
//		
//	}
//	
//	public ArrayList<Node> onion(ArrayList<Node> a, ArrayList<Node> b){
//		//union
//	}


	//@SuppressWarnings("static-access")
	public void construct(Boolean isChar){
		if (isChar){
			
		} else {
			for (int word = 0; word < trainingData.size() - super.n; word++){
				for(int gram = 0; gram < super.n; gram++){
					System.out.print(trainingData.get(word + gram) + "-");
				}
				System.out.println("");
			}
		}
	}
}


@@ -2537,24 +2537,137 @@
 %7B%0A%09%09
-if (isChar)%7B%0A%09%09%09
+// check if tree files exist%0A%09%09// if yes, reconstruct from those%0A%09%09// else:%0A%0A%09%09// character tree%0A%09%09if (isChar)%7B%0A%09%09%0A%09%09// word tree
 %0A%09%09%7D
@@ -2884,16 +2884,16 @@
 ln(%22%22);%0A
-
 %09%09%09%7D%0A%09%09%7D
@@ -2895,12 +2895,411 @@
 %7D%0A%09%09%7D%0A%09%7D
+%0A%0A%09// write entire tree structure to txt file for later reconstruction%0A%09public void writeToFile() %7B%0A%09%09// open file here%0A%0A%09%09dfs(this.origin);%0A%0A%09%09// close file%0A%09%7D%0A%0A%09public void dfs(Node n) %7B%0A%09%09for (int i = 0; i %3C n.children.size(); i++) %7B%0A%09%09%09%0A%09%09%09// Write (n.children.get(i).content + %22 %22 + n.children.get(i).probability + %22%5Cn%22) to file%0A%09%09%09%0A%09%09%09dfs(n.children.get(i));%0A%09%09%09%0A%09%09%09// Write %22u%22 to file%0A%09%09%7D%0A%09%7D
 %0A%7D%0A%0A

Set up file writing procedures