C ++ implements the optimal binary tree of Huffman

Source: Internet
Author: User

The optimal Huffman Binary Tree plays an important role in compression encoding.

This article uses C ++ to implement the Huffman binary tree for learning reference.

[Cpp]
/* Huffman tree-optimal binary tree */
# Include <iostream>
# Include <vector>
# Include <algorithm>
 
Using namespace std;
 
// Define the node struct type
Typedef struct Node {
Int val;
Node * left, * right, * parent; // left and right nodes and parent Node pointer
Void set (int value, Node * lft, Node * rgt ){
Val = value;
Left = lft;
Right = rgt;
Parent = NULL;
};
Node (){
Val =-1;
Left = right = parent = NULL;
}
} Node;
 
// Store the node Vector
Vector <Node> nodes;
// Store the node number vector
Vector <int> pnodes;
// Initialize the minimum weighted path and
Int sum = 0;
// Sort the numbers by the value of the node corresponding to the node number
Bool sortpnode (int a, int B)
{
Return nodes [a]. val <nodes [B]. val;
}
 
// Recursively traverse the huffman tree for output
Void output (Node * tree)
{
Node * ptr;
// Exit if the current node is empty
If (tree = NULL)
Return;
// Otherwise, output the value of the current Root Node
Cout <tree-> val <"-> Left:" <(tree-> left? Tree-> left-> val:-1) <", Right:" <(tree-> right? Tree-> right-> val:-1) <endl;
// If the left child is not empty, recursively traverse the left subtree
If (tree-> left)
Output (tree-> left );
// If the left child has been traversed and the right subtree is not empty, recursively traverse the right subtree
If (tree-> right)
Output (tree-> right );
// If the left and right children of the current node are empty, it indicates that the node is a leaf node.
// Calculate its weighted path and sum
If (! Tree-> left &&! Tree-> right)
{
// Calculate the length to the root node
Int level = 0;
Ptr = tree-> parent;
While (ptr! = NULL)
{
Level ++;
Ptr = ptr-> parent;
}
// Multiply by the leaf depth
Sum + = level * tree-> val;
Cout <"depth:" <level <", node value:" <tree-> val <", weighted path and = "<sum <endl;
}
}
 
