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 (((CharNode) n.children.get(child))).equals(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){
		// check if tree files exist
		// if yes, reconstruct from those
		// else:

		// character tree
		if (isChar){
		
		// word tree
		} 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("");
			}
		}
	}

	// write entire tree structure to txt file for later reconstruction
	public void writeToFile() {
		// open file here

		dfs(this.origin);

		// close file
	}

	public void dfs(Node n) {
		for (int i = 0; i < n.children.size(); i++) {
			
			// Write (n.children.get(i).content + " " + n.children.get(i).probability + "\n") to file
			
			dfs(n.children.get(i));
			
			// Write "u" to file
		}
	}
}


@@ -790,18 +790,16 @@
 t node%09%0A
-//
 %09%09%09%09%7D el
@@ -839,17 +839,16 @@
 (child))
-)
 .equals(
@@ -864,18 +864,16 @@
 c))) %7B%09%0A
-//
 %09%09%09%09%09n =
@@ -893,32 +893,34 @@
 et(child);%0A%09%09%09%09%09
+//
 break;%0A%09%09%09%09%7D%0A%09%09%09
@@ -1972,16 +1972,28 @@
 lse if (
+((WordNode) 
 n.childr
@@ -2005,24 +2005,17 @@
 t(child)
-.content
+)
 .equals(
@@ -2024,18 +2024,24 @@
 ram%5B
-wor
+chil
 d%5D)) %7B%09
+%09%09%09%09%09
 %0A%09%09%09

Check line 32 and 73