Top.Mail.Ru
Ответы
Аватар пользователя
15лет
Изменено
Аватар пользователя
Аватар пользователя
Программирование
+1

Вывод бинарного дерева на чистом Си...

Дополнен

Вот структура, описывающая упорядоченное дерево
struct tree{
int data;
struct tree *l;
struct tree *r;
};
как можно вывести на экран в ступенчатом виде?

Дополнен

_____________________________________________
Иван Лузинов:
Выводится столбец, а не дерево. НЕобходимо вывести в ступенчатом виде для наглядности:
root
son-1
son-1.1
son-1.2
son-2

По дате
По рейтингу
Аватар пользователя
Новичок
15лет

рекурсия, задаешь отступ, при рекурсивном вызове отступ увеличиваешь
void PrintTree (node *pNode, int n)
{
if (pNode->pLeft) PrintTree(pNode->pLeft, n+1);

for (int i = 0; i < n; i++) prinf(" ");
prinf("%d\n", Node->Value);

if (pNode->pRight) PrintTree(pNode->pRight, n+1);
}

Аватар пользователя
Мыслитель
15лет

#include<stdlib.h>
#include<stdio.h>

struct tree_el {
int val;
struct tree_el * right, * left;
};

typedef struct tree_el node;

void insert(node ** tree, node * item) {
if(!(*tree)) {
*tree = item;
return;
}
if(item->val<(*tree)->val)
insert(&(*tree)->left, item);
else if(item->val>(*tree)->val)
insert(&(*tree)->right, item);
}

void printout(node * tree) {
if(tree->left) printout(tree->left);
printf("%d\n",tree->val);
if(tree->right) printout(tree->right);
}

void main() {
node * curr, * root;
int i;

root = NULL;

for(i=1;i<=10;i++) {
curr = (node *)malloc(sizeof(node));
curr->left = curr->right = NULL;
curr->val = rand();
insert(&root, curr);
}

printout(root);
}

Аватар пользователя
Искусственный Интеллект
15лет

ru.wikipedia.org/wiki/Двоичное_дерево_поиска