Κότ μάτρόϛκϰΗ
Просветленный
(21386)
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);
}
nameПрофи (832)
15 лет назад
спасибо, сделал так:
void ViewTree(struct tree *rt,int sp)
{
int i;
if(rt->l)
{
ViewTree(rt->l,++sp);
--sp;
}
for(i=0;i<=sp;i++)
MSPACE;
printf("%d\n",rt->data);
if(rt->r)
{
ViewTree(rt->r,++sp);
--sp;
}
}
чтобы отступы возвращались при выходе из этапа рекурсии
kepz
Мыслитель
(5471)
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);
}