0% found this document useful (0 votes)
80 views

Family Tree

This document contains code to represent a family tree as a node structure with pointers. It includes functions to create nodes, add child-parent relationships, find nodes by name, count descendants, and calculate the generation of a node. The main function calls run, which gets input to build the tree and respond to queries about descendants and generations.

Uploaded by

ddkunkun267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Family Tree

This document contains code to represent a family tree as a node structure with pointers. It includes functions to create nodes, add child-parent relationships, find nodes by name, count descendants, and calculate the generation of a node. The main function calls run, which gets input to build the tree and respond to queries about descendants and generations.

Uploaded by

ddkunkun267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Family tree

#include <stdio.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct node {

char name[20];

struct node *child;

struct node *parent;

struct node *siblings;

};

struct node *nodes[10000];

int n ;

struct node *find (char *name){

int i;

for (i=0;i<n;i++){

if (strcmp(name, nodes[i]->name)==0) return nodes[i];

return NULL;

struct node *makenode (char a[]){

struct node* newnode = (struct node*)malloc(sizeof(struct node));

strcpy(newnode->name, a);
newnode->child = NULL;

newnode->parent = NULL;

newnode->siblings = NULL;

nodes[n] = newnode;

n++;

return newnode;

/* struct node *find (char a[], struct node *r){

if (r==NULL) return NULL;

if (strcmp(a, r->name)==0) return r;

struct node *p = r->child;

while(p!=NULL){

struct node *q = find(a,p);

if (q!=NULL) return q;

p = p->siblings;

return NULL;

void addChild (char child[], char parent[]){

struct node *p = find(child, root);

struct node *q = find(parent, root);

if (p==NULL && q == NULL){

struct node *p = makenode(child);

struct node *q= makenode(parent);

q->child = p;

p->parent = q;

}
else if (p==NULL && q != NULL){

struct node *p = makenode(child);

struct node *r = q->child;

while (r->siblings!=NULL) r = r->siblings;

r->siblings = p;

p->parent = q;

else if (p!=NULL && q == NULL){

struct node *q= makenode(parent);

q->child = p;

p->parent = q;

else{

struct node *r = q->child;

while (r->siblings!=NULL) r = r->siblings;

r->siblings = p;

p->parent = q;

}*/

void makechildparent (struct node *childnode, struct node *parentnode){

childnode->parent = parentnode;

childnode->siblings = parentnode->child;

parentnode->child = childnode;

int count (struct node *r){

if (r==NULL)return 0;

int c = 1;
struct node *p ;

for (p = r->child; p !=NULL; p=p->siblings){

c += count(p);

return c;

int height (struct node *r){

if (r==NULL) return 0;

int max = 0;

struct node *p = r->child;

for (;p!=NULL;p = p->siblings){

int h = height(p);

if (max<h) max = h;

return max+1;

void run() {

char child[30];

char parent[30];

while (1){

scanf("%s", child);

if (strcmp(child, "***")==0) break;

scanf("%s", parent);

struct node *childnode = find(child);

if (childnode == NULL) childnode = makenode(child);

struct node *parentnode = find(parent);

if (parentnode == NULL) parentnode = makenode(parent);


makechildparent(childnode, parentnode);

char query[30];

char name[30];

while (1){

scanf("%s", query);

if (strcmp(query, "***")==0) break;

else if (strcmp(query, "descendants")==0){

scanf("%s", name);

struct node *t = find(name);

printf ("%d\n", count(t)-1);

else if (strcmp(query, "generation")==0){

scanf("%s", name);

struct node *t = find(name);

printf ("%d\n", height(t)-1);

int main(){

n = 0;

run();

You might also like