在中午
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
BiThrTree pre;
enum PointTag
{
link, thread//link左右孩子指针,thread前驱后继线索
};
//二叉树结构
typedef struct BiThrNode
{
ElemType data;
PointTag ltag;
PointTag rtag;
BiThrNode* lchild;
BiThrNode* rchild;
}BiThrNode, * BiThrTree;
//二叉树的建立
void CreatBiThrTree(BiThrTree& T)
{
ElemType c;
scanf_s("%c", &c);
if (c == ' ')
{
T = nullptr;
}
else
{
T = (BiThrTree)malloc(sizeof(BiThrNode));
T->data = c;
T->ltag = link;
T->rtag = link;
CreatBiThrTree(T->lchild);
CreatBiThrTree(T->rchild);
}
}
//二叉树的线索化1,中序遍历
void InThreading(BiThrTree T)
{
if (T)
{
InThreading(T->lchild);
if (T->lchild == nullptr)
{
T->ltag = thread;
T->lchild = pre;
}
if (pre->rchild == nullptr)
{
pre->rtag = thread;
pre->rchild = T;
}
pre = T;
InThreading(T->rchild);
}
}
//二叉树的线索化2
void InOrderThreading(BiThrTree& T, BiThrTree p)
{
p = (BiThrTree)malloc(sizeof(BiThrNode));
p->ltag = link;
p->rtag = thread;
p->rchild = p;
if (T != nullptr)
{
p->lchild = T;
pre = p;
InThreading(T);
pre->rchild = p;
pre->rtag = thread;
p->rchild = pre;
}
}
int main()
{
BiThrTree T = nullptr, p;
CreatBiThrTree(T);
InOrderThreading(T, p);
return 0;
}
报了什么错?
结题了,我把全局变量写在结构前了