中序线索链表构造函数算法InThrBiTree由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“链表冒泡排序算法”。
template
InThrBiTree::InThrBiTree(ThrNode *root){
Creat(root);
pre=NULL;
ThrBiTree(root);
}
template
void InThrBiTree ::Creat(ThrNode *root){
cin>>ch;
if(ch=='# ')root=NULL;//建立一棵空树else {
root=new ThrNode;//生成一个结点,左右标志均置0root->data=ch;root->ltag=0;root->rtag=0;Creat(root->lchild);//递归建立左子树Creat(root->rchild);//递归建立右子树}
}
template
void InThrBiTree ::ThrBiTree(ThrNode *root){
if(root==NULL)return;
ThrBiTree(root->lchild);
if(!root->lchild){//对root的左指针进行处理
root->ltag=1;
root->lchild=pre;//设置pre的前驱线索
}
if(!root->rchild)root->rtag=1;//对root的右指针进行处理if(pre->rtag==1)pre->rchild=root;//设置pre的后继线索pre=root;
ThrBiTree(root->rchild);
}