The construction of Huffman tree implemented in Java

Source: Internet
Author: User

Huffman tree content does not explain here, please search yourself. The following gives the Java implementation of the Huffman tree construction process.

Node class:

 1./** 2. * Binary tree Node 3. */4.public class node implements comparable {5.
6. private int value;    7.8.
Private Node leftchild;    9.10.
Private Node Rightchild;    11.12.        public Node (int value) {13.
This.value = value;
14.} 15.        public int GetValue () {17.
return value;
18.} 19.        public void SetValue (int value) {21.
This.value = value;
22.} 23.        Public Node Getleftchild () {25.
return leftchild;
26.} 27.        public void Setleftchild (Node leftchild) {29.
This.leftchild = Leftchild;
30.} 31.        Public Node Getrightchild () {33.
return rightchild;
34.} 35.        public void Setrightchild (Node rightchild) {37.
This.rightchild = Rightchild;
38.} 39.        Public String toString (int level) {41.
String indent = "";            for (int i = 0; I < level; i++) {43.
Indent + = "";
44.} 45.                Return indent + value + "\ n" + 47.(Leftchild!= null leftchild.tostring (Level + 1): "") + 48.
(Rightchild!= null rightchild.tostring (Level + 1): "");
49.} 50.        A. public int compareTo (Object o) {52.
node that = (node) o;
Double result = This.value-that.value; Return result > 0? 1:result = 0?
0:-1; .} 

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.