Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node
- {
- char name[20];
- char partner[20];
- short isKilled;
- short isPartnerKilled;
- struct node *left;
- struct node *right;
- };
- struct msgNode
- {
- struct node *n;
- char msg[50];
- };
- /*
- struct node* newNode(int data)
- {
- // Allocate memory for new node
- struct node* node = (struct node*)malloc(sizeof(struct node));
- // Assign data to this node
- node->data = data;
- // Initialize left and right children as NULL
- node->left = NULL;
- node->right = NULL;
- return(node);
- }*/
- void killPerson (struct node *root, char name[20])
- {
- if (!strcmp (root->name, name))
- {
- root->isKilled = 1;
- }
- if (!strcmp (root->partner, name))
- {
- root->isPartnerKilled = 1;
- }
- if (root->left)
- killPerson (root->left, name);
- if (root->right)
- killPerson (root->right, name);
- }
- void addChild (struct node *root, struct node *toAdd, char name[20])
- {
- if (!strcmp (root->name, name))
- {
- if (root->left == NULL)
- {
- root->left = toAdd;
- }
- else if (root->right == NULL)
- {
- root->right = toAdd;
- }
- else
- {
- printf ("All full, cant add\n");
- return;
- }
- }
- if (root->left)
- addChild (root->left, toAdd, name);
- if (root->right)
- addChild (root->right, toAdd, name);
- }
- void padding (char ch, int n)
- {
- int i;
- for (i = 0; i < n; i++)
- putchar (ch);
- }
- void structure (struct node *root, int level)
- {
- int i;
- if (root == NULL)
- {
- padding ('\t', level);
- puts ("~");
- }
- else
- {
- structure (root->right, level + 2);
- padding ('\t', level);
- printf ("%s", root->name);
- if (root->isKilled == 1)
- {
- putchar ('X');
- }
- putchar ('\n');
- padding ('\t', level);
- printf ("%s", root->partner);
- if (root->isPartnerKilled == 1)
- {
- putchar ('X');
- }
- putchar ('\n');
- structure (root->left, level + 2);
- }
- }
- int main ()
- {
- struct node *n = (struct node *) malloc (sizeof (struct node));
- gets (n->name);
- gets (n->partner);
- struct node *p = (struct node *) malloc (sizeof (struct node));
- gets (p->name);
- gets (p->partner);
- struct node *c = (struct node *) malloc (sizeof (struct node));
- gets (c->name);
- gets (c->partner);
- addChild (n, p, "Ivan");
- addChild (n, c, "Ivan");
- struct node *o = (struct node *) malloc (sizeof (struct node));
- gets (o->name);
- gets (o->partner);
- addChild (n, o, "Marto");
- killPerson (n, "Marto");
- structure (n, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement