fork download
  1. #include<stdio.h>
  2.  
  3. /* A tree node structure */
  4. struct node
  5. {
  6. int data;
  7. struct node *left;
  8. struct node *right;
  9. };
  10.  
  11. /* Helper function for getLevel(). It returns level of the data if data is
  12.   present in tree, otherwise returns 0.*/
  13. int getLevelUtil(struct node *node, int data, int level)
  14. {
  15. if (node == NULL)
  16. return 0;
  17.  
  18. if (node->data == data)
  19. return level;
  20. else
  21. {
  22. getLevelUtil(node->left, data, level+1);
  23. getLevelUtil(node->right, data, level+1);
  24.  
  25. }
  26.  
  27. /* Returns level of given data value */
  28. int getLevel(struct node *node, int data)
  29. {
  30. return getLevelUtil(node,data,1);
  31. }
  32.  
  33. /* Utility function to create a new Binary Tree node */
  34. struct node* newNode(int data)
  35. {
  36. struct node *temp = new struct node;
  37. temp->data = data;
  38. temp->left = NULL;
  39. temp->right = NULL;
  40.  
  41. return temp;
  42. }
  43.  
  44. /* Driver function to test above functions */
  45. int main()
  46. {
  47. struct node *root = new struct node;
  48. int x;
  49.  
  50. /* Constructing tree given in the above figure */
  51. root = newNode(3);
  52. root->left = newNode(2);
  53. root->right = newNode(5);
  54. root->left->left = newNode(1);
  55. root->left->right = newNode(4);
  56.  
  57. for (x = 1; x <=5; x++)
  58. {
  59. int level = getLevel(root, x);
  60. if (level)
  61. printf(" Level of %d is %d\n", x, getLevel(root, x));
  62. else
  63. printf(" %d is not present in tree \n", x);
  64.  
  65. }
  66.  
  67. return 0;
  68. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'newNode':
prog.c:36:25: error: 'new' undeclared (first use in this function)
     struct node *temp = new struct node;
                         ^
prog.c:36:25: note: each undeclared identifier is reported only once for each function it appears in
prog.c:36:29: error: expected ',' or ';' before 'struct'
     struct node *temp = new struct node;
                             ^
prog.c: In function 'getLevelUtil':
prog.c:45:5: warning: 'main' is normally a non-static function [-Wmain]
 int main()
     ^
prog.c: In function 'main':
prog.c:47:25: error: 'new' undeclared (first use in this function)
     struct node *root = new struct node;
                         ^
prog.c:47:29: error: expected ',' or ';' before 'struct'
     struct node *root = new struct node;
                             ^
prog.c: In function 'getLevelUtil':
prog.c:69:1: error: expected declaration or statement at end of input
 }
 ^
prog.c:69:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty