The task itself:

In a given non-empty binary tree, count the number of vertices at the nth level, counting the root as the vertex of the 0th level.

But since I just started to understand the trees. I decided to first write a few subroutines and just test them when working with wood.

At first I tried to add elements just from the array, but in general I need from the file. In both cases in the console for some reason SMILE enter image description here

I see this for the first time, so please help me find the error in the code.

Code:

#include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <conio.h> #include <locale.h> //дерево typedef struct tagTREE { int inf; struct tagTREE* left; struct tagTREE* right; }TREE; //очередь typedef struct tagQUEUE { TREE *tr; struct tagQUEUE *next; }QUEUE; QUEUE *q; //функция уничтожения очереди void DestroyQueue() { QUEUE *prev; if (q != NULL) { prev = q; while (q->next != NULL) { q = q->next; free(prev); prev = q; } free(prev); } } //функция добавления элемента в очередь void Put(TREE *a) { QUEUE *n, *prev; prev = q; n = malloc(sizeof(QUEUE)); n->tr = a; n->next = NULL; if (q != NULL) { while (prev->next != NULL) prev = prev->next; prev->next = n; } else q = n; } //функция удаления элемента из очереди TREE* Get() { QUEUE *prev; TREE *a; if (q != NULL) { prev = q; a = q->tr; if (q->next != NULL) q = q->next; else q = NULL; free(prev); return a; } else return 0; } //функция проверки на пустоту очереди int Empty(QUEUE *q) { if (q == NULL) return 1; else return NULL; } //----------------------------------------------- //функция добавления элемента в дерево TREE* Add(TREE *root, int x) { if (root == NULL) { root = malloc(sizeof(TREE)); root->inf = x; root->left = root->right = NULL; } else { if (x <= root->inf) root->left = Add(root->left, x); else root->right = Add(root->right, x); } return root; } //функция удаления элемента из дерева TREE* Del(TREE *root, int x) { TREE *t; if (root == NULL) return 0; if (x == root->inf) { if (root->left == NULL) { t = root->right; free(root); return t; } t = root->left; while (t->right) t = t->right; t->right = root->right; return root->left; } if (x <= root->inf) root->left = Del(root->left, x); else root->right = Del(root->right, x); return root; } //функция вывода дерева на экран //обходя дерево в ширину void ShowTree(TREE *root) { q = NULL; TREE *t; Put(root); while (!Empty(q)) { t = Get(); if (t->left != NULL) Put(t->left); if (t->right != NULL) Put(t->right); printf("%d ", t->inf); } puts(""); } //функция удаления всего дерева TREE* DestroyTree(TREE *root) { if (root) { if (root->left != NULL) root->left = DestroyTree(root->left); if (root->right != NULL) root->right = DestroyTree(root->right); free(root); } return NULL; } void main(void) { setlocale(LC_CTYPE,"Russian"); TREE *mytree = NULL; //указатель на дерево FILE *file; int k; fopen_s(&file, "hello.txt", "r"); if (file == NULL) printf("Ошибка при открытии файла"); else { printf("Дерево:"); while (fscanf_s(file, "%d", &k) != EOF) { mytree = Add(mytree, k); } } // было так, но вообще нужно читать элементы из файла // int mas[10] = { 5, 34, 8, 10, 15, 1, 0, 21, -5, 100 }; // for (i = 0; i<10; i++) // mytree = Add(mytree, mas[i]); // printf("Дерево:"); // ShowTree(mytree); system("cls"); //очищаем экран ShowTree(mytree); //выводим дерево //удаляем элементы mytree = Del(mytree, k); // критическая ошибка // mytree = Del(mytree, 1); пытался удалять элементы из массива // mytree = Del(mytree, -5); printf("Дерево после удаления элементов:"); ShowTree(mytree); //выводим дерево _getch(); } 
  • So what are the calls to puts and what do you expect from them at all? Clear mistake. - AnT
  • @AnT Yes, now I’ve fixed all the warnings, only these are with puts. Tupanul, now change. - yearstover
  • In your original version, it was correct - Put . And now for some reason you have fixed it on some meaningless calls to puts and printf . Obviously, the code is not yours. Stop changing the code - you absolutely do not understand what you are doing, i.e. make it worse. Bring it all back, as it was at the beginning. - AnT
  • @AnT ahahahah, now I understand how tough I am. Soriany for such a stupid question, the topic can be closed (and better to delete: D). - yearstover
  • @ Let it be better, instead of removing and closing, better write the answer - hedgehogues

0