0

I wanna create a family tree using linked list and transverse the tree in preorder.

Rule 1: Each of the node should contain name, gender, pointer for storing the husband's/wive's information, and pointers to store the children's information. Rule 2: Set the number of pointers for the children's information to be the maximum number of children exists in the family. Any unused link should be pointing to NULL. The name of the spouse should be displayed next to the person's name.

The output of my program should be: May (F) & Fuk (M) - Mee (F) & Keong (M) - Min (F) & Heng (M) - Rui (F) - Ning (F) - Saw (F)-

I had a problem of words overflow in the char data. Can anyone help me out? Here are my code:

#include <stdio.h>
#include <stdlib.h>
struct BinTreeNode {
    char data;
    struct BinTreeNode *left;
    struct BinTreeNode *right;
};

struct BinTreeNode *initBinTreeNode(char data);

void visit(char data);

void preorder(struct BinTreeNode *node);
void postorder(struct BinTreeNode *node);
void inorder(struct BinTreeNode *node);

int main(void){
    struct BinTreeNode *root, *parent1, *parent2;
  /* growing the tree */
    root = initBinTreeNode('May (F) & Fuk (M)');
    root->left  = initBinTreeNode('Mee (F) & Keong (M)');
    root->right = initBinTreeNode('Saw (F)');
    parent1 = root->left;
    parent1->left  = initBinTreeNode('Min (F) & Heng (M)');
    parent1->right = initBinTreeNode('Ning (F)');
    parent2 = parent1->left;
    parent2->left  = initBinTreeNode('Rui (F)');
  /* traverse and print tree */
    printf("\nPreorder traversal:\t");  preorder(root);
    
    return(0);  }
void preorder(struct BinTreeNode *node){
  if (node){    /* if Node exists */
    visit(node->data);
    preorder(node->left);
    preorder(node->right);
  }
}

void visit(char data){
  printf("%c ", data);
}

struct BinTreeNode *initBinTreeNode(char data){
  struct BinTreeNode *temp;
  temp = (struct BinTreeNode *) 
    malloc(sizeof(struct BinTreeNode));
  temp->data  = data;
  temp->right = NULL;
  temp->left  = NULL;
  return(temp);
}
yiheng
  • 1
  • 1

0 Answers0