Question

In: Computer Science

Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary...

Subject: Homework - Family Trees

please give me C codes, thanks
FAMILY TREE
In a Binary Tree, each node has two children at most. To eliminate the restriction by linking the children
together to form a list. In this design, each node in the tree needs to contain only two pointers: one to its eldest
child and one to its next younger sibling.
Using this representation, in each node, the pointer on the left always points down to child; the pointer on the
right indicates the next sibling in the same generation.
struct node
{
char name[32];
struct node *child;
struct node *sibling;
};
typedef struct node NODE;
Using the above linked design to implement the following two functions.
1) NODE* SearchMember(NODE *tree, char *target);
that searches a node in a family tree. If it finds it, then it returns the address of the node. If not, it returns
NULL.
2) NODE* InsertMember(NODE *ptree, char *newName, char *parentName);
that searches the parent node with parentName. If found, add a new node with newName to its child list.
3) NODE* ReadFamilyTree(char *filename);
that reads in a family tree from a data file whose name is supplied as the argument to the call. The first line of
the file should contain a name corresponding to the root of the tree. All subsequent lines in the data file should
have the following form:
child:patent
where child is the name of the new individual being entered and parent is the name of that child's parent, which
must appear earlier in the data file. For example,
William I
Pobert:William I
William II:William I
Anna:William I
Henry I:William I
Steven:Anna
William:Henry I
Matilda:Henry I
Henry II:Matilda
4) void DisplayFamilyTree(NODE *tree);

that displays all the individuals in a family tree. To record the hierachy of tree, the output of your program
should ident each generation so that the name of each child appears two spaces to the right of the
corresponding parent, as shown in the following:
William I
Pobert
William II
Anna
   Steven
Henry I
   William
   Matilda
   Henry II

5) Write a test program that tests all the functions above.

Solutions

Expert Solution

N.B. In your problem, in the binary tree each node has two children at most. The data file should have the following form:
child:patent. But in the given data file has four children of William I. That is why output is mismatch with the desired result.

William I
Pobert:William I [ 1 child ]
William II:William I    [ 1 child ]
Anna:William I    [ 1 child ]
Henry I:William I   [ 1 child ]
Steven:Anna
William:Henry I
Matilda:Henry I
Henry II:Matilda

Program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//structure definition
struct node
{
char name[32];
struct node *child;
struct node *sibling;
};
typedef struct node NODE;

NODE* SearchMember(NODE *tree, char *target);
NODE* InsertMember(NODE *ptree, char *newName, char *parentName);
NODE* ReadFamilyTree(char *filename);
void DisplayFamilyTree(NODE *tree);

NODE *tree = NULL;

//main function
int main()
{
   char filename[] = "familytree.txt";
  
   tree = ReadFamilyTree(filename);
  
   DisplayFamilyTree(tree);
  
   return 0;  
}

//search a node
NODE* SearchMember(NODE *tree, char *target)
{
   if (tree == NULL)
return NULL;
  
if (strcmp(tree->name, target) == 0)
return tree;
  
/* search on the left sutree */
   return SearchMember(tree->child, target);
  
/* search on the right subtree */
return SearchMember(tree->sibling, target);
}

//insert a node
NODE* InsertMember(NODE *ptree, char *newName, char *parentName)
{
   NODE *t = SearchMember(ptree, parentName);
  
   if(t==NULL)
       return NULL;
  
   NODE *newnode = (NODE*) malloc(sizeof(NODE));
   newnode->child = NULL;
   newnode->sibling = NULL;
   strcpy(newnode->name,newName);
  
   if(t->child==NULL)
       t->child = newnode;
   else if(t->sibling==NULL)
       t->sibling = newnode;
      
   return ptree;
}

//function to read file and create the tree
NODE* ReadFamilyTree(char *filename)
{
   FILE *f;
   char pname[20], cname[20], ch;
  
   f = fopen(filename, "r");
   if(f==NULL)
   {
       printf("File not open!!");
       exit(1);
   }
  
   fscanf(f, "%[^\n]", pname);
   fscanf(f,"%c", &ch);

   NODE *newnode = (NODE*) malloc(sizeof(NODE));
   newnode->child = NULL;
   newnode->sibling = NULL;
   strcpy(newnode->name,pname);
  
   if(tree==NULL) tree = newnode;
  
   while(!feof(f))
   {
       fscanf(f, "%[^:]", cname);
       fscanf(f,"%c", &ch);
       fscanf(f, "%[^\n]", pname);
       fscanf(f,"%c", &ch);
       InsertMember(tree, cname, pname);  
   }
  
   fclose(f);
  
   return tree;
}

//display the tree
void DisplayFamilyTree(NODE *tree)
{
   if(tree ==NULL)
       return;
   printf("%s\n", tree->name);

   printf(" ");
   DisplayFamilyTree(tree->child);
   DisplayFamilyTree(tree->sibling);  
}

Output:

William I
Pobert
William II

N.B. Even, if you face any problem regarding this problem, then share with me, I'll help you.


Related Solutions

Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
In C++ with lots of comments please Complete a binary search tree of 20 numbers Show...
In C++ with lots of comments please Complete a binary search tree of 20 numbers Show in the output the steps while it's performing the search with 20 numbers in a text file called list.txt The numbers will be imported to the program Simple program that should let you have the option to search for numbers, remove numbers, print numbers, and insert numbers in the binary tree If the number isn't there then give an error
what is antiretroviral   pharmacodynamics and Antifungal pharmacodynamics. please give me complete answers thanks
what is antiretroviral   pharmacodynamics and Antifungal pharmacodynamics. please give me complete answers thanks
Hello, please can you give me the answers of these questions. Thanks 12.1 Because of the...
Hello, please can you give me the answers of these questions. Thanks 12.1 Because of the possibility that a forensic accounting engagement may result in litigation, ·         (a) forensic accountants must be law enforcement officers, lawyers, or officers of the court. ·         (b) forensic accountants must be certified by the ACFE. ·         (c) forensic accountants do not rely on interviews as a source of evidence. ·         (d) forensic accountants are likely to have an adversarial relationship with the employees of...
Can someone please give me a summary on chapter 1 (the lexus and the olive tree...
Can someone please give me a summary on chapter 1 (the lexus and the olive tree revisted) in the book bad samartians by Ha-Joon Chang
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Give a proof by induction that the number of nodes of a full binary tree with...
Give a proof by induction that the number of nodes of a full binary tree with a height of "h" is: (2^(h+1))-1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT