fork download
  1. # include <iostream>
  2. # include <conio.h>
  3. using namespace std;
  4. int h = 0;
  5. class date
  6. {
  7. public:
  8. date(int day=1, int month=1, int year = 1);
  9. friend bool operator > (const date a, const date b);
  10. friend bool operator == (const date a, const date b);
  11. friend istream& operator >> (istream& in,date& a);
  12. friend ostream& operator << (ostream& out, date& a);
  13. ~date();
  14. private:
  15. int day;
  16. int month;
  17. int year;
  18. };
  19.  
  20. date::date(int d, int m, int y)
  21. {
  22. day = d; month = m; year = y;
  23. }
  24.  
  25. bool operator > (date a1, date b1){
  26. if(a1.year > b1.year)
  27. return 1;
  28. else if((a1.year==b1.year)&&(a1.month>b1.month))
  29. return 1;
  30. else if((a1.year==b1.year)&&(a1.month==b1.month)&&(a1.day>b1.day))
  31. return 1;
  32. else return 0;
  33. }
  34. bool operator == (const date a, const date b){
  35. return ((a.day==b.day)&&(a.month==b.month)&&(a.year==b.year));
  36. }
  37.  
  38. istream & operator>>(istream &in, date &a){
  39. int d, m, y;
  40. char c;
  41. in >> d >> c >> m >> c >> y;
  42. a = date(d, m, y);
  43. return in;
  44. }
  45.  
  46. ostream & operator<<(ostream &out, date &a){
  47. if(a.day<10)
  48. out << '0' << a.day << '.';
  49. else
  50. out << a.day << '.';
  51. if(a.month<10)
  52. out << '0' << a.month << '.';
  53. else
  54. out << a.month << '.';
  55. out << a.year;
  56. return out;
  57. }
  58.  
  59. date::~date(){
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. //Наша структура
  67. struct node
  68. {
  69. date info; //Информационное поле
  70. node *l, *r, *p;//Левая и Правая часть дерева
  71. };
  72.  
  73. node * tree=NULL; //Объявляем переменную, тип которой структура Дерево
  74.  
  75. /*ФУНКЦИЯ ЗАПИСИ ЭЛЕМЕНТА В БИНАРНОЕ ДЕРЕВО*/
  76. void push(date a,node **t)
  77. {
  78. if ((*t)==NULL) //Если дерева не существует
  79. {
  80. (*t)=new node; //Выделяем память
  81. (*t)->info=a; //Кладем в выделенное место аргумент a
  82. (*t)->l=(*t)->r=NULL; //Очищаем память для следующего роста
  83. return; //Заложили семечко, выходим
  84. }
  85. //Дерево есть
  86. if (a>(*t)->info) push(a,&(*t)->r); //Если аргумент а больше чем текущий элемент, кладем его вправо
  87. else push(a,&(*t)->l); //Иначе кладем его влево
  88. }
  89.  
  90. void minElement(node*t){
  91. while(t->l)
  92. t = t->l;
  93. cout << t->info;
  94. }
  95.  
  96. void secondElement(node*t, int i){
  97. if(!(t->l)&&!(t->r)){
  98. cout << "00.00.0000" << endl; return;
  99. }
  100. while(t->r->r){
  101. t=t->r;
  102. }
  103. if(t->r->l){
  104. cout << t->r->l->info << endl; return;
  105. }
  106. if(t->l)
  107. cout <<t->l->info << endl; return;
  108. }
  109.  
  110.  
  111.  
  112. int Node_Height(const node* p){
  113. int l, r, h = 0;
  114. if(p != NULL){
  115. l = Node_Height(p->l);
  116. r = Node_Height(p->r);
  117. h = ((l > r) ? l : r) + 1;
  118. }
  119. return h;
  120. }
  121.  
  122. void postOrderTravers(node* root) {
  123. if (root) {
  124. postOrderTravers(root->l);
  125. postOrderTravers(root->r);
  126. cout << root->info << ' ';
  127. }
  128. }
  129.  
  130. void growingPrint(node*Root){
  131. if(Root->l)
  132. growingPrint(Root->l);
  133. cout << Root->info << ' ';
  134. if(Root->r)
  135. growingPrint(Root->r);
  136.  
  137. }
  138.  
  139. void outLeaves(node*Root){
  140. if(Root->l)
  141. outLeaves(Root->l);
  142. if(!(Root->l)&& !(Root->r))
  143. cout << Root->info << ' ';
  144. if(Root->r)
  145. outLeaves(Root->r);
  146.  
  147. }
  148.  
  149. int maxDepth(node* root) {
  150. if (!root) {
  151. return 0;
  152. }
  153. return max(maxDepth(root->l), maxDepth(root->r))+1;
  154. }
  155.  
  156. int minDepth(node* root) {
  157. if (!root) {
  158. return 0;
  159. }
  160. return min(minDepth(root->l), minDepth(root->r))+1;
  161. }
  162.  
  163. void isBalanced(node* root) {
  164. if (maxDepth(root) - minDepth(root) <= 1)
  165. cout << "YES";
  166. else
  167. cout << "NO";
  168.  
  169. }
  170.  
  171. int main ()
  172. {
  173. date d;
  174. date end(00, 00, 0000);
  175. while(1){
  176. cin >> d;
  177. if(d==end)
  178. break;
  179. else
  180. push(d, &tree);
  181. }
  182. h=Node_Height(tree);
  183. cout << h << endl;//1
  184. minElement(tree);//2
  185. cout << endl;
  186. secondElement(tree, 0);//3 кажется, норм
  187. // cout << endl;
  188. growingPrint(tree);//4
  189. cout << endl;
  190. postOrderTravers(tree);//5
  191. cout << endl;
  192. outLeaves(tree);//6
  193. cout << endl;
  194. isBalanced(tree);//7, не работает
  195.  
  196. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:11: fatal error: 'conio.h' file not found
# include <conio.h>
          ^~~~~~~~~
1 error generated.
stdout
Standard output is empty