fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct BinaryTreeNode {
  5. BinaryTreeNode* left;
  6. BinaryTreeNode* right;
  7. int val;
  8.  
  9. BinaryTreeNode(int value) {
  10. left = NULL;
  11. right = NULL;
  12. val = value;
  13. }
  14. };
  15.  
  16. void inorder(BinaryTreeNode* root) {
  17. if (root == NULL)
  18. return;
  19.  
  20. inorder(root->left);
  21. cout << root->val << " ";
  22. inorder(root->right);
  23. }
  24.  
  25. void preorder(BinaryTreeNode* root) {
  26. if (root == NULL)
  27. return;
  28.  
  29. cout << root->val << " ";
  30. preorder(root->left);
  31. preorder(root->right);
  32. }
  33.  
  34. void postorder(BinaryTreeNode* root) {
  35. if (root == NULL)
  36. return;
  37.  
  38. postorder(root->left);
  39. postorder(root->right);
  40. cout << root->val << " ";
  41. }
  42.  
  43. int SumOfAllNodes(BinaryTreeNode* root) {
  44. if (root == NULL)
  45. return 0;
  46. return root->val + SumOfAllNodes(root->left) + SumOfAllNodes(root->right);
  47. }
  48.  
  49. int calculateHeightOfTree(BinaryTreeNode* root) {
  50. if (root == NULL)
  51. return 0;
  52. int leftHeight = calculateHeightOfTree(root->left);
  53. int rightHeight = calculateHeightOfTree(root->right);
  54. return 1 + max(leftHeight, rightHeight);
  55. }
  56.  
  57. bool isElementExistsInTree(BinaryTreeNode* root, int key) {
  58. if (root == NULL)
  59. return false;
  60.  
  61. if (root->val == key)
  62. return true;
  63.  
  64. return isElementExistsInTree(root->left, key) || isElementExistsInTree(root->right, key);
  65. }
  66.  
  67. int main() {
  68. BinaryTreeNode* root = new BinaryTreeNode(5);
  69. BinaryTreeNode* leftNode = new BinaryTreeNode(2);
  70. BinaryTreeNode* rightNode = new BinaryTreeNode(7);
  71. BinaryTreeNode* leftLeft = new BinaryTreeNode(1);
  72. BinaryTreeNode* leftRight = new BinaryTreeNode(3);
  73.  
  74. root->left = leftNode;
  75. root->right = rightNode;
  76. leftNode->left = leftLeft;
  77. leftNode->right = leftRight;
  78.  
  79. cout << "Inorder Traversal: ";
  80. inorder(root);
  81. cout << endl;
  82.  
  83. cout << "Preorder Traversal: ";
  84. preorder(root);
  85. cout << endl;
  86.  
  87. cout << "Postorder Traversal: ";
  88. postorder(root);
  89. cout << endl;
  90.  
  91. cout << "Sum of all nodes: " << SumOfAllNodes(root) << endl;
  92. cout << "Height of tree: " << calculateHeightOfTree(root) << endl;
  93.  
  94. int searchValue;
  95. cout << "Enter value to search: ";
  96. cin >> searchValue;
  97.  
  98. if (isElementExistsInTree(root, searchValue))
  99. cout << "Element " << searchValue << " exists in the tree." << endl;
  100. else
  101. cout << "Element " << searchValue << " does not exist in the tree." << endl;
  102.  
  103. return 0;
  104. }
  105.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Inorder Traversal: 1 2 3 5 7 
Preorder Traversal: 5 2 1 3 7 
Postorder Traversal: 1 3 2 7 5 
Sum of all nodes: 18
Height of tree: 3
Enter value to search: Element 5263 does not exist in the tree.