Advertisement
Guest User

Untitled

a guest
May 26th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node
  6. {
  7. char name[20];
  8. char partner[20];
  9. short isKilled;
  10. short isPartnerKilled;
  11. struct node *left;
  12. struct node *right;
  13. };
  14.  
  15. struct msgNode
  16. {
  17. struct node *n;
  18. char msg[50];
  19. };
  20.  
  21. /*
  22. struct node* newNode(int data)
  23. {
  24. // Allocate memory for new node
  25. struct node* node = (struct node*)malloc(sizeof(struct node));
  26.  
  27. // Assign data to this node
  28. node->data = data;
  29.  
  30. // Initialize left and right children as NULL
  31. node->left = NULL;
  32. node->right = NULL;
  33. return(node);
  34. }*/
  35.  
  36. void killPerson (struct node *root, char name[20])
  37. {
  38. if (!strcmp (root->name, name))
  39. {
  40. root->isKilled = 1;
  41. }
  42.  
  43. if (!strcmp (root->partner, name))
  44. {
  45. root->isPartnerKilled = 1;
  46. }
  47.  
  48. if (root->left)
  49. killPerson (root->left, name);
  50. if (root->right)
  51. killPerson (root->right, name);
  52.  
  53. }
  54.  
  55. void addChild (struct node *root, struct node *toAdd, char name[20])
  56. {
  57. if (!strcmp (root->name, name))
  58. {
  59. if (root->left == NULL)
  60. {
  61. root->left = toAdd;
  62. }
  63. else if (root->right == NULL)
  64. {
  65. root->right = toAdd;
  66. }
  67. else
  68. {
  69. printf ("All full, cant add\n");
  70. return;
  71. }
  72. }
  73.  
  74. if (root->left)
  75. addChild (root->left, toAdd, name);
  76. if (root->right)
  77. addChild (root->right, toAdd, name);
  78. }
  79.  
  80.  
  81. void padding (char ch, int n)
  82. {
  83. int i;
  84. for (i = 0; i < n; i++)
  85. putchar (ch);
  86. }
  87.  
  88. void structure (struct node *root, int level)
  89. {
  90. int i;
  91. if (root == NULL)
  92. {
  93. padding ('\t', level);
  94.  
  95. puts ("~");
  96. }
  97. else
  98. {
  99. structure (root->right, level + 2);
  100.  
  101. padding ('\t', level);
  102. printf ("%s", root->name);
  103. if (root->isKilled == 1)
  104. {
  105. putchar ('X');
  106. }
  107. putchar ('\n');
  108. padding ('\t', level);
  109. printf ("%s", root->partner);
  110. if (root->isPartnerKilled == 1)
  111. {
  112. putchar ('X');
  113. }
  114. putchar ('\n');
  115. structure (root->left, level + 2);
  116. }
  117. }
  118.  
  119.  
  120.  
  121.  
  122. int main ()
  123. {
  124. struct node *n = (struct node *) malloc (sizeof (struct node));
  125. gets (n->name);
  126. gets (n->partner);
  127.  
  128. struct node *p = (struct node *) malloc (sizeof (struct node));
  129. gets (p->name);
  130. gets (p->partner);
  131.  
  132. struct node *c = (struct node *) malloc (sizeof (struct node));
  133. gets (c->name);
  134. gets (c->partner);
  135.  
  136. addChild (n, p, "Ivan");
  137. addChild (n, c, "Ivan");
  138.  
  139. struct node *o = (struct node *) malloc (sizeof (struct node));
  140. gets (o->name);
  141. gets (o->partner);
  142.  
  143. addChild (n, o, "Marto");
  144.  
  145. killPerson (n, "Marto");
  146.  
  147. structure (n, 0);
  148.  
  149.  
  150.  
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement