Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我不明白为什么我的c++;当我没有将heapify函数中的值赋给变量时,huffman树的代码正在工作 #包括 #包括 使用名称空间std; float AverageCodeLength=0; //包含输入符号的节点的结构及其相应概率 结_C++_Huffman Code_Min Heap - Fatal编程技术网

C++ 我不明白为什么我的c++;当我没有将heapify函数中的值赋给变量时,huffman树的代码正在工作 #包括 #包括 使用名称空间std; float AverageCodeLength=0; //包含输入符号的节点的结构及其相应概率 结

C++ 我不明白为什么我的c++;当我没有将heapify函数中的值赋给变量时,huffman树的代码正在工作 #包括 #包括 使用名称空间std; float AverageCodeLength=0; //包含输入符号的节点的结构及其相应概率 结,c++,huffman-code,min-heap,C++,Huffman Code,Min Heap,我不明白为什么我的c++;当我没有将heapify函数中的值赋给变量时,huffman树的代码正在工作 #包括 #包括 使用名称空间std; float AverageCodeLength=0; //包含输入符号的节点的结构及其相应概率 结构节点{ 字符符号; 浮动概率; 结构节点*左、*右; }; //堆的结构 结构MinHeap{ 整数大小; 结构节点**数组; }; //函数创建一个新节点 结构节点*createNode(字符符号,浮点概率){ 结构节点*temp=(结

