Java Applet Huffman tree and file compression and decompression (i) Huffman tree Construction Chapter __ Static function

Source: Internet
Author: User
Tags numeric value

Java applet's Huffman tree and file compression and decompression (i) Huffman Tree Construction Chapter

Before we get to know Huffman tree, let's look at the relevant knowledge of the tree first.
I. Knowledge of tree in data structureThe data structure is the way that the computer stores and organizes it. Data structures are collections of elements that have one or more specific relationships between each other. Typically, carefully chosen data structures can lead to higher operational or storage efficiencies. Data structures are often associated with efficient retrieval algorithms and indexing techniques. The data structure mainly includes the collection, the linear structure, the tree row structure and the diagram row structure;
This time, the main look at the tree-shaped structure:
1, tree definition: Tree: N (n>=1) a finite node composed of a hierarchical relationship of the set 2, the relevant terms of the tree
Node Degree: A node contains the number of subtrees called the degree of the node
Degree of the tree: the degree of the largest node in a tree
Leaf node: node with zero degrees
Father node: If a node contains child nodes, the current node is the parent node of the child node
Child node: A node contains a subtree, the root node of the subtree is a child node of the modified node
Sibling node: child nodes with the same Father node are siblings of each other
Node hierarchy: From the root start defined, the root is the 1th layer, the root of the child node is the 2nd layer, and so on
Depth (height) of a tree: the largest level of nodes in a tree
Other: Cousins nodes, descendants, forests:
3, the tree classification
Unordered tree: The Order of nodes in the tree is irregular
Ordered tree: Refers to the tree in the same layer node from left to right in order, such a tree is called an ordered tree.
1 binary Tree: Each node contains up to two subtrees of the subtree is called a binary tree
2 Non-binary tree: All trees that are not binary trees belong to a non binary tree 4, binary tree definition: Two fork tree is a special tree-shaped structure, each node at most only two subtrees, and the subtree has a left and right, its order can not be reversed, is an ordered tree.
Note: The two-fork tree consists of a root node and two Saozi right subtrees.
Binary Tree classification
Full two fork tree: for the above complete binary tree, if you remove all the nodes of the D layer, then the remaining part of a full two fork tree
Complete binary tree: For a binary tree, assume its depth is D (d>1). In addition to layer D, the number of nodes in each of the other layers has reached the maximum, and all the nodes in layer D are continuously aligned from left to right, such two-fork trees are called complete binary trees;
5, Complete binary tree properties
The level of the Jogen node is 1, then the two-forked tree layer I has a maximum of 2 (i-1) secondary nodes.
In a two-fork tree with a height of k, a maximum of 2k-1 nodes (k≥0)
Set a binary tree the number of nodes is n, the number of the parent node is N/2.
A complete binary tree with n nodes and a node with ordinal I (0≤i<n):
If the i=0, then I is the root node, no parents node, if I >0, then I of the left and right sub node number is:
If 2i+1<n, then I of the left child node serial number is 2i+1; otherwise I have no left child.
If 2i+2<n, then I of the right child node serial number is 2i+2; otherwise I have no right child.

6, the Huffman tree given n weight as n leaf node, the construction of a binary tree, if the right path length to achieve the smallest, that such a two-fork tree for the best binary tree, also known as Huffman Tree (Huffman trees). Huffman tree is a tree with the shortest length of weighted path, and the node with larger weights is nearer to the root.
The term for Huffman tree path Length:Node path length: The path length of a tree from one node (the root node) to the path of another node: the sum of the path lengths of all nodes of a tree Weight value:Refers to the date field of all leaf nodes, which holds a numeric value that represents the current leaf node's data, which is the weight value. weighted path length:The sum of the length of the weighted path of all leaf nodes optimal binary tree (Haverman):1, through the ownership value node, the formation of a binary tree, the final binary tree with the right path is the smallest, then this binary tree is Huffman tree (the best binary tree) 2, the structure of more than one tree, the same layer of nodes, can be replaced around

The idea of constructing Huffman tree:

1. Create a new node class

2. Package the data into a node and put it in a container for building a forest

3, the cycle to find the smallest weight of two nodes, used to construct a new node

You need to define a method to find where the current node is inserted into the forest.

Get Huffman code (for later file compression and decompression)


Two or two-fork tree construction Source code:

