-->
当前位置:首页 > 题库 > 正文内容

程序填空题:递归建立和递归中序遍历二叉树

Luz4年前 (2021-05-10)题库2875
本题要求递归建立和遍历二叉树

```c
#include
#include
typedef char DataType;
typedef struct BTreeNode
{
DataType data;
struct BTreeNode *leftchild;
struct BTreeNode *rightchild;
}BinTreeNode;
typedef BinTreeNode *BinTree;


BinTree CreateBinTree_Recursion()
{
char ch;
BinTree bt;
scanf("%c", &ch);
if (ch == '@')
@@[bt = NULL](2);
else
{
bt = (BinTreeNode *)malloc(sizeof(BinTreeNode));
@@[bt->data = ch](2);
@@[bt->leftchild = CreateBinTree_Recursion()](2);
@@[bt->rightchild = CreateBinTree_Recursion()](2);
}
@@[return bt](2);
}

void InOrder_Recursion(BinTree bt)
{
if (@@[bt == NULL](2)) return;
@@[InOrder_Recursion(bt->leftchild)](2);
printf("%c", bt->data);
@@[InOrder_Recursion(bt->rightchild)](2);
}

void DestroyBinTree(BinTree bt)
{
if (bt != NULL)
{
DestroyBinTree(bt->leftchild);
DestroyBinTree(bt->rightchild);
free(bt);
}
}

int main()
{
BinTree bt = NULL;
bt = CreateBinTree_Recursion();
InOrder_Recursion(bt);
DestroyBinTree(bt);
return 0;
}
```






答案:
第1空:bt = NULL

第2空:bt->data = ch

第3空:bt->leftchild = CreateBinTree_Recursion()

第4空:bt->rightchild = CreateBinTree_Recursion()

第5空:return bt

第6空:bt == NULL

第7空:InOrder_Recursion(bt->leftchild)

第8空:InOrder_Recursion(bt->rightchild)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。