+
+struct hufftree
+{
+ struct hufftree*left;//0
+ struct hufftree*right;//1
+ int index;
+};
+
+struct hufftree * rle_tree;
+struct hufftree * mcbpc_intra_tree;
+struct hufftree * mcbpc_inter_tree;
+struct hufftree * cbpy_tree;
+struct hufftree * mvd_tree;
+
+static void insert(struct hufftree*tree, char*code, int index)
+{
+ if(!*code) {
+ assert(!tree->left); //shannon conditional
+ assert(!tree->right);
+ tree->left = 0;
+ tree->right = 0;
+ tree->index = index;
+ return;
+ }
+ if(code[0] == '0') {
+ if(!tree->left) {
+ tree->left = (struct hufftree*)malloc(sizeof(struct hufftree));
+ memset(tree->left, 0, sizeof(struct hufftree));
+ tree->left->index = -1;
+ }
+ insert(tree->left, code+1, index);
+ return;
+ } else {
+ assert(code[0] == '1');
+ if(!tree->right) {
+ tree->right = (struct hufftree*)malloc(sizeof(struct hufftree));
+ memset(tree->right, 0, sizeof(struct hufftree));
+ tree->right->index = -1;
+ }
+ insert(tree->right, code+1, index);
+ return;
+ }
+}
+
+struct hufftree* huffcode2tree(struct huffcode*code)
+{
+ struct hufftree* t = malloc(sizeof(struct hufftree));
+ memset(t, 0, sizeof(struct hufftree));
+ t->index = -1;
+ while(code->code) {
+ insert(t, code->code, code->index);
+ code++;
+ }
+ return t;
+}
+