c中的huffman树打印内存地址而不是值?

时间:2017-06-04 07:05:04

标签: c huffman-code

这是一个创建霍夫曼树的程序, 我的大三学生写的。

错误是什么?也许是打印内存地址。

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

struct link
{
    int data;
    struct link *next, *left, *right;
};
typedef struct link node;

node *curr = NULL, *head = NULL, *nnode = NULL;

void createheap();

void main()
{
    node* i;
    int k1, k2;
    int ch;
    int x;

    printf("create node\nadd nodes\ntraverse node\nmake heap\n");
    do
    {
        printf("enter the choice\n");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
                nnode = (node*)malloc(sizeof(node));
                printf("enter number please\n");
                scanf("%d", &x);
                nnode->data = x;
                nnode->next = NULL;
                nnode->left = NULL;
                nnode->right = NULL;
                head = nnode;
                curr = nnode;
                break;
            case 2:
                nnode = (node*)malloc(sizeof(node));
                printf("enter number please\n");
                scanf("%d", &x);
                nnode->data = x;
                nnode->next = NULL;
                nnode->left = NULL;
                nnode->right = NULL;
                curr->next = nnode;
                curr = nnode;
                break;
            case 3:
                printf("traverse\n");
                for (i = head; i != NULL; i = i->next)
                {
                    printf("%d\n", i->data);
                }
                break;
            case 4:
                createheap();
                break;
            default:
                printf("this is wrong number\n");
        }
    } while (ch <= 5);
}
void createheap()
{
    node* l;

    node *t1, *t2, *t;
    node *np, *r1;
    int k1, k2, k3;
    node* i;
    // void traverse(node*);
    l = head;
    while (head->next != NULL)
    {

        l = head;
        t = l;
        nnode = (node*)malloc(sizeof(node));
        t1 = t->next;
        t2 = t->next->next;
        k1 = t->data;
        k2 = t1->data;
        k3 = k1 + k2;

        if (t2 == NULL)
        {
            nnode->left = t;
            nnode->right = t1;
            nnode->data = k3;

            nnode->next = NULL;
            curr = nnode;
            head = nnode;
        }
        else if (k3 <= t2->data)
        {
            nnode->next = t2;

            nnode->left = t;
            nnode->right = t1;
            nnode->data = k3;
            head = nnode;
        }
        else if (k3 > t2->data)
        {
            nnode->left = t;
            nnode->right = t1;
            nnode->data = k3;
            if (k3 <= curr->data)
            {
                for (i = t2; i != NULL; i = i->next)
                {
                    if (i->data >= k3)
                    {
                        break;
                    }
                    else
                    {
                        np = i;
                    }
                }
                r1 = np->next;
                nnode->next = r1;
                np->next = nnode;
            }
            else if (k3 > curr->data)
            {
                nnode->next = NULL;
                curr->next = nnode;

                curr = nnode;
            }
            head = t2;
        }

        free(t);
        free(t1);

        printf("l %d\n", nnode->left);
        printf("r %d\n", nnode->right);
        printf("d %d\n", nnode->data);
    }
    // traverse(l);
    // printf("\n%d",l->left->data);
    // printf("\n%d",l->right->left->data);*/
}
void traverse(node* t)
{
    if (t != NULL)
    {
        traverse(t->left);
        printf("\n%d\n", t->data);
        traverse(t->right);
    }
}

“创建节点”表示初始化列表并输入第一个值。

“添加节点”意味着添加第二个,第三个等等。值。 (请仅提供现在的排序值)

“make heap”表示从提供的列表中创建堆,AND从树的底部(当它生成时)打印left(l),right(r)和root值(d)。

所以,问题是root是打印正确的值。但是左右两边都是打印的.. 31298384,31298323。

0 个答案:

没有答案