我在調用viewTree(root)時出現段錯誤;分段錯誤 - 顯示樹
struct treeElement {
unsigned long weight;
unsigned short id;
char chr;
struct treeElement *lchild, *rchild, *parent;
};
typedef struct treeElement node;
node *root;
//INITIALIZE TREE
void initTree() {
root = malloc(sizeof(node));
currentNYT = root;
} //initTree
//VIEW TREE
void viewTree(node *tree) {
printf("%5d%5d%5d%5d%5c%lu", tree->id, tree->parent->id, tree->lchild->id, tree->rchild->id, tree->chr, tree->weight);
viewTree(tree->lchild);
viewTree(tree->rchild);
}
//ADD NODE
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
int main()
{
initTree();
addNode('a');
addNode('b');
viewTree(root);
getchar();
return 0;
}
而你的問題是? – 2009-11-17 21:25:38