• 哈夫曼树课设代码


    // 可设2013.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <limits.h>
    struct node{
       char t;
       int value;
    
       struct node *right,*left;
    };
    struct node *A[100];
    int len,heapsize;
    struct node *newnode()
    {
       struct node *p = (node*)malloc(sizeof(node));
       p->value = 0 ;
       p->left  = p->right = NULL;
       return p;
    };
    int RIGHT(int i )
    {
       return i*2+1; 
    }
    int LEFT(int i)
    {
      return i*2;
    }
    int PARENT(int i)
    {
        return i/2;
    }
    void MIN_HEAPIFY(int i )
    {
      int l = LEFT(i);
      int r = RIGHT(i);
      int lest;
      if(l <= heapsize && A[l]->value < A[i]->value)
         lest = l ;
      else lest = i;
      if(r <= heapsize && A[r]->value < A[lest]->value )
          lest = r;
      if(i != lest)
      {
        struct node *temp;
        temp = A[i];
        A[i] = A[lest];
        A[lest] = temp;
        MIN_HEAPIFY(lest);
      }
    }
    
    void BUILD_MAX_HEAP()
    {
       heapsize = len;
       for(int i = len/2 ;i >= 1; i --)
         MIN_HEAPIFY(i);
    }
    void HEAP_EXTRACT_MIN()
    {
       struct node *max ;
       max = A[1];
       A[1] = A[heapsize];
       A[heapsize] = max;
       heapsize = heapsize -1;
       MIN_HEAPIFY(1);
    }
    void HEAP_CHANGE_KEY(int i)
    {
       while(i > 1 && A[PARENT(i)]->value > A[i]->value)
       {
          struct node *temp = A[i];
          A[i] = A[PARENT(i)];
          A[PARENT(i)] = temp;
          i = PARENT(i);
       }
    }
    char ans[100] = {0};
    void dotree(int i,struct node *p)
    {
         if(p->left != NULL)
         {
            ans[i] = '0';
            dotree(i+1,p->left);
            ans[i] = '';
            ans[i] = '1';
            dotree(i+1,p->right);
            ans[i] = '';
         }
         else
            printf("%c %s
    ",p->t,ans);
    
    }
    int main()
    {
          int n ;
          printf("请输入字符个数:");
          scanf("%d",&n);
          printf("请输入字符:");
          for(int i = 1 ;i <= n;i ++)
          {
              getchar();
              A[i] = newnode();
              scanf("%c",&A[i]->t);
            
          }
          printf("请输入字符对应的权值:");
          for(int i = 1 ;i <= n;i ++)
          {
            scanf("%d",&A[i]->value);
          }
          len = n;
          BUILD_MAX_HEAP();
          while(heapsize >= 2)
          {
            struct node *p = newnode();
            HEAP_EXTRACT_MIN();
           
            p->left = A[heapsize+1];
            p->value += A[heapsize+1]->value;
       
            HEAP_EXTRACT_MIN();
            p->right = A[heapsize+1];
            p->value += A[heapsize+1]->value;
        
            heapsize = heapsize + 1;
            A[heapsize] = p;
            HEAP_CHANGE_KEY(heapsize);        
          }
         dotree(0,A[1]);      
          
       return 0 ;
    }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    jdk动态代理底层实现
    spring-FactoryBean
    大型网站技术架构 核心原理与案例分析 pdf
    实战JAVA虚拟机 JVM故障诊断与性能优化 pdf
    《实战Java高并发程序设计》pdf
    mysql-注意点
    Json入门
    inflate, findViewById与setContentView的区别与联系
    提高编程能力的7条建议
    JDBC之一:JDBC快速入门
  • 原文地址:https://www.cnblogs.com/zyue/p/3156031.html
Copyright © 2020-2023  润新知