Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //ПЕРВАЯ ЗАДАЧА
- struct Node {
- Node* left;
- Node* right;
- int info;
- };
- void AddLeft(Node* node, int data) {
- node->left = new Node();
- node->left->info = data;
- }
- void AddRight(Node* node, int data) {
- node->right = new Node();
- node->right->info = data;
- }
- void AddElement(Node* node, int data, int direction) {
- if (direction == 1) {
- AddLeft(node, data);
- }
- else {
- AddRight(node, data);
- }
- }
- //ВТОРАЯ ЗАДАЧА
- void print(Node* node, const std::string& prefix = "")
- {
- if (node != nullptr)
- {
- std::cout << prefix << " ";
- std::cout << node->info << std::endl;
- print(node->left, prefix + " ");
- print(node->right, prefix + " ");
- }
- }
- //Третья задача
- void GetOddEvenCount(Node* node, int& odd, int& even) {
- if (node->info % 2 == 0) {
- even++;
- }
- else {
- odd++;
- }
- if (node->left != nullptr) {
- GetOddEvenCount(node->left, odd, even);
- }
- if (node->right != nullptr) {
- GetOddEvenCount(node->right, odd, even);
- }
- }
- int main()
- {
- Node* root = new Node();
- root->info = 1;
- AddElement(root, 2, 1);
- AddElement(root, 3, 0);
- AddElement(root->right, 4, 1);
- AddElement(root->right, 5, 0);
- AddElement(root->right->right, 6, 1);
- AddElement(root->right->right, 7, 0);
- print(root);
- int odd = 0;
- int even = 0;
- GetOddEvenCount(root, odd, even);
- std::cout << std::endl << odd << " " << even << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement