All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.itool.compress.huffman.s2.HuffmanTree Maven / Gradle / Ivy

The newest version!
package com.mycomm.itool.compress.huffman.s2;

/**
  * Huffman Tree Class
  * Written By: Mitchell Pomery (21130887)
  * Used to create mappings for use in huffman coding
 **/

import java.util.ArrayList;

public class HuffmanTree implements Comparable
{
	public int frequency = -1;
	public byte character;
	public boolean leafnode = false;
	public HuffmanTree left = null;
	public HuffmanTree right = null;
	
	
	public HuffmanTree(byte item, HuffmanTree b1, HuffmanTree b2, int f) {
		frequency = f;
		character = item;
		left = b1;
		right = b2;
		leafnode = true;
	}
	
	
	public HuffmanTree(HuffmanTree b1, HuffmanTree b2, int f) {
		frequency = f;
		left = b1;
		right = b2;
	}
	
	
	/**
	  * Constructs an empty huffman tree
	 **/
	public HuffmanTree() {
	}
	
	
	
	public boolean isLeaf() {
		return leafnode;
	}
	
	
	public byte getCharacter() {
		return character;
	}
	
	
	public HuffmanTree getLeft() {
		return left;
	}
	
	
	public HuffmanTree getRight() {
		return right;
	}
	
	
	public int compareTo(HuffmanTree bt) {
			return frequency - bt.frequency;
	}
	
	
	
	public boolean[] toBooleanArray(){
		//return new boolean[10];
		boolean[] boolArray = new boolean[1024];
		byte[] characters = new byte[1024];
		ArrayList queue = new ArrayList();
		
		queue.add(this);
		boolArray[0] = false;
		int arraypos = 0;
		int position = 0;
		int characterpos = 0;
		while (position < queue.size()) {
			boolArray[arraypos] = queue.get(position).isLeaf();
			if (queue.get(position).isLeaf()) {
				characters[characterpos] = queue.get(position).getCharacter();
				characterpos++;
			}
			else {
				queue.add(queue.get(position).getLeft());
				queue.add(queue.get(position).getRight());
			}
			position++;
			arraypos++;
		}
		
		boolean[] ret = new boolean[arraypos + 8 * characterpos];
		for (int i = 0; i < arraypos; i++) {
			ret[i] = boolArray[i];
		}
		for (int i = 0; i < characterpos; i++) {
			byte characterByte = characters[i];
			boolean[] bits = BitByteConverter.byteToBooleanArray(characterByte);
			for (int j = 0; j < 8; j++) {
				ret[arraypos + (8 * i) + j] = bits[j];
			}
		}
		
		return ret;
	}
	
	
	
	public boolean[][] toArrayList() {
		boolean[][] ret = new boolean[256][];
		ArrayList queue = new ArrayList();
		ArrayList binary = new ArrayList();
		
		//System.out.println("ret.size() = " + ret.size());
		
		queue.add(this);
		binary.add(new boolean[0]);
		
		int position = 0;
		while (position < queue.size()) {
			//int toAdd = queue.get(position).getItem();
			boolean isLeaf = queue.get(position).isLeaf();
			if (!isLeaf) {
				boolean[] binaryString = binary.get(position);
				boolean[] binaryLeft = new boolean[binaryString.length + 1];
				boolean[] binaryRight = new boolean[binaryString.length + 1];
				for (int i = 0; i < binaryString.length; i++) {
					binaryLeft[i] = binaryString[i];
					binaryRight[i] = binaryString[i];
				}
				binaryLeft[binaryLeft.length - 1] = false;
				binaryRight[binaryRight.length - 1] = true;
				
				queue.add(queue.get(position).getLeft());
				binary.add(binaryLeft);
				queue.add(queue.get(position).getRight());
				binary.add(binaryRight);
			}
			else {
				int toAdd = (int) queue.get(position).getCharacter() + 128;
				ret[toAdd] = binary.get(position);
			}
			position++;
		}
		
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy