前序線索二叉樹


 

1.遍歷前序線索二叉樹

void preTraverse(ThrBiTree head){
    ThrBiTree p=head->lChild;
    while(p!=head){
        visit(p->data);
        while(p->lTag==0){
            p=p->lChild;
            visit(p->data);
        }
        while(p->rTag==1&&p->rChild!=head){
            p=p->rChild;
            visit(p->data);
        }
        if(p->lTag==0) p=p->lChild;
        else p=p->rChild;
    }
}

如果當前結點有左孩子,則應該更新p指向左孩子,否則指向右孩子,這與中序遍歷線索二叉樹不同,中序遍歷時,能夠確定當前結點的左子樹一定被訪問過了,直接更新p指向右孩子 

 

2.前序線索化二叉樹

void preThreading(ThrBiTree p){
    if(p){
        if(!p->lChild){
            p->lTag=1;
            p->lChild=pre;
        }//這里不應該加else p->lTag=0;//如果在這里加一個else p->lTag=0;就會報錯 ,因為當線索化右子樹回到雙親時,該雙親已經被線索過了,右指針肯定不空,如果右指針為線索,而又修改rTag=0,則遍歷的時候后無法繼續 if(!pre->rChild){
            pre->rTag=1;
            pre->rChild=p;
        }//同理,不應該加else pre->rTag=0
        pre=p;
        if(p->lTag==0) preThreading(p->lChild);
        if(p->rTag==0) preThreading(p->rChild);
    }
}

bool preThreadingBiTree(ThrBiTree T,ThrBiTree &head){
    if(!(head=new ThrBiTNode)) return false;
    head->lTag=0;
    head->rTag=1;
    head->rChild=head;
    if(!T) head->lChild=head;
    else{
        pre=head;
        head->lChild=T;
        preThreading(T);
        pre->rChild=head;
        head->rChild=pre;
        head->rTag=1;
    }
    return true;
}

 

3.刪除線索二叉樹所占空間

void deleteThrBiTree(ThrBiTree &head){
    ThrBiTree p=head->lChild,q;
    while(p!=head){
        q=p;
        while(p->lTag==0){
            p=p->lChild;
            delete q;
            q=p;
        }
        while(p->rTag==1&&p->rChild!=head){
            p=p->rChild;
            delete q;
            q=p;
        }
        q=p;
        p=p->rChild;
        delete q;    
    }
    delete head;
    head=NULL;
}

 

測試:

#include<iostream>
using namespace std;

typedef struct BiTNode{
    int data;
    BiTNode *lChild,*rChild;
    int lTag,rTag;
}ThrBiTNode,*ThrBiTree;

ThrBiTree pre;

void visit(int a){
    cout<<a<<" ";
}bool preAndInCreateBiTree(ThrBiTree &p,int *preOrder,int *inOrder,int length){
    if(length>0){
        if(!(p=new ThrBiTNode)) return false;
        p->data=preOrder[0];
        p->lTag=0;
        p->rTag=0;
        int i;
        for(i=0;i<length&&inOrder[i]!=preOrder[0];++i);
        preAndInCreateBiTree(p->lChild,preOrder+1,inOrder,i);
        preAndInCreateBiTree(p->rChild,preOrder+i+1,inOrder+i+1,length-i-1);    
    }else{
        p=NULL;
    }
    return true;
}

int main(){
    int n,*preOrder,*inOrder;
    
    cout<<"Input the number of nodes : ";
    cin>>n;
    
    preOrder=new int[n];
    inOrder=new int[n];
    cout<<"Input the pre-order sequence : "<<endl;
    for(int i=0;i<n;++i) cin>>preOrder[i];
    cout<<"Input the in-order sequence : "<<endl;
    for(int i=0;i<n;++i) cin>>inOrder[i];
    
    ThrBiTree T;
    ThrBiTree head;
    if(!preAndInCreateBiTree(T,preOrder,inOrder,n)){
        cout<<"Fail to construct this tree"<<endl;
    }else{
        preThreadingBiTree(T,head);
        cout<<"Pre-traverse:"<<endl;
        preTraverse(head);    
    }
    deleteThrBiTree(head);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM