. 1 #include " stdio.h " 2 #include " stdlib.h " . 3 // define Huffman node type . 4 typedef struct Node { . 5 int weight; // weight . 6 struct Node * left; // left child . 7 struct right * node; // right child . 8 struct node * next; // next node . 9 } Huffman; 10 // initialize Huffman . 11 Huffman * creatHuffman () 12 is { 13 is int n-; 14 Huffman * H; 15 Huffman * P; 16 Huffman * Q; . 17 H = Q = (Huffman *) the malloc ( the sizeof (Huffman)); 18 is the printf ( " Please enter your need several nodes: " ) ; . 19 Scanf ( " % D " , & n-); 20 is IF (n-<= 0 ) 21 is { 22 is the printf ( " you entered value is not correct! " ); 23 is returnNULL; 24 } 25 the else 26 is { 27 for ( int I = 0 ; I <n-; I ++ ) 28 { 29 P = (Huffman *) the malloc ( the sizeof (Huffman)); 30 the printf ( " Please enter% d node value: " , I + . 1 ); 31 is Scanf ( " % D " , & p-> weight); 32 p-> left = NULL; 33 is p-> right = NULL; 34 is Q-> Next = P; 35 } 36 p-> Next = NULL; 37 [ // returns the next node h, h is the head of the linked list node Huffman node, where the node has the value returns directly on it ; 38 is return H-> Next; 39 } 40 } 41 is // delete the node 42 is void delNode (Huffman H *, int index) 43 is { 44 is Huffman * P = H; 45 Huffman * Q; 46 is IF (index = 0 ) 47 { 48 = H- H> Next; 49 return ; 50 } 51 is for ( int I = 0 ; I <index - . 1 ; I ++) // move to the node to be removed before a 52 is { 53 is P = p-> Next; 54 is } 55 q = p; // q-p to point to the previous node 56 is p = p-> Next; // p to the mobile node to remove 57 is Q-> = p-Next> Next; // list element in the elimination 58 } 59 // merge two nodes, as a new node, and the two nodes as children of about 60 void mergeNode (Huffman * H, * MIN1 Huffman, Huffman * MIN2) 61 is { 62 is Huffman the newNode * = (Huffman *) the malloc ( the sizeof (Huffman)); // Create a new node 63 is Huffman * P = H; 64 newnode- > weight = min1-> + min2- weight> weight; // weights assigned to merge two nodes merged node 65 newnode-> left = MIN1; 66 newnode-> right = MIN2; 67 the while (p-> Next ! = NULL) 68 { 69 P = p-> Next; 70 } 71 is p-> Next = the newNode; // new node into the list of the last node in order to compare the size of the back 72 newnode-> Next = NULL; 73 is } 74 // compare the size, find the smallest value of two node 75 void compareNode (Huffman * H) 76 { 77 // exit recursive conditions, when only the last exit node list node (root) 78 iF (H-> Next == NULL) 79 { 80 return ; 81 } 82 int index = 0 ; // mark position of the minimum 83 int COUNT = 0 ; // mark the current node in the first few 84 Huffman MIN1 *, * MIN2; 85 Huffman * P = H; 86 MIN1 = H; 87 P = H-> Next; 88 // Find the minimum value 89 the while (P =! NULL ) 90 { 91 is COUNT ++ ; 92 IF (p-> weight <min1-> weight) 93 { 94 MIN1 = P; 95 index = COUNT; 96 } 97 P = p->Next; 98 } 99 delNode (H, index); // call the function to delete the deletion node in the linked list node 100 COUNT = 0 ; // node re-start 101 P = H-> Next; 102 MIN2 = H; 103 // looking for a second minimum value 104 the while (P =! NULL) 105 { 106 COUNT ++ ; 107 IF (p-> weight <min2-> weight) 108 { 109 MIN2 = P; 110 index = COUNT; 111 } 112 P = p-> Next; 113 } 114 delNode (H, index); // call the function to delete the deletion node in the node list 115 compareNode (H); // call itself recursively 116 } 117 // output binary 1 18 void PrintTree (Huffman * H) 119 { 120 the printf ( " % D " , H-> weight); 121 PrintTree (H-> left); 122 PrintTree (H-> right); 123 } 124 main () 125 { 126 Huffman *h; 127 h = creatHuffman(); 128 compareNode(h); 129 printTree(h); 130 }