线索二叉树(先序,中序,后序)

建立二叉树 : 示例输入

ABDGK##L##H#M###CE##FINP##Q###JO#R###

三种序的结果应该是:

pre:A B D G K L H M C E F I N P Q J O R

in :K G L D H M B A E C P N Q I F O R J

pos:K L G M H D B E P Q N I R O J F C A



先序线索化  -> 按先序线索遍历

中序线索化  -> 按中序线索遍历

后序线索化  -> 按后序线索遍历


#include<iostream>
#define Child 0
#define Order 1
using namespace std;
struct Node {
    char d;
    int lg, rg;
    Node *l , *r;
};
Node *head , *pre;
void create(Node*& p){
    char t;
    cin>>t;
    if(t=='#'){
        p=NULL;
    }else {
        p = new Node ;
        p->lg = Child;
        p->rg = Child;
        p->d = t;
        create(p->l);
        create(p->r);
    }
}
void pre_thread(Node*p){
    if(!p) return ;
    if(!pre->r){
        pre->rg = Order;
        pre->r = p;
    }
    if(!p->l){
        p->lg = Order;
        p->l = pre;
    }
    pre = p;
    if(p->lg==Child) pre_thread(p->l);
    if(p->rg==Child) pre_thread(p->r);
}
void in_thread(Node*p){
    if(!p) return ;
    in_thread(p->l);
    if(!pre->r){
        pre->rg = Order;
        pre->r = p;
    }
    if(!p->l){
        p->lg = Order;
        p->l = pre;
    }
    pre = p;
    in_thread(p->r);
}
void pos_thread(Node *p){
    if(!p)return ;
    pos_thread(p->l);
    pos_thread(p->r);
    if(!pre->r){
        pre->rg = Order;
        pre->r = p;
    }
    if(!p->l){
        p->lg = Order;
        p->l = pre;
    }
    pre = p;
}

void thread(Node* root , int choice){
    if(!root) return;
    head = new Node;
    head->lg = Child ; head->l = root;
    head->rg = Order ; head->r = NULL;
    pre = head;
    if(choice<2){
        if(choice<1)pre_thread(root);
        else in_thread(root);
        pre ->rg = Order ; pre->r = head;
    }else pos_thread(root);
    head->r = pre;
}

void pre_visit(Node* head){
    Node *p = head->l;
    while(p!=head){
        cout<<p->d<<" ";
        if(p->lg==Child)
            p = p->l;
        else
            p = p->r;
    }
}
void in_visit(Node* head){
    Node* p = head->l;
    while(p!=head){
        while(p->lg == Child)
            p = p->l;
        cout<<p->d<<" ";
        while(p->rg==Order && p->r!=head){
            p=p->r;
            cout<<p->d<<" ";
        }
        p = p->r;
    }
}
Node* parent(Node* head , Node* p){
    Node* t = head;
    if(t->l == p) return head;
    t=t->l;
    while(t->l!=p && t->r!=p){
        if(t->rg==Child)
            t = t->r;
        else t = t->l;
    }
    return t;
}
void pos_visit(Node* head){
    Node *p=head->l , *par;
    while(1){
        while(p->lg == Child&&p->l)
            p=p->l;
        if(p->rg==Child&&p->r)
            p=p->r;
        else break;
    }

    while(p!=head){
        cout<<p->d<<" ";
        par = parent(head,p);
        if(par->r!=p && par->rg==Child){
            while(par->rg==Child){
                par = par->r;
                while(par->lg ==Child)
                    par=par->l;
            }
        }
        p = par;
    }
}

void init(Node*p){
    if(!p) return ;
    if(p->lg==Child)
        init(p->l);
    else{
        p->lg = 0;
        p->l = NULL;
    }
    if(p->rg==Child)
        init(p->r);
    else {
        p->rg = 0;
        p->r = NULL;
    }
}

int main(){
    int choice;
    Node* root;
    do{
        cout<<"按先序建立二叉树,#表空子树:\n>>";
        create(root);
    }while(!root);
    cout<<"输入0,1,2分别表示先序,中序,后序线索化\n>>";
    while(cin>>choice){
        thread(root,choice);
        if(choice<1)
            pre_visit(head);
        else if(choice==1)
            in_visit(head);
        else
            pos_visit(head);
        cout<<endl;
        init(root);
    }
    return 0;
}

你可能感兴趣的:(线索二叉树,前序线索,中序线索,先序线索)