• 哈夫曼树课设代码


    // 可设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
    没有梦想,何谈远方
  • 相关阅读:
    android 从服务器获取新闻数据并显示在客户端
    Java多线程系列之:线程间的通信
    Java多线程系列之:多线程一些基本概念
    Java多线程系列之:内存可见性
    计算机基础
    tomcat系列之六:Tomcat顶层组件
    tomcat系列之五:Tomcat各个组件生命周期
    tomcat系列之四:Tomcat架构(下)
    tomcat系列之三:Tomcat架构
    tomcat系列之二:Servlet规范
  • 原文地址:https://www.cnblogs.com/zyue/p/3156031.html
Copyright © 2020-2023  润新知