一、根据给定字符串str = "ABC##DE##F##G#H##"建立对应的二叉树(其中‘#’代表空)
1、树的节点结构为
typedef char ElementType;
typedef struct BTNode
{
BTNode* LeftNode;
BTNode* RightNode;
ElementType data;
}BTNode;
2、二叉树的建立
BTNode* BuyNode()
{
BTNode* s = (BTNode*)calloc(1,sizeof(BTNode));
if (s == nullptr)
{
exit(1);
}
return s;
}
BTNode* CreateBTree(const char*& str)
{
BTNode* s = nullptr;
if (*str != '#')
{
s = BuyNode();
s->data = *str;
s->LeftNode=CreateBTree(++str);
s->RightNode=CreateBTree(++str);
}
return s;
}
BTNode* BuildBTree(const char* & str)
{
if (str == nullptr || strlen(str) <= 0)return nullptr;
else return CreateBTree(str);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30