Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node
  4. {
  5.   int ID;
  6.   struct Node *leftMostChild;
  7.   struct Node *rightSibling;
  8. } Node;
  9.  
  10. Node *root;
  11. //        1
  12. //  2     3      4
  13. //  |     |     / \
  14. //  5     6    7   8
  15. //            / \
  16. //          10   9
  17. Node *makeNode(int ID)
  18. {
  19.   Node *newNode = (Node *)malloc(sizeof(Node));
  20.   newNode->ID = ID;
  21.   newNode->leftMostChild = NULL;
  22.   newNode->rightSibling = NULL;
  23.   return newNode;
  24. }
  25. Node *findID(Node *tree, int u)
  26. {
  27.   if (tree == NULL)
  28.     return NULL;
  29.   if (tree->ID == u)
  30.     return tree;
  31.   Node *p = tree->leftMostChild;
  32.   while (p != NULL)
  33.   {
  34.     Node *q = findID(p, u);
  35.     if (q != NULL)
  36.       return q;
  37.     p = p->rightSibling;
  38.   }
  39.   return NULL;
  40. }
  41. void addChild(Node *tree, int u, int x)
  42. {
  43.   //create a node having ID x, addd this node at the end of children list of node with ID u
  44.   Node *p = findID(tree, u);
  45.   if (p == NULL)
  46.     return;
  47.   Node *newNode = makeNode(x);
  48.   if (p->leftMostChild == NULL)
  49.     p->leftMostChild = newNode;
  50.   else
  51.   {
  52.     Node *last = p->leftMostChild;
  53.     while (last->rightSibling != NULL)
  54.     {
  55.       last = last->rightSibling;
  56.     }
  57.     last->rightSibling = newNode;
  58.   }
  59. }
  60.  
  61. void loadDataBuildTree(char *filename)
  62. {
  63.   FILE *f = fopen(filename, "r");
  64.   while (1)
  65.   {
  66.     int u;
  67.     fscanf(f, "%d", &u);
  68.     if (u == -1)
  69.       break;
  70.     if (root == NULL)
  71.       root = makeNode(u);
  72.     int k;
  73.     fscanf(f, "%d", &k);
  74.     for (int i = 0; i < k; i++)
  75.     {
  76.       int x;
  77.       fscanf(f, "%d", &x);
  78.       addChild(root, u, x);
  79.     }
  80.   }
  81.   fclose(f);
  82. }
  83.  
  84. void printTree(Node *r)
  85. {
  86.   if (r == NULL)
  87.     return;
  88.   if (r->leftMostChild != NULL)
  89.     printf("%d ", r->ID);
  90.   for (Node *p = r->leftMostChild; p != NULL; p = p->rightSibling)
  91.   {
  92.     printf("%d ", p->ID);
  93.     if(p->rightSibling==NULL) printf("\n");
  94.   }
  95.   for (Node *p = r->leftMostChild; p != NULL; p = p->rightSibling)
  96.     printTree(p);
  97. }
  98.  
  99.  
  100. int countNodes(Node *r)
  101. {
  102.   int c = 1;
  103.   if (r == NULL)
  104.     return 0;
  105.   Node *p = r->leftMostChild;
  106.   while (p != NULL)
  107.   {
  108.     c += countNodes(p);
  109.     p = p->rightSibling;
  110.   }
  111.   return c;
  112. }
  113. int countLeaves(Node *r)
  114. {
  115.   if (r == NULL)
  116.     return 0;
  117.   if (r->leftMostChild == NULL)
  118.     return 1;
  119.   int c = 0;
  120.   Node *p = r->leftMostChild;
  121.   while (p != NULL)
  122.   {
  123.     c += countLeaves(p);
  124.     p = p->rightSibling;
  125.   }
  126.   return c;
  127. }
  128.  
  129. int height(Node *cur)
  130. {
  131.   if (cur == NULL)
  132.     return 0;
  133.   int h = 0;
  134.   Node *p = cur->leftMostChild;
  135.   while (p != NULL)
  136.   {
  137.     int hp = height(p);
  138.     if (hp > h)
  139.       h = hp;
  140.     p = p->rightSibling;
  141.   }
  142.   return h + 1;
  143. }
  144. int depthTmp(Node *q, int depthQ, int u)
  145. {
  146.   if (q == NULL)
  147.     return -1;
  148.   if (q->ID == u)
  149.     return depthQ;
  150.   Node *p = q->leftMostChild;
  151.   while (p != NULL)
  152.   {
  153.     if (p->ID == u)
  154.       return depthQ + 1;
  155.     int d = depthTmp(p, depthQ + 1, u);
  156.     if (d > 0)
  157.       return d;
  158.  
  159.     p = p->rightSibling;
  160.   }
  161.   return -1;
  162. }
  163.  
  164. int depth(Node *r, int u)
  165. {
  166.   if (r == NULL)
  167.     return 0;
  168.   return depthTmp(r, 1, u);
  169. }
  170.  
  171. void preOrder(Node *r)
  172. {
  173.   if (r == NULL)
  174.     return;
  175.   printf("%d ", r->ID);
  176.   Node *p = r->leftMostChild;
  177.   while (p != NULL)
  178.   {
  179.     preOrder(p);
  180.     p = p->rightSibling;
  181.   }
  182. }
  183.  
  184. void inOrder(Node *r)
  185. {
  186.   if (r == NULL)
  187.     return;
  188.   inOrder(r->leftMostChild);
  189.   printf("%d ", r->ID);
  190.   if (r->leftMostChild == NULL)
  191.     return;
  192.   Node *p = r->leftMostChild->rightSibling;
  193.   while (p != NULL)
  194.   {
  195.     inOrder(p);
  196.     p = p->rightSibling;
  197.   }
  198. }
  199.  
  200. void postOrder(Node *r)
  201. {
  202.   if (r == NULL)
  203.     return;
  204.   Node *p = r->leftMostChild;
  205.   while (p != NULL)
  206.   {
  207.     postOrder(p);
  208.     p = p->rightSibling;
  209.   }
  210.   printf("%d ", r->ID);
  211. }
  212.  
  213. int main()
  214. {
  215.   loadDataBuildTree("treedata.txt");
  216.   int curID = 4;
  217.   Node *cur = findID(root, curID);
  218.  
  219.   printf("number of nodes: %d\nnumber of leaves: %d\n", countNodes(root), countLeaves(root));
  220.   printf("height of %d : %d\n", curID, height(cur));
  221.   printf("depth of %d : %d\n", curID, depth(root, curID));
  222.   printf("preOrder: "); preOrder(root);printf("\n");
  223.   printf("inOrder: ");inOrder(root);printf("\n");
  224.   printf("postOrder: ");postOrder(root);printf("\n");
  225.   printTree(root);
  226.   return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement