我試圖實現自適應huffman代碼,但在嘗試構建樹時,我在執行代碼時出現了段錯誤「currentNYT-> lchild = newNYT;」在addnode()函數中。分段錯誤 - 自適應Huffman樹
任何人都可以請幫我嗎?這可能是簡單的,我不知道。現在暫時不使用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
你可能是最好的重新發布作爲一個新的問題。 – caf 2009-11-17 21:38:51