fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5.  
  6. class BSTNode
  7. {
  8.  
  9. public:
  10. int data;
  11. BSTNode *right,*left;
  12.  
  13.  
  14. BSTNode()
  15. {
  16. left=0;
  17. right=0;
  18.  
  19. }
  20.  
  21. BSTNode(const int &value , BSTNode * LEFT=0 ,BSTNode * RIGHT=0)
  22. {
  23. data=value;
  24. left=LEFT;
  25. right=RIGHT;
  26. }
  27. BSTNode * getleft()
  28. {
  29. return left;
  30. }
  31. BSTNode * getright()
  32. {
  33. return right;
  34. }
  35. int getvalue()
  36. {
  37. return data;
  38. }
  39.  
  40. };
  41.  
  42. class BSTFCI
  43. {
  44. protected:
  45. BSTNode * root;
  46.  
  47. public:
  48. BSTFCI()
  49. {
  50. root=0;
  51. }
  52. void insertt ( const int &value)
  53. {
  54. BSTNode *itr= root;
  55. BSTNode *prev=0;
  56. while( itr !=0)
  57. {
  58. prev=itr;
  59. if (itr->data < value)
  60. itr=itr->right;
  61. else
  62. itr=itr->left;
  63. }
  64. if (root ==0)
  65. root = new BSTNode (value);
  66. else if (prev->data < value)
  67. prev->right = new BSTNode(value);
  68. else
  69. prev->left = new BSTNode(value);
  70.  
  71. }
  72. int getHeight(BSTNode *root)
  73. {
  74. if(root==NULL){return 0;}
  75.  
  76. return 1+ max(getHeight(root->left),getHeight(root->right));
  77. }
  78. bool isBalance( BSTNode* root)
  79. {
  80. if (root==0)
  81. return true;
  82. int hightDiff= getHeight(root->left) - getHeight(root->right);
  83. if (abs(hightDiff)>1)
  84. return false;
  85. else
  86. return isBalance(root->left) && isBalance(root->right);
  87. }
  88.  
  89.  
  90. };
  91.  
  92.  
  93. int main()
  94. {
  95.  
  96.  
  97. BSTFCI sana;
  98. sana.insertt(13);
  99. sana.insertt(10);
  100. sana.insertt(25);
  101. sana.insertt(2);
  102. sana.insertt(12);
  103. sana.insertt(20);
  104. sana.insertt(31);
  105.  
  106. BSTNode
  107. sana.isBalance(sana);
  108.  
  109. return 0;
  110. }
  111.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:107:9: error: expected initializer before ‘.’ token
     sana.isBalance(sana);
         ^
stdout
Standard output is empty