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[MAXNODE];
  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.  
  37. srand(time(NULL));// used to get random Trees
  38. // Random Tree Generation
  39. breadthTraversalTreeGenerate();
  40. printf("\n");
  41. return 0;
  42. }
  43.  
  44. NODEPTR getnode()
  45. {
  46. NODEPTR p;
  47. p=(struct treenode*)malloc(sizeof(struct treenode));
  48. return p;
  49. }
  50.  
  51. void freenode(NODEPTR p)
  52. {
  53. free(p);
  54. }
  55.  
  56. NODEPTR maketree(char x)
  57. {
  58. NODEPTR p;
  59. p=getnode();
  60. p->info=x;
  61. p->flag=0;
  62. p->firstchild=NULL;
  63. p->next=NULL;
  64.  
  65.  
  66. return(p);
  67. }
  68.  
  69. void setchild(NODEPTR tptr,char child[],int n)
  70. {
  71. NODEPTR p,q;
  72. int i,j=0;
  73. if((tptr->firstchild)!=NULL)
  74. printf("VOID INSERTION");
  75. else
  76. {
  77. if (n==1)
  78. printf("\n%c has one child, namely \t",tptr->info);
  79. else
  80. printf("\n%c has %d children, namely \t",tptr->info,n);
  81.  
  82. p=maketree(child[j]);
  83. j++;
  84. tptr->firstchild=p;
  85. printf("%c",tptr->firstchild->info);
  86. stack[nodecount]=&(tptr->firstchild);
  87. nodecount++;
  88. for(i=0;i<n-1;i++)
  89. {
  90. q=maketree(child[j]);
  91. j++;
  92. p->next=q;
  93. printf("\t%c",q->info);
  94. stack[nodecount]=&(p->next);
  95. nodecount++;
  96. p=q;
  97. }
  98. p->flag=1;
  99. p->next=tptr;
  100. printf("\n");
  101. }
  102. }
  103.  
  104. void breadthTraversalTreeGenerate()
  105. {
  106. int i,j,n,dummy;
  107. char *child,lol;
  108. NODEPTR tptr;
  109.  
  110. // tree-> info can be given anything I used char notation,it will
  111. // be easy to understand order traversing using alphabats.
  112. root=maketree(nodename);
  113. // to store address of ptr to everynode
  114. // will be used in generating children
  115. stack[nodecount]=&root;
  116. nodecount++;
  117. // nodename++ will make next nodename as next character
  118. //i.e A, then B, then C & so on..
  119. nodename++;
  120.  
  121.  
  122. // Set children for next levels
  123. dummy=nodecount-1;
  124. while(dummy>=0)
  125. {
  126. dummy=nodecount-1;
  127. tptr=*(stack[dummy]);
  128. while(tptr->firstchild==NULL)
  129. {
  130.  
  131. n=(rand()%(MAXCHILD))+1;
  132. if(nodecount+n>MAXNODE)
  133. break;
  134. child=(char *)malloc(sizeof(char)*n);
  135. for(i=0;i<n;i++)
  136. {
  137. child[i]=nodename;
  138. nodename++;
  139. }
  140. setchild(tptr,child,n);
  141. dummy--;
  142. if (dummy<0)
  143. {
  144. dummy++;
  145. break;
  146. }
  147. tptr=*stack[dummy];
  148.  
  149. }
  150. if(nodecount+n>MAXNODE)
  151. break;
  152. }
  153.  
  154. }
  155.  
  156.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
A has 2 children, namely 	B	C

C has 4 children, namely 	D	E	F	G

B has 2 children, namely 	H	I

I has 2 children, namely 	J	K

H has 2 children, namely 	L	M

G has one child, namely 	N

F has 3 children, namely 	O	P	Q

E has one child, namely 	R