我不明白为什么我的c++;当我没有将heapify函数中的值赋给变量时,huffman树的代码正在工作
#包括
#包括
使用名称空间std;
float AverageCodeLength=0;
//包含输入符号的节点的结构及其相应概率
结构节点{
字符符号;
浮动概率;
结构节点*左、*右;
}; 
//堆的结构
结构MinHeap{
整数大小;
结构节点**数组;
}; 
//函数创建一个新节点
结构节点*createNode(字符符号,浮点概率){
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
临时->左=临时->右=空;
温度->符号=符号;
温度->概率=概率;
返回温度;
} 
//用于检查提供的输入是否有效的函数
布尔性能单位检查(字符符号[],浮点概率[],整数n){
//检查总概率之和是否为1
浮点数和=0;
对于(int i=0;i概率;
} 
if(minHeap->array[右]){
rprob=minHeap->array[右]->概率;
} 
if(最小堆->数组[最小]){
lprob=minHeap->array[最小]->概率;
} 
cout数组[left]->概率数组[left]->概率数组[minimest]->概率)
最小=左;
if(rightarray[right]->probabilityarray[minimest]->probability)
最小=右;
如果(最小!=idx){
交换节点(&minHeap->array[最小],&minHeap->array[idx]);
Heapify(minHeap,最小);
} 
} 
//函数从堆中提取最小值
结构节点*extractMinValue(结构MinHeap*MinHeap){
结构节点*temp=minHeap->array[0];
minHeap->array[0]=minHeap->array[minHeap->size-1];
minHeap->size-=1;
Heapify(minHeap,0);
返回温度;
} 
//函数在minHeap中插入新节点
void insertMinHeap(struct MinHeap*MinHeap,struct Node*Node){
minHeap->size+=1;
inti=minHeap->size-1;
浮点nprob=节点->概率;
而(i&&nprobarray[(i-1)/2]->概率){
minHeap->array[i]=minHeap->array[(i-1)/2];
i=(i-1)/2;
} 
minHeap->array[i]=节点;
} 
//函数来构建给定大小的最小堆
struct MinHeap*buildMinHeap(字符符号[],浮点概率[],整数大小){
struct MinHeap*MinHeap=(struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size=size;
minHeap->array=(结构节点**)malloc(大小*sizeof(结构节点*);
对于(int i=0;iarray[i]=createNode(符号[i],概率[i]);
}
minHeap->size=size;
int n=大小-1;
对于(int i=(n-1)/2;i>=0;--i)
希皮菲(minHeap,i);
返回minHeap;
} 
//函数打印整数数组
void printArr(int arr[],int n){
int i;
对于(i=0;i你可以发布不起作用的代码,而不是不起作用的代码。我可以想象各种方法来恢复Heapify函数中被注释掉的代码,但我无法准确地告诉你在崩溃时有哪些代码。@john我已经编辑了它。你能看一下吗。谢谢你,这将确保我发布代码从下次开始更正。
int n;字符符号[n]
。我可能错了,但我认为这不符合您的预期。当看到
cin>>n;
并更改这两个数组的分配时,逻辑不会神奇地返回。谢谢,但这是有效的。我在从控制台获取值后检查了它。您应该发布不起作用的代码,而不是不起作用的代码。我可以想象一下在Heapify函数中恢复注释掉的代码的各种方法,但是我无法确切地告诉您在发生崩溃时使用的代码。@john我已经编辑了它,您可以看一下它一次。谢谢,这将确保我从下次开始发布要更正的代码。
int n;char symbols[n]
。我可能错了,但我认为这不符合您的预期。当看到
cin>>n;
并更改这两个数组的分配时,逻辑不会神奇地返回。谢谢,但这是可行的,我在从控制台获取值后检查了它
#include<bits/stdc++.h>
#include<string>
using namespace std; 
  
float AverageCodeLength = 0;

// structure of a node containing the input symbol and its corresponding probability
struct Node { 
    char symbol; 
  
    float probability; 
  
    struct Node *left, *right; 
}; 

// structure of a minheap
struct MinHeap { 
  
    int size; 
   
    struct Node** array; 
}; 

// function to create a newnode  
struct Node* createNode(char symbol, float probability) { 
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); 
  
    temp->left = temp->right = NULL; 
    temp->symbol = symbol; 
    temp->probability = probability; 
  
    return temp; 
} 
  
// function to check whether the provided input is valid or
bool performSanityCheck(char symbols[] ,float probabilities[] , int n){
    
    //check if the total probabilities sum to 1
    float sum = 0;

    for (int i = 0 ; i < n ; i++){
        sum += probabilities[i];
    }

    // cout << sum << "\n";

    if (sum != 1) return true ;

    //check if there are unique symbols being used

    //first we will sort the symbols array

    sort(symbols , symbols + n);

    // printCharacterArray(symbols , n);

    bool flg = false ;

    for (int i = 0 ; i<n-1 ; i++){

        if (symbols[i] == symbols[i+1]){

            flg = true;
            break;

        }
    }

    return flg;
}

// function to swap two nodes
void swapNodes(struct Node** a, struct Node** b) { 
    struct Node* t = *a; 
    *a = *b; 
    *b = t; 
} 
  
// The standard Heapify function. 
void Heapify(struct MinHeap* minHeap, int idx) { 
    int smallest = idx; 
    int left = 2 * idx + 1; 
    int right = 2 * idx + 2; 
    int size = minHeap-> size;
    float lprob = 0 ;
    float rprob = 0 ;
    float sprob = 0;

    if (minHeap->array[left]){
        lprob = minHeap->array[left]->probability;
    } 
    if (minHeap->array[right]){
        rprob = minHeap->array[right]->probability;
    } 
    if (minHeap->array[smallest]){
        lprob = minHeap->array[smallest]->probability;
    } 
    cout << minHeap->array[left]-> probability << " " << minHeap->array[right]-> probability << " " << minHeap->array[smallest]->probability <<  endl;
    
    if (left < size && minHeap->array[left]-> probability < minHeap->array[smallest]->probability) 
        smallest = left; 
  
    if (right < size && minHeap->array[right]-> probability < minHeap->array[smallest]->probability) 
        smallest = right; 
  
    if (smallest != idx) { 
        swapNodes(&minHeap->array[smallest], &minHeap->array[idx]); 
        Heapify(minHeap, smallest); 
    } 
} 
  

//function to extract minimum value from a heap
struct Node* extractMinValue(struct MinHeap* minHeap) { 
  
    struct Node* temp = minHeap->array[0]; 
    minHeap->array[0] = minHeap->array[minHeap->size - 1]; 
  
    minHeap->size -= 1; 
    Heapify(minHeap, 0); 
  
    return temp; 
} 
  
// function to insert a new node in the minHeap
void insertMinHeap(struct MinHeap* minHeap, struct Node* Node) { 
  
    minHeap->size += 1; 
    int i = minHeap->size - 1; 
    float nprob = Node->probability;
    while (i && nprob < minHeap->array[(i - 1) / 2]->probability) { 
  
        minHeap->array[i] = minHeap->array[(i - 1) / 2]; 
        i = (i - 1) / 2; 
    } 
  
    minHeap->array[i] = Node; 
} 
  
// function to build min heap of the given size
struct MinHeap* buildMinHeap(char symbol[], float probability[], int size)   { 
    
    struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap)); 
  
    minHeap->size = size; 
  
    minHeap->array = (struct Node**)malloc(size * sizeof(struct Node*)); 
  
    for (int i = 0; i < size; ++i) {
        minHeap->array[i] = createNode(symbol[i], probability[i]); 
    }

    minHeap->size = size; 

    int n = size - 1;  
  
    for (int i = (n - 1) / 2; i >= 0; --i) 
        Heapify(minHeap, i); 
    
    return minHeap;
} 
  
// function to print an array of integers
void printArr(int arr[], int n) { 
    int i; 
    for (i = 0; i < n; ++i) 
        cout<< arr[i]; 
  
    cout<<"\n"; 
} 

//function to print an array of decimal values , i.e , probabilities

void printFloatArray(float arr[] , int n){
    for (int i = 0; i<n ; i++){
        cout << arr[i] << " ";
    }
    cout << endl;
}

//function to print an array of character , i.e , symbols

void printCharacterArray(char arr[] , int n){
    for (int i = 0; i<n ; i++){
        cout << arr[i] << " ";
    }
    cout << endl;
}

// function to check if a node is leafnode or not 

bool isLeaf(struct Node* root) { 
    return !(root->left) && !(root->right); 
} 

// function to build huffmantree with the given symbols and their corresponding probabilities 

struct Node* buildHuffmanTree(char symbol[], float probability[], int size) { 
    struct Node *left, *right, *top; 
    struct MinHeap* minHeap = buildMinHeap(symbol, probability, size); 
  
    while (!(minHeap->size == 1)) { 
  
        // here we extract wo minimum values from our heap 
        left = extractMinValue(minHeap); 
        right = extractMinValue(minHeap); 

        // here $ represents a new node which is not an input symbol and represents an internal node while building our huffman tree
        top = createNode('$', left->probability + right->probability); 
  
        top->left = left; 
        top->right = right; 

        // we insert the newly created intermediate node into the minheap
        insertMinHeap(minHeap, top); 
    } 
    // after the tree is being built the minimum value is present at the root of the tree which is being returned here
    return extractMinValue(minHeap); 
} 
  

// function for printing the huffman codes for corresponding symbols

void printHuffManCodes(struct Node* root, int arr[], int top) { 
  
    if (root->left) {  
        arr[top] = 1;
        printHuffManCodes(root->left, arr, top + 1); 
    } 
  
    if (root->right) { 
        arr[top] = 0;
        printHuffManCodes(root->right, arr, top + 1); 
    } 
  
    if (isLeaf(root)) { 
        cout<< " " << root->symbol <<": "; 
        //cout << top << endl;
        //cout << root->probability << endl;
        ::AverageCodeLength += top*root->probability;
        printArr(arr, top); 
    } 
} 
  


int main() { 
  
    // taking user input

    int n ;                 // variable for getting the number of unique symbols 

    char symbols[n];
    float  probabilities[n] ;     //arrays for the symbols and their probablities

    while (true) {

        cout << " Enter the no. of unique symbols ";
        cin >> n ; 

        cout << " Enter the symbols and their corresponding probabilities \n";

        for (int i = 0 ; i<n ; i++){
            cout << " Enter the " + to_string(i+1) + "th symbol   ";
            cin >> symbols[i] ;
            cout << " Enter the " + to_string(i+1) + "th probability   ";
            cin >> probabilities[i];
        }

        bool flg = performSanityCheck(symbols,probabilities,n);
        //cout << "flg" << flg;
        //printFloatArray(probabilities,n);
        //printCharacterArray(symbols,n);
        if (!flg) break ;
        else{
            cout << " The values provided were not correct . Please make sure that all the entered symbols are unique and all the probabilities sum up to 1 \n";
        } 
    }
    // Construct Huffman Tree 
    struct Node* root  = buildHuffmanTree(symbols, probabilities, n); 
   
    int arr[n], top = 0; 
  
    printHuffManCodes(root, arr, top); 
    cout << " AverageCodeLength: " << AverageCodeLength << endl;
    return 0; 
}