private static Node buildTrie(int[] freq) {
		PriorityQueue<Node> pq = new PriorityQueue<Node>();
		
		for (char c = 0; c < R; c++) 
			if (freq[c] > 0)
				pq.add(new Node(c, freq[c], null, null));
		
		// if only one char
		if (pq.size() == 1) {
			if (freq['\0'] == 0) pq.add(new Node('\0', 0, null, null));
			else                 pq.add(new Node('\1', 0, null, null));
		}
		
		// merge two tries
		while (pq.size() > 1) {
			Node l = pq.remove();
			Node r = pq.remove();
			Node p = new Node('\0', l.freq + r.freq, l, r); // parent
			pq.add(p);
		}
		
		return pq.remove();
	}