fork(1) download
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct binaryTreeNode{
  5. int data;
  6. struct binaryTreeNode * left;
  7. struct binaryTreeNode * right;
  8. };
  9.  
  10. struct node
  11. {
  12. struct binaryTreeNode * data;
  13. struct node * next;
  14. };
  15.  
  16. struct queue
  17. {
  18. struct node * front;
  19. struct node * rear;
  20. };
  21.  
  22. struct queue * enQueue(struct queue * q, struct binaryTreeNode * num)
  23. {
  24. struct node * temp = (struct node*)malloc(sizeof(struct node));
  25. temp -> data = num;
  26. temp -> next = NULL;
  27. if(q == NULL)
  28. {
  29. q = (struct queue*)malloc(sizeof(struct queue));
  30. q -> front = temp;
  31. }
  32. else
  33. q -> rear -> next = temp;
  34. q -> rear = temp;
  35. //Code obtained from http://w...content-available-to-author-only...s.com
  36. //Feel free to copy but please acknowledge the site wherever possible
  37. return q;
  38. }
  39.  
  40. struct queue * deQueue(struct queue * q)
  41. {
  42. struct node * temp = q->front;
  43. q -> front = q->front->next;
  44. free(temp);
  45. if(q->front == NULL)
  46. return NULL;
  47. else
  48. return q;
  49. }
  50.  
  51. int isQueueEmpty(struct queue * q)
  52. {
  53. if(q)
  54. return 0;
  55. else
  56. return 1;
  57. }
  58.  
  59. int numberOfFullNodes(struct binaryTreeNode * root)
  60. {
  61. // Level order traversal
  62. struct binaryTreeNode * temp = NULL;
  63. struct queue * Q = NULL;
  64.  
  65. // Maintain a count
  66. int count = 0;
  67.  
  68. if(root == NULL)
  69. return 0;
  70.  
  71. Q = enQueue(Q, root);
  72. while(!isQueueEmpty(Q))
  73. {
  74. temp = Q -> front -> data;
  75.  
  76. // Now check if the node is a leaf node
  77. if(temp -> left != NULL && temp -> right != NULL)
  78. {
  79. // This means a full node
  80. count++;
  81. }
  82.  
  83. Q = deQueue(Q);
  84. if(temp -> left)
  85. Q = enQueue(Q, temp -> left);
  86. if(temp -> right)
  87. Q = enQueue(Q, temp -> right);
  88. }
  89. // Delete the queue
  90. free(Q);
  91.  
  92. // Now return the count
  93. return count;
  94. }
  95.  
  96. // Test the above functions
  97. int main(void)
  98. {
  99. // Initialize the tree
  100. struct binaryTreeNode * root = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  101. root-> data = 1;
  102. struct binaryTreeNode * l = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  103. l -> data = 2;
  104. struct binaryTreeNode * ll = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  105. ll -> data = 4;
  106. ll -> left = ll -> right = NULL;
  107. struct binaryTreeNode * lr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  108. lr -> data = 5;
  109. lr -> left = lr -> right = NULL;
  110. l -> left = ll;
  111. l -> right = lr;
  112. struct binaryTreeNode * r = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  113. r -> data = 3;
  114. struct binaryTreeNode * rl = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  115. rl -> data = 6;
  116. rl -> left = rl -> right = NULL;
  117. struct binaryTreeNode * rr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  118. rr -> data = 7;
  119. rr -> left = rr -> right = NULL;
  120. r -> left = rl;
  121. r -> right = rr;
  122. root -> left = l;
  123. root -> right = r;
  124.  
  125. printf("Number of full nodes = %d",numberOfFullNodes(root));
  126.  
  127. return 0;
  128. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
Number of full nodes = 3