Node class:

Package mytree;

public class Node {
	//node class, public
	int data;
	Public Node left;
	public Node right;



The construction process of binary tree and traversal:
Package mytree;

Import java.util.ArrayList;
	public class Treelist {public int [] values ={1,2,3,4,5,6,7,8,9};
	
	Public arraylist<node> list = new arraylist<node> ();
		public static void Main (string[] args) {treelist tree = new Treelist ();
		Node Root=tree.createtree ();
		System.out.println ("Pre-sequence Traversal:");
		Tree.looktree1 (root);
		SYSTEM.OUT.PRINTLN ("Sequence Traversal:");
		Tree.looktree2 (root);
		SYSTEM.OUT.PRINTLN ("Subsequent traversal:");
	Tree.looktree3 (root);
			Public node Createtree () {//Encapsulated node for (int i = 0; i < values.length; i++) {node node = new node ();
			Node.data=values[i];
		List.add (node);
			}//Construct two-fork tree for (int i = 0; i < list.size ()/2-1; i++) {node node =list.get (i);
			Node.left=list.get (2*i+1);
		Node.right=list.get (2*i+2);
		}//constructs the last parent node lastnode = List.get (List.size ()/2-1);
		Lastnode.left=list.get ((List.size ()/2-1) *2+1);
		if (List.size ()%2==1) {Lastnode.right=list.get (List.size ()/2-1) *2+2);
	Return List.get (0); }//Pre-sequence traversal public void LookTree1 (Node root) {System.out.print (root.data+ "");
		if (root.left!=null) {lookTree1 (root.left);
		} if (Root.right!=null) {lookTree1 (root.right);
		The In//sequence traverses the public void lookTree2 (Node root) {if (root.left!=null) {lookTree2 (root.left);
		} System.out.print (root.data+ "");
		if (root.right!=null) {lookTree2 (root.right);
		}//Subsequent traversal of the public void lookTree3 (Node root) {if (root.left!=null) {lookTree3 (root.left);
		} if (Root.right!=null) {lookTree3 (root.right);
	} System.out.print (root.data+ "");
 }

}



Third, the Construction Huffman Tree source code: Huffman Node Class:

Package COM.BLUESKY.HUFFM;

public class Huffmnode {
	//Constructs Huffmnode class
	private int data;
	Private Huffmnode left;
	Private Huffmnode right;
	Huffmnode class Constructor public  huffmnode (int data) {
		this.data=data;
	}
	
	Package attribute Public
	int GetData () {return
		data;
	}
	public void SetData (int data) {
		this.data = data;
	}
	Public Huffmnode GetLeft () {return left
		;
	}
	public void Setleft (Huffmnode left) {
		this.left = left;
	}
	Public Huffmnode GetRight () {return right
		;
	}
	public void Setright (Huffmnode right) {
		this.right = right;
	}

}

The construction of Huffman Tree:
Package COM.BLUESKY.HUFFM;

Import java.util.LinkedList;
	public class Huffmtree {public int [] Datas ={2,1,6,4,7,9,12,3};
	
	Public linkedlist 

Program Entry:
Package COM.BLUESKY.HUFFM;

public class Test {public
	
	static void Main (string[] args) {
		Huffmtree tree = new Huffmtree ();
		Huffmnode root = Tree.createtree ();
		Tree.gethuffmcode (Root, "");
	}

}

Four, Summary: The tree belongs to the knowledge of the data structure, before oneself learning data structures, when the teacher asked himself to construct some such as linked lists, queues and other data structures, and to be able to achieve these data structure of additions, deletions and other functions. The list and the queue is also simple, the tree and graph is really a bit, in fact, the data structure of this course is very important, this is not, we will soon see the usefulness of Huffman tree; The recursive traversal of the tree is really a bit difficult to understand, do not know where the recursion to go; the tangle will fall into the cycle of death, sometimes not so tangled, You pass down, the head is immediately blindfolded, of course, some logic is very clear people will not, can only say, I also need experience. Huffman tree, also known as the best binary tree, is the tree structure of the more important knowledge, previously only know that it is useful, but do not know how to use it, is now a witness to the power of Huffman tree. In the process of constructing the Havermann tree, it should be noted that after the child node is removed, the second byte point becomes the first node, which should be noted here. Share.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.