-3

我的目标是:当我输入“ecdgaf b”时,输出是

e

(\t) c                <- first left node

(\t)(\t) a            <- second left node

(\t)(\t)(\t) b        <- third right node

(\t)(\t) d            <- second right node

(\t) g                <- first right node

(\t)(\t) f            <- second left node

但我无法让它按预期工作。

这是 Brian W. Kernighan 和 Dennis M. Ritchie 的“C 编程语言”中的一个示例。

我改变了我的功能,我如何在输出窗口中看到 \t。

我的尝试:

void prt(AW *p)
{
int j;
int temp;

if(p != NULL)
{   
          temp = judge;

    while(1)
    {   temp = temp / 2;
        count++;
        if(temp == 0)
            break;
    }

    for(j=1; j<count; j++)
        putchar('\t');
    printf("%-16s\n",p->word);

    count=0;

    if(p->left != NULL)judge++;
    prt(p->left);       

    if(p->right != NULL)judge++;
    prt(p->right);      }

}

4

1 回答 1

3

假设你有下面这样的结构。

typedef struct
{
int data;
struct node * left;
struct node *right;
} node; 



 void printtreenode(node *root)
    {
    if(root == NULL)
      return;
    printtreenode(root -> left);
    printf("%d\n",root -> data);
    printtreenode(root ->right);
    }

无需将 2 个参数传递给printtreenode并查看它也是递归的。这应该可以工作,您可以根据格式化的输出对其进行修改。

于 2012-11-26T10:13:49.150 回答