N
nikblow
Написал код для создания и отображения дерева (еле еле получилось)
теперь мне нужно очистить память из под каждого элемента. Я так понимаю мне нужно написать функцию, которая будет проходит дерево до конца, обнулять последние элементы и очищать память, далее вызываться рекурсивно, чтобы очистить таким образом всё дерево
Я не могу понять, что написать, чтобы пройти дерево до конца. Та же проблема была с выводом, поэтому впихнул его в функцию создания, работает. С очисткой всё сложнее
если возможно напишите пример кода =/ на словах очень сложно понять
Попробовал создать процедуру очистки. Программа работает, но не знаю очищается ли память, подскажите правильно ли? И если нет, то что переделать
Программирую в среде xcode и VS2012
C++:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
} tree_1;
void tree_go(struct tree *tree_1, int max_level, int current)
{
tree_1=(struct tree*)malloc(sizeof(struct tree));
tree_1->data=1+rand()%20;
printf("\nlevel %d: %d", current, tree_1->data);
if(current==max_level)
{
tree_1->left=0;
tree_1->right=0;
}
else
{
tree_1->left=(struct tree*)malloc(sizeof(struct tree));
tree_go(tree_1->left, max_level, current+1);
tree_1->right=(struct tree*)malloc(sizeof(struct tree));
tree_go(tree_1->right, max_level, current+1);
}
}
void clear(struct tree *tree_1, int max_level, int current)
{
}
int main()
{
srand(time(NULL));
struct tree *data;
int level=0;
printf("\nPlease type max level your tree: ");
scanf("%d", &level);
data=(struct tree*)malloc(sizeof(struct tree));
tree_go(data, level, 1);
// clear tree
printf("\n\nThe end");
return 0;
}
теперь мне нужно очистить память из под каждого элемента. Я так понимаю мне нужно написать функцию, которая будет проходит дерево до конца, обнулять последние элементы и очищать память, далее вызываться рекурсивно, чтобы очистить таким образом всё дерево
Я не могу понять, что написать, чтобы пройти дерево до конца. Та же проблема была с выводом, поэтому впихнул его в функцию создания, работает. С очисткой всё сложнее
если возможно напишите пример кода =/ на словах очень сложно понять
Попробовал создать процедуру очистки. Программа работает, но не знаю очищается ли память, подскажите правильно ли? И если нет, то что переделать
C++:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
} tree_1;
void tree_go(struct tree *tree_1, int max_level, int current, int summ)
{
tree_1=(struct tree*)malloc(sizeof(struct tree));
tree_1->data=1+rand()%20;
printf("\nlevel %d: %d", current, tree_1->data);
if(current==max_level)
{
tree_1->left=NULL;
tree_1->right=NULL;
}
else
{
tree_1->left=(struct tree*)malloc(sizeof(struct tree));
tree_go(tree_1->left, max_level, current+1, summ);
tree_1->right=(struct tree*)malloc(sizeof(struct tree));
tree_go(tree_1->right, max_level, current+1, summ);
}
}
void clear(struct tree *tree_1, int max_level, int current)
{
if(current==max_level)
{
free(&tree_1->data);
}
else
{
clear(tree_1->left, max_level, current-1);
clear(tree_1->right, max_level, current-1);
}
}
int main()
{
srand(time(NULL));
struct tree *data;
int level=0, summ=0;
printf("\nPlease type max level your tree: "),
scanf("%d", &level);
printf("\nWhat level summ:");
data=(struct tree*)malloc(sizeof(struct tree));
tree_go(data, level, 1, summ);
clear(data, level, level);
printf("\n\nThe end");
free(data);
return 0;
}
Программирую в среде xcode и VS2012