如何在c++中的结构指针变量树(对于完整树)中存储值
How to store values in struct pointer variable tree (for full tree) in c++
我是编程和c++方面的新手(我想问题的概念在c中也是一样的)。
我正在阅读一个文件作为唯一的参数,其中包含字母表(我代码中的符号),如"aabbacceaad",用于计算频率。我的代码计算频率正确。我确信。问题是在我的代码Node*树指针变量(类型为Node)中。我正在用它来创建树。但是,当我尝试根据从重复符号计算的频率创建树时,这个树指针变量只记住for-loop
之外最后执行的频率请注意,我只需要使用指针而不是数组。
我的意思是假设我在输入文件中有这样的符号"aabcdedeebbaa"。它的预期输出是:
0 symbol:a Freq:4 Left 0 Right 0 Index1
1 symbol:b Freq:3 Left 0 Right 0 Index2
2 symbol:c Freq:1 Left 0 Right 0 Index3
3 symbol:d Freq:2 Left 0 Right 0 Index4
4 symbol:e Freq:3 Left 0 Right 0 Index-1
但我的代码输出是这样的:
0 symbol:e Freq:3 Left:0 Right:0 Next:5 //Last "e" is executed,tree variable forgot a,b,c and d.
1 symbol:e Freq:3 Left:0 Right:0 Next:5
2 symbol:e Freq:3 Left:0 Right:0 Next:5
3 symbol:e Freq:3 Left:0 Right:0 Next:5
4 symbol:e Freq:3 Left:0 Right:0 Next:-1
我的完整c++代码是:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <cassert>
#include <vector>
using namespace std;
class Huffman {
public: int data_size,
length;
Huffman(char * argv);
~Huffman() {};
vector < char > storesym;
vector < int > storefreq;
struct Node
{
int value;
int freq, next;
short flag;
unsigned char symbol;
struct Node * left, * right;
};
Node * tree;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTOR definition ////////////////////////////////////////////////////////////////////////////////
Huffman::Huffman(char * argv)
{
char c;
int count = 0;
int flag[256]; //this flag is to know if the alphabet is already counted or not.If counted that i set it to "1" other wise it is "0".Please see below in my code
int j = 0;
FILE * input_file;
int *symbolarray;
symbolarray=new int[30];
char i, n;
input_file = fopen(argv, "rb");
c = fgetc(input_file);
////////////////////////////////////////////////////////////////////////////// From here to down i read the alphabbets from file taken as sole argument ///////////////////////////////////////
while (c != EOF && c != 'n' && c != 'r')
{
symbolarray[count] = c;
count++;
c = fgetc(input_file);
}
fclose(input_file);
for (i = 0; i < count; i++)
flag[i] = {0 };
int fcount1 = 0;
for (i = 0; i < count; i++)
{
if (flag[i] == 0)
{
for (j = i; j < count; j++)
{
if (symbolarray[i] == symbolarray[j]&& flag[j] == 0)
{
fcount1++;
flag[j] = 1; //**I am setting flag to 1 those alphabets to 1 so that they will not be counted again on next iteration**
}
}
storesym.push_back(symbolarray[i]);
storefreq.push_back(fcount1);
}
fcount1 = 0;
}
cout << endl;
////////////////////////////////////////////////////////////////////////// ERROR PRONE PART STARTS NOW /////////////////////////////////////////////////////
for (i = 0; i < storesym.size(); i++)
{
tree = new Node; // the problem is here this tree pointer don't store the values for all alphabets, it just remembers the last executed alphabet after this for loop.
tree -> left = NULL;
tree ->right = NULL;
tree -> symbol = storesym[i];
tree -> freq = storefreq[i];
tree -> flag = 0;
tree -> next = i + 1;
cout<<"check1 : "<<tree -> symbol<<endl;
}
//////////////////////////////////////////////////////////////////// eror PRONE PART ENDS HERE ///////////////////////////////////////////////////////////////////////////////////
cout << "Here is the data read from file" << endl;
n = storesym.size() - 1;
for (int i = 0; i < storesym.size(); i++)
{
if (n == i)
{
tree -> next = -1;
cout << i << " symbol:" << tree -> symbol << " Freq:" << tree ->freq << " Left:" << tree -> left << " Right:" << tree -> right << " Next:" << tree -> next << endl;
break;
} else
{
cout << i << " symbol:" << tree -> symbol << " Freq:" << tree -> freq << " Left:" << tree -> left << " Right:" << tree ->right << " Next:" << tree -> next << endl;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char * * argv)
{
int freq[256] = {0};
if (argc < 2) {
cout << "Ohh.. Sorry , you forgot to provide the Input File please" << endl;
return (0);
}
Huffman Object1(argv[1]);
return (0);
}
**请帮我如何记住所有的"a,b,c,d和e"(不仅仅是最后一个"e")?我知道只有指针才能做一些事情。
此位
while (c != EOF && c != 'n' && c != 'r')
{
tree[count].symbol = c;
count++;
c = fgetc(input_file);
}
取消引用未初始化的指针CCD_ 2
这是一个很大的否定,意味着您的程序在形式上是未定义的
你只是运气不好,它没有坠毁。
你再往下走一点。
然后您在这个循环中分配:
for (i = 0; i < storesym.size(); i++)
{
tree = new Node; // the problem is here this tree pointer don't store the values for all alphabets, it just remembers the last executed alphabet after this for loop.
tree -> left = NULL;
tree ->right = NULL;
tree -> symbol = storesym[i];
tree -> freq = storefreq[i];
tree -> flag = 0;
tree -> next = i + 1;
cout<<"check1 : "<<tree -> symbol<<endl;
}
它重复地分配一个Node
并将tree
指向它。
换句话说,它不是在构建树,你需要重写它,这样它才能真正构建树
无论你学的是什么课程材料,最近都应该被覆盖了。
我能给出的最好的建议是:重新开始,多读一些,在测试之前不要写太多代码。
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 将字符串存储在c++中的稳定内存中
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- std::原子加载和存储都需要吗
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- MOVNTI 存储是否相对于由同一线程创建的其他 MOVNTI 存储重新排序?
- 为什么 std::vector 适用于类定义中的不完整类型?
- C++ ->尝试逐字阅读一行文本。如何使指针等效于用于存储输入的当前二维数组
- 如何在类中存储指向字符串到成员函数映射的智能指针,以便它不会因类型不完整而失败?
- 静态类模板成员:将"sizeof"应用于不完整类型无效
- 如何在c++中的结构指针变量树(对于完整树)中存储值
- 将"sizeof"应用于不完整的类型(创建的类)无效
- 释放嵌入式应用程序中的内存无助于减少虚拟存储
- 应用于不完整类型的变量的成员访问表达式
- 需要帮助将 Kruskal 算法应用于使用 2D 结构存储数据的现有邻接矩阵程序
- 存储一组不重叠的范围,并严格查找一个值是否存在于任何一个范围中
- 你如何检查一个特定的文件名是否存在于谷歌云存储?
- 创建自己的输出存储而不是应用于现有存储的c++算法
- 如何存储一个完整的句子与空白的字符串在cpp