基礎實驗4-2.3 二叉樹的非遞歸遍歷 (25分)


本題要求用非遞歸的方法實現對給定二叉樹的 3 種遍歷。

函數接口定義:

void InorderTraversal( BinTree BT ); void PreorderTraversal( BinTree BT ); void PostorderTraversal( BinTree BT ); 
 

其中BinTree結構定義如下:

typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; int flag; }; 
 

要求 3 個函數分別按照訪問順序打印出結點的內容,格式為一個空格跟着一個字符。

此外,裁判程序中給出了堆棧的全套操作,可以直接調用。

裁判測試程序樣例:

#include <stdio.h> #include <stdlib.h> typedef enum { false, true } bool; typedef char ElementType; typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; int flag; }; /*------堆棧的定義-------*/ typedef Position SElementType; typedef struct SNode *PtrToSNode; struct SNode { SElementType Data; PtrToSNode Next; }; typedef PtrToSNode Stack; /* 裁判實現,細節不表 */ Stack CreateStack(); bool IsEmpty( Stack S ); bool Push( Stack S, SElementType X ); SElementType Pop( Stack S ); /* 刪除並僅返回S的棧頂元素 */ SElementType Peek( Stack S );/* 僅返回S的棧頂元素 */ /*----堆棧的定義結束-----*/ BinTree CreateBinTree(); /* 裁判實現,細節不表 */ void InorderTraversal( BinTree BT ); void PreorderTraversal( BinTree BT ); void PostorderTraversal( BinTree BT ); int main() { BinTree BT = CreateBinTree(); printf("Inorder:"); InorderTraversal(BT); printf("\n"); printf("Preorder:"); PreorderTraversal(BT); printf("\n"); printf("Postorder:"); PostorderTraversal(BT); printf("\n"); return 0; } /* 你的代碼將被嵌在這里 */ 
 

輸入樣例:

如圖
 

tree.jpg

輸出樣例:

Inorder: D B E F A G H C I
Preorder: A B D F E C G H I
Postorder: D E F B H G I C A

代碼:

void InorderTraversal( BinTree BT ) {
    if(!BT) return;
    BinTree s[100] = {BT};
    int c = 1;
    while(c) {
        while(s[c - 1] -> Left) {
            s[c] = s[c - 1] -> Left;
            c ++;
        }
        while(c && s[c - 1] -> Right == NULL) printf(" %c",s[-- c] -> Data);
        if(c) {
            printf(" %c",s[c - 1] -> Data);
            s[c - 1] = s[c - 1] -> Right;
        }
    }
}
void PreorderTraversal( BinTree BT ) {
    if(!BT) return;
    BT -> flag = 0;
    BinTree s[100] = {BT};
    printf(" %c",BT -> Data);
    int c = 1;
    while(c) {
        while(s[c - 1] -> flag == 0 && s[c - 1] -> Left) {
            s[c - 1] -> flag = 1;
            s[c] = s[c - 1] -> Left;
            s[c] -> flag = 0;
            printf(" %c",s[c ++] -> Data);
        }
        while(c && s[c - 1] -> Right == NULL) c --;
        if(c) {
            s[c - 1] = s[c - 1] -> Right;
            s[c - 1] -> flag = 0;
            printf(" %c",s[c - 1] -> Data);
        }
    }
}
void PostorderTraversal( BinTree BT ) {
    if(!BT) return;
    BT -> flag = 0;
    BinTree s[100] = {BT};
    int c = 1;
    while(c) {
        BinTree temp = s[c - 1];
        if(temp -> flag == 0) {
            s[c - 1] -> flag = 1;
            if(temp -> Left) {
                s[c ++] = temp -> Left;
                s[c - 1] -> flag = 0;
            }
        }
        else if(temp -> flag == 1) {
            s[c - 1] -> flag = 2;
            if(temp -> Right) {
                s[c ++] = temp -> Right;
                s[c - 1] -> flag = 0;
            }
        }
        else {
            printf(" %c",s[-- c] -> Data);
        }
    }
}

 


免責聲明!

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



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