線索二叉樹的創建,中序線索化和中序遍歷(嚴蔚敏版數據結構實現)


#include<stdio.h>

#include<iostream>

#include<cstdio>

using namespace std;

 

#define OK 1

#define ERROR 0

#define OVERFLOW -1

 

typedef int Status;

typedef char TElemType;

 

/*線索二叉樹的結構*/

typedef enum PointerTag{Link,Thread};//Link==0,指針;Thread==1,線索

typedef struct BiThrNode

{

        TElemType data;    //結點數據

        PointerTag ltag, rtag;//左右標志

        struct BiThrNode *lchild,*rchild;//左右孩子指針

}BiThrNode,*BiThrTree;

 

Status PrintElement(TElemType e)// 輸出結點里面的值

{

        cout << e;

        return OK;

}

 

// 創建線索二叉樹

Status CreateBiThrTree(BiThrTree &T)

{

        char ch;

        cin >> ch;

        if (ch == '!')

                 T = NULL;

        else

        {

                 if (!(T = (BiThrNode *)malloc(sizeof(BiThrNode))))

                         exit(OVERFLOW);

                 T->data = ch;

                 T->ltag = Link;

                 T->rtag = Link;

                 CreateBiThrTree(T->lchild);

                 CreateBiThrTree(T->rchild);

        }

        return OK;

}

 

// T指向頭結點,頭結點的lchild指向根節點,中序遍歷線索二叉樹

Status InOrderTraverse_Thr(BiThrTree T, Status(*Visit)(TElemType e))

{

        BiThrTree p = T->lchild;

        while (p&&(p != T))

        {

                 while (p->ltag == Link)

                 {                      

                         p = p->lchild;

                 }

                 Visit(p->data);

                 while (p->rtag == Thread && p->rchild != T)

                 {

                         p = p->rchild;

                         Visit(p->data);

                 }

                 p = p->rchild;

        }

        return OK;

}

 

 

// 中序遍歷進行二叉樹線索化

void InThreading(BiThrTree p,BiThrTree &pre)

 

{

        if (p)

        {

                 InThreading(p->lchild,pre);//左子樹線索化

                 if (!p->lchild)

                 {

                         p->ltag = Thread;

                         p->lchild = pre;//前驅線索

                 }

                 if ((!pre->rchild)&&pre->rchild==NULL)

                 {

                         pre->rtag = Thread;

                         pre->rchild = p;//后繼線索

                 }

                 pre = p;

                 InThreading(p->rchild, pre);//右子樹線索化

        }

}

 

//中序遍歷並線索化二叉樹T,Thrt指向頭結點

Status InOrderThreading(BiThrTree &Thrt, BiThrTree T)

{

        if (!(Thrt = (BiThrTree)malloc(sizeof(BiThrNode))))   //建立頭結點

                 exit(OVERFLOW);

        Thrt->ltag = Link;

        Thrt->rtag = Thread;

        Thrt->rchild = Thrt;//右指針回指

        BiThrTree pre;

        if (!T)

                 Thrt->lchild = Thrt;//若二叉樹為空,則左指針回指

        else

        {

                 Thrt->lchild = T;

                 pre = Thrt;

                 InThreading(T,pre);//中序遍歷進行中序線索化

                 pre->rchild = Thrt;//最后一個結點線索化

                 pre->rtag = Thread;

                 Thrt->rchild = pre;

        }

        return OK;

}

 

 

 

void main(){

        BiThrTree T, Thrt;

        cout << "創建線索二叉樹,按先序次序輸入線索二叉樹中結點的值:\n";

        CreateBiThrTree(T);

        if (InOrderThreading(Thrt, T) == OK)

                 cout << "成功建立中序線索化鏈表!\n";

        cout << "中序遍歷線索二叉樹,結果是:\n";

        InOrderTraverse_Thr(Thrt, PrintElement);

        cout << endl;

}

 


免責聲明!

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



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