Convert a user-defined tree into a binary tree

Source: Internet
Author: User

Convert a user-defined tree into a binary tree

This is a good evening. I haven't felt this for a long time. It takes me some time to get up and work. I have never been so determined to write code for myself, I was surprised to help her write well. Haha... This evening, I also helped her write the West mail navigation and couldn't sleep. So I knocked on the code for converting the Harman tree into a binary tree. In fact, it's really hard to understand it. I defined F as a second-level pointer, use it to point to the node address. It is easy to create. input data and the weight value weight, and then set its left and right to NULL;

After initialization, let's start to create a binary tree. I define two variables s1 and s2. s1 always marks the smallest node weight in the F pointer array, and s2 is small, note that each time I initialize s1 and s2, they are stored in F [I]! = NULL node, here I used the method I used a few times, using the for NULL statement loop to find s1 and s2, for how to find the minimum S2. I have already said, needless to say again;

The key steps here should be how to establish a binary tree. If I have already found S1. The following figure shows the explanation:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + IDwvcD4KPHA + ICAgICAgICC0tL2os/batl7nysfv410820fnx08hlo6y + fingerprint + dfTo6w8L3A + fingerprint = "http://www.2cto.com/uploadfile/Collfiles/20141218/20141218084906161.png" alt = "\">

After N-n-1 cycles, the binary tree is successfully created and directly enters the code;

# Include
 
  
# Include
  
   
# Include
   
    
# 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
    
     
Data, F [k1]-> weight); for (k2 = k1 + 1; k2
     
      
Data, F [k2]-> weight); for (j = k2; j
      
        Weight
       
         Weight) {k2 = k1; k1 = j;} else if (F [j]-> weight
        
          Weight & F [k2]) {k2 = j ;}} printf ("output minimum 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 ("Enter the number of nodes:"); scanf ("% d", & n ); F = (HuffNode **) malloc (n * sizeof (HuffNode *); for (I = 0; I
         
           Data, & F [I]-> weight); F [I]-> left = F [I]-> right = null;} root = CreateHuffman (F, n ); printf ("\ n first-order traversal of Binary Tree: \ n"); Print (root); printf ("\ n traversal of leaf nodes: \ n"); leafprint (root ); return 0 ;}
         
        
       
      
     
    
   
  
 

You can traverse the binary tree in ascending, central, and descending order and run the results at will.

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.