fork download
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define MAXCHILD 4
  5. #define MAXNODE 20
  6.  
  7.  
  8. struct treenode
  9. {
  10. char info;
  11. // pointer to first child
  12. struct treenode *firstchild;
  13. // pointer to next sibling or father node
  14. struct treenode *next;
  15. // flag=0 if next is sibling , 1 if next is father node
  16. int flag;
  17. };
  18.  
  19. typedef struct treenode *NODEPTR;
  20. int nodename=65;// used for naming tree node
  21. int nodecount=0; // To count total number of node in Tree
  22. // To store address of ptr to every node
  23. // Every Node and its related info can be directly accessed from it
  24. NODEPTR *stack;
  25. NODEPTR root;//Pointer to the root of the Tree
  26.  
  27. /* All function written are listed Here */
  28. NODEPTR getnode();
  29. void freenode(NODEPTR p);
  30. NODEPTR maketree(char x);
  31. void setchild(NODEPTR tptr,char child[],int n);
  32. void breadthTraversalTreeGenerate();
  33.  
  34. int main()
  35. {
  36. int i;
  37. srand(time(NULL));// used to get random Trees
  38. // Random Tree Generation
  39. stack = malloc(sizeof(*stack)*20);
  40. breadthTraversalTreeGenerate();
  41. free(stack);
  42.  
  43.  
  44.  
  45. printf("\n");
  46. return 0;
  47. }
  48.  
  49. NODEPTR getnode()
  50. {
  51. NODEPTR p;
  52. p=(struct treenode*)malloc(sizeof(struct treenode));
  53. return p;
  54. }
  55.  
  56. void freenode(NODEPTR p)
  57. {
  58. free(p);
  59. }
  60.  
  61. NODEPTR maketree(char x)
  62. {
  63. NODEPTR p;
  64. p=getnode();
  65. p->info=x;
  66. p->flag=0;
  67. p->firstchild=NULL;
  68. p->next=NULL;
  69.  
  70.  
  71. return(p);
  72. }
  73.  
  74. void setchild(NODEPTR tptr,char child[],int n)
  75. {
  76. NODEPTR p,q;
  77. int i,j=0;
  78. if((tptr->firstchild)!=NULL)
  79. printf("VOID INSERTION");
  80. else
  81. {
  82. if (n==1)
  83. printf("\n%c has one child, namely \t",tptr->info);
  84. else
  85. printf("\n%c has %d children, namely \t",tptr->info,n);
  86.  
  87. p=maketree(child[j]);
  88. j++;
  89. tptr->firstchild=p;
  90. printf("%c",tptr->firstchild->info);
  91. stack[nodecount]=&(tptr->firstchild);
  92. nodecount++;
  93. for(i=0;i<n-1;i++)
  94. {
  95. q=maketree(child[j]);
  96. j++;
  97. p->next=q;
  98. printf("\t%c",q->info);
  99. stack[nodecount]=&(p->next);
  100. nodecount++;
  101. p=q;
  102. }
  103. p->flag=1;
  104. p->next=tptr;
  105. printf("\n");
  106. }
  107. }
  108.  
  109. void breadthTraversalTreeGenerate()
  110. {
  111. int i,j,n,dummy;
  112. char *child,lol;
  113. NODEPTR tptr;
  114.  
  115. // tree-> info can be given anything I used char notation,it will
  116. // be easy to understand order traversing using alphabats.
  117. root=maketree(nodename);
  118. // to store address of ptr to everynode
  119. // will be used in generating children
  120. stack[nodecount]=&root;
  121. nodecount++;
  122. // nodename++ will make next nodename as next character
  123. //i.e A, then B, then C & so on..
  124. nodename++;
  125.  
  126.  
  127. // Set children for next levels
  128. dummy=nodecount-1;
  129. while(dummy>=0)
  130. {
  131. dummy=nodecount-1;
  132. tptr=*(stack[dummy]);
  133. while(tptr->firstchild==NULL)
  134. {
  135.  
  136. n=(rand()%(MAXCHILD))+1;
  137. if(nodecount+n>MAXNODE)
  138. break;
  139. child=(char *)malloc(sizeof(char)*n);
  140. for(i=0;i<n;i++)
  141. {
  142. child[i]=nodename;
  143. nodename++;
  144. }
  145. setchild(tptr,child,n);
  146. dummy--;
  147. if (dummy<0)
  148. {
  149. dummy++;
  150. break;
  151. }
  152. tptr=*stack[dummy];
  153.  
  154. }
  155. if(nodecount+n>MAXNODE)
  156. break;
  157. }
  158.  
  159. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:37:2: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
  srand(time(NULL));// used to get random Trees
  ^
prog.c:36:6: warning: unused variable ‘i’ [-Wunused-variable]
  int i;
      ^
prog.c: In function ‘setchild’:
prog.c:91:19: warning: assignment from incompatible pointer type [enabled by default]
   stack[nodecount]=&(tptr->firstchild);
                   ^
prog.c:99:20: warning: assignment from incompatible pointer type [enabled by default]
    stack[nodecount]=&(p->next);
                    ^
prog.c: In function ‘breadthTraversalTreeGenerate’:
prog.c:120:18: warning: assignment from incompatible pointer type [enabled by default]
  stack[nodecount]=&root;
                  ^
prog.c:132:7: error: incompatible types when assigning to type ‘NODEPTR’ from type ‘struct treenode’
   tptr=*(stack[dummy]);
       ^
prog.c:152:8: error: incompatible types when assigning to type ‘NODEPTR’ from type ‘struct treenode’
    tptr=*stack[dummy];
        ^
prog.c:112:14: warning: unused variable ‘lol’ [-Wunused-variable]
  char *child,lol;
              ^
prog.c:111:8: warning: unused variable ‘j’ [-Wunused-variable]
  int i,j,n,dummy;
        ^
stdout

Standard output is empty