Копирование дерева

и снова привет. в общем, нужно записать копию дерева, но с другим ключом. при записи консоль вместо элементов выводит нули, а на остальных позициях клонирует один узел. прошу указать на ошибку

#include <iostream>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include "Windows.h"

using namespace std;


struct node
{
    double score;  
    string student;
    int age;
    node* l, * r;                       
}base;

node* tree = NULL;   

struct node2
{
    double score;
    string student;
    int age;
    node* l, * r;
}base2;

node2* tree2 = NULL;


void push(node** t)
{
    if ((*t) == NULL)                   
    {
        (*t) = new node;               
        (*t)->student = base.student;
        (*t)->age = base.age;
        (*t)->score = base.score;    
        (*t)->l = (*t)->r = NULL;       
        return;                         
    }

    if (base.student > (*t)->student) push(&(*t)->r); 
    else push(&(*t)->l);         
}

void push2(node2** t)
{
    if ((*t) == NULL)
    {
        (*t) = new node2;
        (*t)->student = base2.student;
        (*t)->age = base2.age;
        (*t)->score = base2.score;
        (*t)->l = (*t)->r = NULL;
        return;
    }

    if (base2.score > (*t)->score) push(&(*t)->r);
    else push(&(*t)->l);
}




void del_all(node*& t)
{
    if (!t) return;
    del_all(t->l);
    del_all(t->r);
    delete t;
    t = NULL;

}

void del_all2(node2*& t)
{
    if (!t) return;
    del_all(t->l);
    del_all(t->r);
    delete t;
    t = NULL;

}



double avg(node* t, int n)
{
    if (!t) return 0;
    return avg(t->l, n) + avg(t->r, n) + t->score / n;
}


node* Copy(node* t, int n) {
    node* new_root;
    if (t != NULL) {
        new_root = new node;
        new_root->student = t->student;
        new_root->age = t->age;
        new_root->score = t->score;
        new_root->l = Copy(t->l, n);
        new_root->r = Copy(t->r, n);
    }
    else return NULL;
    return new_root;
}

void Print_Tree(node** tree, int l)
{

    if (*tree != NULL)
    {
        Print_Tree(&((**tree).r), l + 2);
        for (int i = 1; i <= l; i++) cout << " ";
        cout << (**tree).student << " " << (**tree).age << " " << (**tree).score << endl;
        Print_Tree(&((**tree).l), l + 2);
    }

}

void Print_Tree2(node2** tree2, int l)
{

    if (*tree2 != NULL)
    {
        Print_Tree(&((**tree2).r), l + 2);
        for (int i = 1; i <= l; i++) cout << " ";
        cout << (**tree2).student << " " << (**tree2).age << " " << (**tree2).score << endl;
        Print_Tree(&((**tree2).l), l + 2);
    }

}

int main()
{
    int n, x;
    double avrage;
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);




    cout << "Задайте кол-во элементов: ";
    cin >> n;                           
    for (int i = 0; i < n; ++i)
    {
        cout << "Фамилия: "; cin >> base.student; cout << "Возраст: "; cin >> base.age; cout << "Оценка: "; cin >> base.score;
        push(&tree);
    }

    cout << "Ваше дерево:\n";
    Print_Tree(&tree, 0);



    do
    {
        cout << "1. Среднее арифметическое поля" << endl;
        cout << "2. Удалить дерево" << endl;
        cout << "3. Переписать дерево" << endl;
        cout << "0. Выйти" << endl;
        cout << "\nНомер операции: "; cin >> x;
        switch (x)
        {
        case 1:
            cout << "Среднее значение поля score в дереве: ";
            avrage = avg(tree, n);
            cout << avrage << endl;
            break;
        case 2:
            del_all(tree);
            del_all2(tree2);
            cout << "Дерево удалено!";
            Print_Tree(&tree, 0); break;
        case 3: 
                Copy(tree, n);
                push2(&tree2);
             Print_Tree2(&tree2, 0);  break;

        }
    } while (x != 0);

    del_all(tree);

}

элементы, что создались(3 поля структуры):

   e 9 9
      d 8 8
    c 7 7
  b 6 6
a 5 5

что выводит после копирования:

 0 0
  e 9 9
    e 9 9
      e 9 9
        e 9 9

Ответы (1 шт):

Автор решения: goldstar_labs
    case 3:
        Copy(tree, n);
        push2(&tree2);
        Print_Tree2(&tree2, 0);  break;

Ошибка в этом месте: 1) Функция Copy возвращает указатель на копию переданного в неё дерева, вы возвращаемое значение игнорируете и получаете утечку памяти, наверное надо как-то так:

    case 3:
        node* copy = Copy(tree, n);
        Print_Tree(&copy, 0); 
        delete copy;
        break;

Upd: вместо delete copy; надо вызывать del_all(copy);

→ Ссылка