我正在尝试实现自适应霍夫曼代码,但是在尝试构建树时,在“currentNYT->lchild = newNYT;”行执行代码时出现分段错误 在 addnode() 函数中。
有人可以帮我吗?这可能是我不知道的简单事情。好久没用C了。
//variable and type declarations
struct treeElement {
unsigned long weight;
unsigned short id;
char chr;
struct treeElement *lchild, *rchild, *parent;
};
typedef struct treeElement node;
node *root, *currentNYT;
//functions
void initTree() {
root = NULL;
currentNYT = malloc(sizeof(node));
currentNYT = root;
} //initTree
void addNode(char newNodeChr) {
node *newNYT, *newExternal;
newNYT = malloc(sizeof(node));
newNYT->id=maxNodes-idCount; idCount++;
newNYT->chr='\0';
newNYT->weight=0;
newNYT->parent=currentNYT;
newNYT->lchild=newNYT->rchild=NULL;
newExternal = malloc(sizeof(node));
newExternal->id=maxNodes-idCount;
newExternal->chr=newNodeChr;
newExternal->weight=1;
newExternal->parent=currentNYT;
newExternal->lchild=newExternal->rchild=NULL;
currentNYT->lchild = newNYT;
currentNYT->rchild = newExternal;
currentNYT=newNYT;
} //addNode