- 后序线索二叉树
void postThread(TBTNode *p,TBTNode *&pre)
{
if(p != NULL)
{
postThread(p->lChild,pre);
postThread(p->rChild, pre);
if(p->lChild == NULL)
{
p->lChild = pre;
p->lTag = 1;
}
if(pre != NULL&&pre->rChild == NULL)
{
pre->rChild = p;
pre->rTag = 1;
}
pre = p;
}
}
在后序线索二叉树中如何找一个结点的后继:
- 若结点X是二叉树的根,则其后继为空;
- 若结点X是其双亲的右孩子,或是其双亲的左孩子且其双亲没有右子树,则其后继即为双亲结点;
- 若结点X是其双亲的左孩子,且其双亲有右子树,则其后继为双亲右子树上按后序遍历列出的第一个结点。
- 三种线索二叉树的比较
9. 二叉树的构造和确定
仅由先序序列、中序序列和后序序列中的任何一个无法确定这棵二叉树的树形。但是,如果同时知道了一棵二叉树的先序序列和中序序列,或者同时知道了中序序列和后序序列,就能确定这棵二叉树。
BTNode *CreateBT(char pre[],char in[], int L1, int R1, int L2, int R2)
{
if(L1 > R1)
return NULL;
BTNode *s = (BTNode *)malloc(sizeof(BTNode));
s->lChild = s->rChild = NULL;
s->data = pre[L1];
int i;
for(i = L2;i <= R2; ++i)
if(in[i] == pre[L1])
break;
s->lChild = CreateBT(pre,in, L1+1, L1+i-L2, L2,i-1);
s->rChild = CreateBT(pre,in, L1+i-L2+1,R1,i+l,R2);
return s;
}