Int main ()
{
Int values [] = {1, 2, 3, 4, 5, 30, 5, 8 },
N = sizeof (values)/sizeof (int );
Int cnt = n;
Node * tnode;
// Put (2 + n) * n/2 directly in nodes
Nodes. resize (2 * n-1 );
// Set n elements for pnodes, and each element-that is, the node number is-1
Pnodes. resize (n,-1 );
// Assign values to nodes
For (int I = 0; I <n; I ++)
{
Nodes [I]. val = values [I];
Pnodes [I] = I;
}
// Loop
While (pnodes. size ()> 1)
{
// Sort the node numbers by the value of the object Node
Sort (pnodes. begin (), pnodes. end (), sortpnode );
// 0 -- n-1 indicates the processed node, and cnt number indicates the node to be processed.
Tnode = & nodes [cnt];
// Assign values to the derived node and specify the left and right children and parent nodes
Tnode-> val = nodes [pnodes [0]. val + nodes [pnodes [1]. val;
Tnode-> left = & nodes [pnodes [0];
Tnode-> right = & nodes [pnodes [1];
Tnode-> parent = NULL;
// Add the derived node number to the pnodes Vector
Pnodes. insert (pnodes. begin (), cnt );
// Set the parent node address of the child node
Nodes [pnodes [1]. parent = & nodes [cnt];
Nodes [pnodes [2]. parent = & nodes [cnt];
// Delete the processed node number from pnodes
Pnodes. erase (pnodes. begin () + 1 );
Pnodes. erase (pnodes. begin () + 1 );
// Set the number of the next derived Node
Cnt ++;
}
// Nodes [cnt-1] is the root node of the tree
// Output tree
Output (& nodes [cnt-1]);
// Output the minimum length of the weighted path of the huffman tree
Cout <"minimum weighted path length:" <sum <endl;
Return 0;
}

/* Huffman tree-optimal binary tree */
# Include <iostream>
# Include <vector>
# Include <algorithm>

Using namespace std;

// Define the node struct type
Typedef struct Node {
Int val;
Node * left, * right, * parent; // left and right nodes and parent Node pointer
Void set (int value, Node * lft, Node * rgt ){
Val = value;
Left = lft;
Right = rgt;
Parent = NULL;
};
Node (){
Val =-1;
Left = right = parent = NULL;
}
} Node;

// Store the node Vector
Vector <Node> nodes;
// Store the node number vector
Vector <int> pnodes;
// Initialize the minimum weighted path and
Int sum = 0;
// Sort the numbers by the value of the node corresponding to the node number
Bool sortpnode (int a, int B)
{
Return nodes [a]. val <nodes [B]. val;
}

// Recursively traverse the huffman tree for output
Void output (Node * tree)
{
Node * ptr;
// Exit if the current node is empty
If (tree = NULL)
Return;
// Otherwise, output the value of the current Root Node
Cout <tree-> val <"-> Left:" <(tree-> left? Tree-> left-> val:-1) <", Right:" <(tree-> right? Tree-> right-> val:-1) <endl;
// If the left child is not empty, recursively traverse the left subtree
If (tree-> left)
Output (tree-> left );
// If the left child has been traversed and the right subtree is not empty, recursively traverse the right subtree
If (tree-> right)
Output (tree-> right );
// If the left and right children of the current node are empty, it indicates that the node is a leaf node.
// Calculate its weighted path and sum
If (! Tree-> left &&! Tree-> right)
{
// Calculate the length to the root node
Int level = 0;
Ptr = tree-> parent;
While (ptr! = NULL)
{
Level ++;
Ptr = ptr-> parent;
}
// Multiply by the leaf depth
Sum + = level * tree-> val;
Cout <"depth:" <level <", node value:" <tree-> val <", weighted path and = "<sum <endl;
}
}

Int main ()
{
Int values [] = {1, 2, 3, 4, 5, 30, 5, 8 },
N = sizeof (values)/sizeof (int );
Int cnt = n;
Node * tnode;
// Put (2 + n) * n/2 directly in nodes
Nodes. resize (2 * n-1 );
// Set n elements for pnodes, and each element-that is, the node number is-1
Pnodes. resize (n,-1 );
// Assign values to nodes
For (int I = 0; I <n; I ++)
{
Nodes [I]. val = values [I];
Pnodes [I] = I;
}
// Loop
While (pnodes. size ()> 1)
{
// Sort the node numbers by the value of the object Node
Sort (pnodes. begin (), pnodes. end (), sortpnode );
// 0 -- n-1 indicates the processed node, and cnt number indicates the node to be processed.
Tnode = & nodes [cnt];
// Assign values to the derived node and specify the left and right children and parent nodes
Tnode-> val = nodes [pnodes [0]. val + nodes [pnodes [1]. val;
Tnode-> left = & nodes [pnodes [0];
Tnode-> right = & nodes [pnodes [1];
Tnode-> parent = NULL;
// Add the derived node number to the pnodes Vector
Pnodes. insert (pnodes. begin (), cnt );
// Set the parent node address of the child node
Nodes [pnodes [1]. parent = & nodes [cnt];
Nodes [pnodes [2]. parent = & nodes [cnt];
// Delete the processed node number from pnodes
Pnodes. erase (pnodes. begin () + 1 );
Pnodes. erase (pnodes. begin () + 1 );
// Set the number of the next derived Node
Cnt ++;
}
// Nodes [cnt-1] is the root node of the tree
// Output tree
Output (& nodes [cnt-1]);
// Output the minimum length of the weighted path of the huffman tree
Cout <"minimum weighted path length:" <sum <endl;
Return 0;
}


 

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.