huffman樹,二叉樹實現。

#include<stdio.h>
#include<stdlib.h>


typedef char datatype;
typedef unsigned int uint;

typedef struct hftree
{
    datatype data;
    uint weight;
    hftree *left,*right,*parent,*treel,*treer;
}hftree;

typedef struct hfroot
{
    hftree * root=NULL;
    hftree * leftmost=NULL;
}hfroot;

void insert(hfroot * T,hftree *node)
{
    hftree *iter=T->root,*pre=NULL;
    uint isleft = 1;
    while(iter)
    {
        pre = iter;
        if(node->weight < iter->weight )
        {
            iter = iter->left;
        }
        else
        {
            isleft = 0;
            iter = iter->right;
        }
    }
    if(NULL != pre)
    {
        node->parent = pre;
        if(node->weight < pre->weight)
        {
            pre->left = node;
            if(isleft)
            {
                T->leftmost = node;
            }
        }
        else
        {
            pre->right = node;
        }
    }
    else
    {
        T->root = T->leftmost = node;
    }
}

void right_rotate(hfroot *T,hftree *node)
{
    hftree *left = node->left;
    T->leftmost = left;
    if(NULL == node->parent)
    {
        T->root = left;
    }
    else
    {
        node->left = NULL;
        left->parent = node->parent;
        node->parent = left;
    }
}

hftree * createNode(datatype data,uint weight)
{
    hftree * node;
    node = (hftree*)malloc(sizeof(hftree));
    node->parent=node->left=node->right=node->treel =node->treer = NULL;
    node->data = data;
    node->weight = weight;
    return node;
}

void createhf(hfroot *T)
{
    hftree *iter=T->leftmost,*min1,*min2,*pre,*node;
    if(NULL == T || NULL == T->root)
    {
        return;
    }
    while(NULL != iter->parent || NULL != iter->right)
    {
        if(NULL == iter->right)
        {
            right_rotate(T,iter->parent);
        }
        min1 = iter;
        pre = min2 = iter->right;
        while(min2)
        {
            pre = min2;
            min2 = min2->left;
        }
        min2 = pre;
        node = (hftree*)malloc(sizeof(hftree));
        node->parent=node->left=node->right=NULL;
        node->data = -1;
        node->weight = min1->weight + min2->weight;
        node->treel = min1;
        node->treer = min2;

        if(NULL != min1->parent)
        {
            if(min2 == min1->right)
            {
                T->leftmost = min1->parent->left = min2->right;
                if(NULL != min2->right)
                {
                    min2->right->parent = min1->parent;
                }
                else
                {
                    T->leftmost = min1->parent;
                }
            }
            else
            {
                T->leftmost = min1->parent->left = min1->right;
                min1->right->parent = min1->parent;
                min2->parent->left = min2->right;
                min2->right->parent = min2->parent;
            }
        }
        else
        {
            if(min2 == min1->right)
            {
                T->leftmost = T->root = min2->right;
                if(NULL != min2->right)
                {
                    min2->right->parent = NULL;
                }
                else
                {
                    T->leftmost = min1->parent;
                }
            }
            else
            {
                T->leftmost = T->root = min1->right;
                min1->right->parent = NULL;
                min2->parent->left = min2->right;
                min2->right->parent = min2->parent;
            }
        }
        min1->parent = min2->parent = node;
        insert(T,node);
        iter = T->leftmost;
    }
}
void visit(hftree * tree)
{
    if(tree)
    {
        // visit(tree->left);
        visit(tree->treel);
        printf("%c %d\n",tree->data,tree->weight);
        // visit(tree->right);
        visit(tree->treer);
    }
}

int main()
{
    hfroot T={NULL,NULL};
    insert(&T,createNode('a',10));
    insert(&T,createNode('b',5));
    insert(&T,createNode('c',9));
    // insert(&T,createNode('a',1));
    // insert(&T,createNode('b',1));
    // insert(&T,createNode('c',1));
    // insert(&T,createNode('d',1));

    createhf(&T);
    visit(T.root);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章