fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int value;
  8. struct node* left;
  9. struct node* right;
  10. };
  11.  
  12. struct node* creation(int x)
  13. {
  14. struct node* temp = (struct node*) malloc(sizeof(struct node));
  15. temp->value = x;
  16. temp->right = NULL;
  17. temp->left = NULL;
  18. return temp;
  19. };
  20. void insertion(struct node** root, int x)
  21. {
  22.  
  23. if(*root==NULL) {*root = creation(x); return;}
  24. else
  25. {
  26. if (x <= (*root)->value) insertion(&((*root)->left),x);
  27. else insertion(&((*root)->right),x);
  28. }
  29. }
  30.  
  31. void print(struct node *root)
  32. {
  33. if (root == NULL) return;
  34. queue<node*> q;
  35. q.push(root);
  36. while(!q.empty())
  37. {
  38. cout << (q.front()->value) << " ";
  39. if ((q.front())->left != NULL) q.push((q.front())->left);
  40. if ((q.front())->right != NULL) q.push((q.front())->right);
  41. q.pop();
  42. }
  43. }
  44. void search(struct node *root, int level, int x)
  45. {
  46. if (root == NULL) {cout << "NOT found" << endl;return;}
  47. else
  48. {
  49. if (root -> value == x) {cout << "found at level: " << level << endl; return;}
  50. else if (root -> value < x){search(root->right, level + 1,x);}
  51. else if (root -> value > x){search(root->left, level + 1,x);}
  52. }
  53. }
  54. int height (struct node* root, int h)
  55. {
  56. if (root == NULL) return h;
  57. else {return max(height(root->right, h+1),height(root->left, h+1));}
  58. }
  59. int maxi (struct node* root)
  60. {
  61. if (root == NULL) return -1e9;
  62. if (root-> right == NULL) return root->value;
  63. return maxi(root->right);
  64. }
  65. int main()
  66. {
  67. struct node* root = creation(5);
  68. insertion(&root, 10);
  69. insertion(&root, 3);
  70. insertion(&root, 4);
  71. insertion(&root, 6);
  72. cout << maxi(root) << endl;
  73. print(root);
  74. }
  75.  
  76.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
10
5 3 10 4 6