I am trying to implement the adaptive huffman code, but while trying to build the tree i get a segmentation fault when executing the code at line "currentNYT->lchild = newNYT;" in addnode() function.
Could anyone please help me out? It might be something simple i'm not aware of. didn't use C for a while now.
//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