简体   繁体   English

在 C 中使用递归创建二叉树

[英]Creating a binary tree with recursion in C

I tried to use recursion to create a binary tree, but when I type ABD***CE**FG*** , the code didn't yield any result.我尝试使用递归来创建二叉树,但是当我输入ABD***CE**FG*** ,代码没有产生任何结果。 I pressed space key but the code still didn't half.我按了空格键,但代码仍然没有减半。 Was the code wrong or my input wrong?是代码错误还是我输入错误?

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

typedef struct tree
{
    struct tree *left;
    struct tree *right;
    char val;
}treeNode;
void createTree(treeNode **node)
{
    char value=0;
    value=getchar();
    if(value=='*')
        *node=NULL;
    else
    {
        *node = (treeNode*)malloc(sizeof(treeNode));
        if(!*node)  exit(-1);
        (*node)->val=value;
        createTree(&(*node)->left);
        createTree(&(*node)->right);
    }

}
void preOrder(treeNode *node)
{
    if (node==NULL) return;
    putchar(node->val);
    preOrder(node->left);
    preOrder(node->right);
}
int main() {
    // insert code here...
    treeNode **node;
    createTree(node);
    preOrder(*node);
    return 0;
}
int main() { // insert code here... treeNode **node; createTree(node); preOrder(*node); return 0; }

must be必须是

int main() {
    treeNode *node;
    createTree(&node);
    preOrder(node);
    return 0;
}

else in createTree *node = ... write in a non valid location (*node is not set to a valid pointer in main ) else in createTree *node = ...在无效位置写入(*node 未设置为main 中的有效指针)

your input must be ABD***CEFG***** to finish :您的输入必须是ABD***CEFG*****才能完成:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra b.c
pi@raspberrypi:/tmp $ ./a.out
ABD***CEFG******
ABDCEFGpi@raspberrypi:/tmp $ 

About your remark :关于您的评论:

yes i don't know which one is left which is right是的,我不知道哪个是左哪个是右

a practical way is to draw the tree.一种实用的方法是绘制树。

A very simple way is :一个非常简单的方法是:

void draw(treeNode *node, int level, char empty)
{
  if (node != NULL) {
    draw(node->right, level+1, '/');
    for (int i = 0; i != level; ++i)
      putchar(' ');
    printf("%c\n", node->val);
    draw(node->left, level+1, '\\');
  }
  else {
    for (int i = 0; i != level; ++i)
      putchar(' ');
    printf("%c\n", empty);
  }
}

if I change main to :如果我将 main 更改为:

int main() {
    treeNode *node;
    createTree(&node);
    preOrder(node);
    putchar('\n');
    draw(node, 1, ' ');
    return 0;
}

Compilation and execution :编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra b.c
pi@raspberrypi:/tmp $ ./a.out
ABD***CEFG*****
ABDCEFG
   /
  C
    /
   E
     /
    F
      /
     G
      \
 A
   /
  B
    /
   D
    \

The '/' indicates there is nothing on the right and the '\\' indicates there is nothing on the left '/' 表示右边什么都没有,'\\' 表示左边什么都没有


[edit] Some ways to draw a prettiest tree can be found on C How to “draw” a Binary Tree to the console [编辑] 可以在C 如何将二叉树“绘制”到控制台上找到一些绘制最漂亮树的方法


I did a mistake on the input, if I use yours being ABD***CE**FG*** the result is :我在输入上犯了一个错误,如果我使用你的ABD***CE**FG***结果是:

/tmp % ./a.out
ABD***CE**FG***
ABDCEFG
    /
   F
     /
    G
     \
  C
    /
   E
    \
 A
   /
  B
    /
   D
    \

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM