程序填空题:递归建立和递归中序遍历二叉树
本题要求递归建立和遍历二叉树
```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)
```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)