线索二叉树
线索二叉树
节点
struct node
{
int val;
node *left,*right;
bool lnode,rnode;
node(){left = NULL,right = NULL,lnode = false,rnode = false;}
};
构造
void set_link(node *p,node *&pre)
{
if(p != NULL)
{
set_link(p->left,pre);
if(p->left == NULL){
p->left = pre;
p->lnode = true;
}
//printf("%d\n",p->val);
if(pre!=NULL&&pre->right == NULL)
{
pre->right = p;
pre->rnode = true;
}
pre = p;
set_link(p->right,pre);
}
}
void Creat(node *T)
{
node *pre = NULL;
if(T!=NULL)
{
set_link(T,pre);
pre->right = NULL;
pre->rnode = true;
}
}
求中序二叉树的第一个节点
node *Firstnode(node *p)
{
while(!p->lnode) p = p->left;
return p;
}
求中序线索二叉树中节点p的后继节点
node *Nextnode(node *p)
{
if(!p->rnode)
return Firstnode(p->right);
return p->right;
}
油!油!you@