Advertisement
TvoiNikto

Untitled

Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1.  
  2. #include <cstring>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. template<class T> class node {
  8. public:
  9. T data;
  10. node<T>* left;
  11. node<T>* right;
  12.  
  13. node() {
  14. left = NULL;
  15. right = NULL;
  16. }
  17. };
  18.  
  19.  
  20. template<class T> class tree
  21. {
  22.  
  23. private:
  24. node<T>* root;
  25.  
  26. void push(node<T>* parent, T& obj) {
  27. if (parent->data < obj ) {
  28. if (parent->left == NULL) {
  29. parent->left = new node<T>();
  30. parent->left->data = obj;
  31. }
  32. else {
  33. push(parent->left, obj);
  34. }
  35. }
  36. else {
  37. if (parent->right == NULL) {
  38. parent->right = new node<T>();
  39. parent->right->data = obj;
  40. }
  41. else {
  42. push(parent->right, obj);
  43. }
  44. }
  45. }
  46.  
  47. void print(node<T>* parent){
  48. if (parent != NULL){
  49. print(parent->left);
  50. cout << parent->data << endl;
  51. print(parent->right);
  52. }
  53. }
  54.  
  55. public:
  56.  
  57. tree() {
  58. root = NULL;
  59. }
  60.  
  61. void push(T& obj) {
  62. if (root == NULL) {
  63. root = new node<T>();
  64. root->data = obj;
  65. }
  66. else {
  67. push(root, obj);
  68. }
  69. }
  70.  
  71. void print() {
  72. print(root);
  73. }
  74. };
  75.  
  76.  
  77.  
  78. class product{
  79. public:
  80. char name[32];
  81. float price;
  82.  
  83. product() {
  84.  
  85. }
  86.  
  87. product(product& p) {
  88. this->price = p.price;
  89. strcpy(this->name, p.name);
  90. }
  91.  
  92. product(char* n, float p) {
  93. strcpy(name, n);
  94. price = p;
  95. }
  96.  
  97.  
  98. /*Начало вашего кода*/
  99. //TODO: Пишите код тут
  100.  
  101. /*Конец вашего кода*/
  102. /*Памятка! Если вы стёрли строки доступные для редактирования - нажмите F5*/
  103.  
  104.  
  105. friend ostream& operator <<(ostream &os, product& p) {
  106. os << p.name;
  107. return os;
  108. }
  109. };
  110.  
  111.  
  112. int main()
  113. {
  114. product p1 ("Milk", 55.0f);
  115. product p2 ("Bread", 10.5f);
  116. product p3 ("Cheese", 300.0f);
  117. product p4 ("Beer", 70.0f);
  118. product p5 ("Potatoes", 20.0f);
  119.  
  120. tree<product> store;
  121. store.push(p1);
  122. store.push(p2);
  123. store.push(p3);
  124. store.push(p4);
  125. store.push(p5);
  126.  
  127. store.print();
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement