remove duplicates in binary balanced tree. tree traversal - preorder

15 Views Asked by At

i cannot remove duplicates from balanced tree, the function working incorrectly, i tried many varietes of it, but tests are wrong

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_set>

using namespace std;

struct Node
{
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* createBalTree(vector<int>& nums) {
    queue<Node*> q;
    int n = nums.size();
    if (n == 0)
        return nullptr;

    Node* root = new Node(nums[0]);

    q.push(root);

    int i = 1;
    while (!q.empty() && i < n) {
        int size = q.size();

        for (int j = 0; j < size && i < n; j++) {

            Node* current = q.front();
            q.pop();

            if (i < n) {
                current->left = new Node(nums[i]);
                q.push(current->left);
                i++;
            }

            if (i < n) {
                current->right = new Node(nums[i]);
                q.push(current->right);
                i++;
            }
        }
    }

    return root;
}

void saveToFile(Node* root, ofstream& file)
{
    if (root == nullptr)
    {
        return;
    }
    file << root->val << " ";// Записываем значение корня в файл
    saveToFile(root->left, file);
    saveToFile(root->right, file);
}

void deleteTree(Node* root)
{
    if (root == nullptr)
    {
        return;
    }
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}
void deleteDuplicates(Node* root) {
    if (root == nullptr) {
        return;
    }

    if (root->left != nullptr && root->left->val == root->val) {
        Node* temp = root->left;
        root->left = temp->left;
        delete temp;
        deleteDuplicates(root); // Рекурсивный вызов, чтобы удалить дубликаты, которые могли появиться после удаления
    }

    if (root->right != nullptr && root->right->val == root->val) {
        Node* temp = root->right;
        root->right = temp->right;
        delete temp;
        deleteDuplicates(root); // Рекурсивный вызов, чтобы удалить дубликаты, которые могли появиться после удаления
    }

    deleteDuplicates(root->left); // Рекурсивный вызов для левого поддерева
    deleteDuplicates(root->right); // Рекурсивный вызов для правого поддерева
}
int main()
{
    vector<int> numbers;
    ifstream inputFile("input.txt");
    int number;
    while (inputFile >> number)
    {
        numbers.push_back(number);
    }
    inputFile.close();
    Node* root = nullptr;
    root = createBalTree(numbers);
    deleteDuplicates(root);

    ofstream outputFile("output.txt");
    saveToFile(root, outputFile);
    outputFile.close();
    cout << endl;

    deleteTree(root);
    return 0;
}

in my code i delete duplicates using recursion,Task: Balanced tree. Procedure: Delete all repetitions. Tree traversal: Straight.In any incomprehensible situation, refer first to the "left". Example:

  • for the search tree. If the number is greater than the root element – "to the right", otherwise (less than or equal to) – "to the left",
  • for a balanced tree – first add the element "to the left". Some examples of tests Input 11 12 23 434 434 192 23 12 12 output 11 12 434 192 23 my out 11 12 434 12 12 434 23 192 Input 0 1 0 1 0 1 0 1 0 output 0 1 my out 0 1 0
0

There are 0 best solutions below