一、根据给定字符串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);
}