Turn Huffman tree into a binary tree

Source: Internet
Author: User
Tags define null

Feel good tonight, a long time without this feeling, get up to need a bit of explosive power, do things also need a bit of motivation, to oneself have not been so big determined to write code, help her but write very good, I myself are surprised. Hahaha ... Tonight is also to help her write the West mail navigation can not sleep, then knocked the Huffman tree into a binary tree code, in fact, understand really not difficult, I define F for a two-level pointer, with it point to the address of the node, create a very simple, input data and weights weight, and then set its left and right to null;

When the initialization is over, start creating a two-fork tree, I define two variables S1 and s2,s1 are always labeled F, which is the smallest of the node weights in the pointer array, S2 is minor, special attention, S1 and S2 each time I initialize is placed on the F[i]!=null node, Here used the method I used a few times, using a for empty statement loop find S1 and S2, for how to let find S1 the smallest S2 times the ego has said, again needless to say;

The key steps in this should be how to set up a two-fork tree, if I have found a small S1 and small S2, the following figure is good explanation:

This is what it was created to be, once created to look like this,

After the loop n-1, the binary tree is created and directly enters the code;

#include <iostream> #include <stdlib.h> #include <conio.h> #define NULL nulltypedef struct huffnode{ Char data;int weight;struct huffnode *left,*right;} Huffnode; Huffnode *createhuffman (Huffnode **,int); void preorder (Huffnode *); void Leafprint (Huffnode *); void Print (Huffnode *);    Huffnode *createhuffman (Huffnode **f,int N) {Huffnode *p;    int i,j;    int k1=0,k2=1;  for (i=0;i<n-1;i++) {for (k1=0;k1<n&&f[k1]==null;k1++);   printf ("Data%c, Weight value%d\n", f[k1]->data,f[k1]->weight);  for (k2=k1+1;k2<n&&f[k2]==null;k2++);   printf ("Data%c, Weight value%d\n", f[k2]->data,f[k2]->weight);       for (j=k2;j<n;j++) {if (F[j]) {if (f[j]->weight<f[k1]->weight) {k2=k1;       K1=j;   } else if (F[j]->weight<f[k2]->weight&&f[k2]) {k2=j;      }}} printf ("Output min k1=%d k2=%d\n", K1,K2);   p= (Huffnode *) malloc (sizeof (Huffnode));   p->weight=f[k1]->weight+f[k2]->weight;   P->data= ' W '; P->left=f[K1];   p->right=f[k2];   F[k1]=p;      F[k2]=null;    Print (F[k1]); }return F[K1];} void Print (Huffnode *root) {if (root) {printf ("Data%c, Weight%d\n", root->data,root->weight); Print (Root->left); Print (Root->right);}} void Leafprint (Huffnode *root) {if (root) {if (root->left==null&&root->right==null) printf ("Data%c, Weight%d\n ", root->data,root->weight); Leafprint (Root->left); Leafprint (root->right);}} int main (int argc, char** argv) {    huffnode **f;    Huffnode *root;    int n,i;    printf ("Please enter the number of nodes:");    scanf ("%d", &n);    f= ( Huffnode * *) malloc (n*sizeof (Huffnode *));    for (i=0;i<n;i++) {      Fflush (stdin);       f[i]= (Huffnode *) malloc (sizeof (Huffnode));       printf ("\ n enter data for%d nodes, weights:", i+1);      scanf ("%c%d", &f[i]->data, &f[i]->weighT);      f[i]->left=f[i]->right=null;          }    Root=createhuffman (f,n);        printf ("N-Sequence traversal binary tree: \ n");     Print (Root);    printf ("\ n traverse leaf node: \ n");    Leafprint (root);      return 0;}

For how to traverse the binary tree First order, the middle sequence, the order can be, random run results

Turn Huffman tree into a binary tree

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.