fork(2) 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. void leftViewOfBinaryTree(struct binaryTreeNode * root)
  60. {
  61. // Level order traversal
  62. struct binaryTreeNode * temp = NULL;
  63. struct queue * Q = NULL;
  64.  
  65. // Maintain a level count
  66. int level = 0;
  67.  
  68. if(root == NULL)
  69. return;
  70.  
  71. Q = enQueue(Q, root);
  72.  
  73. //print the root
  74. printf("%d ",root->data);
  75.  
  76. int needToPrint = 0;
  77.  
  78. // Now the first level will end over here,
  79. // So append a NULL node
  80. Q = enQueue(Q, NULL);
  81.  
  82. while(!isQueueEmpty(Q))
  83. {
  84. temp = Q -> front -> data;
  85. Q = deQueue(Q);
  86.  
  87. if(needToPrint)
  88. {
  89. printf("%d ", temp->data);
  90. needToPrint = 0;
  91. }
  92.  
  93. // If we encounter a NULL, that means an end of a level
  94. // And we need to increment the level count
  95. if(temp == NULL)
  96. {
  97. // Put the marker for next level also
  98. if(!isQueueEmpty(Q))
  99. Q = enQueue(Q, NULL);
  100.  
  101. level++;
  102. needToPrint = 1;
  103. }
  104. else
  105. {
  106. // We continue with the level order traversal
  107. if(temp -> left)
  108. Q = enQueue(Q, temp -> left);
  109. if(temp -> right)
  110. Q = enQueue(Q, temp -> right);
  111. }
  112. }
  113. // Delete the queue
  114. free(Q);
  115. }
  116.  
  117. // Test the above functions
  118. int main(void)
  119. {
  120. // Initialize the tree
  121. struct binaryTreeNode * root = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  122. root-> data = 1;
  123. struct binaryTreeNode * l = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  124. l -> data = 2;
  125. struct binaryTreeNode * ll = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  126. ll -> data = 4;
  127. ll -> left = ll -> right = NULL;
  128. struct binaryTreeNode * lr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  129. lr -> data = 5;
  130. lr -> left = lr -> right = NULL;
  131. l -> left = ll;
  132. l -> right = lr;
  133. struct binaryTreeNode * r = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  134. r -> data = 3;
  135. struct binaryTreeNode * rl = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  136. rl -> data = 6;
  137. rl -> left = rl -> right = NULL;
  138. struct binaryTreeNode * rr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  139. rr -> data = 7;
  140. rr -> left = rr -> right = NULL;
  141. r -> left = rl;
  142. r -> right = rr;
  143. root -> left = l;
  144. root -> right = r;
  145.  
  146. printf("Left view of tree = ");
  147. leftViewOfBinaryTree(root);
  148.  
  149. return 0;
  150. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
Left view of tree = 1 2 4