資料結構 非遞迴

2021-05-25 16:59:45 字數 1330 閱讀 6054

#include "stdafx.h"

#include

using namespace std;

//二叉樹鏈式儲存的標頭檔案

typedef char datatype;         //結點屬性值型別

typedef struct node            //二叉樹結點的型別

bintnode;

typedef bintnode *bintree;

bintree root;                   //指向二叉樹根結點指標

//下面是些棧的操作 為非遞迴實現做準備

typedef struct stack            //棧結構定義

seqstack;

void push(seqstack* s,bintree t) //進棧

bintree pop(seqstack* s)            //出棧

else

return null;

}//按照前序遍歷順序建立一棵給定的二叉樹

void createbintree(bintree* t)  

}//二叉樹前序遍歷 遞迴 實現

void preorder(bintree t)            //t 是指標變數,而不是 結點 結構體變數

}//二叉樹前序遍歷 非遞迴 實現

void preorder1(bintree t)

//迴圈直到遍歷完 當前子樹 根結點,和其 左 孩子

if(s.top>-1)}}

//二叉樹中序遍歷遞迴演算法

void inorder(bintree t)

}//二叉樹中序遍歷 非遞迴 演算法

void inorder1(bintree t)

if(s.top!=-1)}}

//後序遞迴遍歷二叉樹

void postorder(bintree t)

}//後序遍歷 非遞迴 實現

void postorder1(bintree t)

while((s.top>-1)&&(s.tag[s.top]==1))

if(s.top>-1)

else

t=null;}}

int main()

}// microsoft visual c++ will insert additional declarations immediately before the previous line.

#endif // !defined(afx_stdafx_h__c0cbea7b_e2a6_4828_a3e7_58f987b9ce12__included_)

資料結構 樹非遞迴遍歷

這裡以二叉樹為乙個例子來進行樹的先序,中序,後序,層序,二叉樹的刪除操作。include include using namespace std typedef struct bitnodebitnode bitree bitnode newnode int ch void insert bitre...

資料結構 演算法 14 遞迴 轉非遞迴

目錄遞迴 改為非遞迴 遞迴是一種應用非常廣泛的演算法 程式設計技巧,如 所有的遞迴都可以轉為非遞迴實現。李柱明部落格 遞迴的定義 遞迴與棧結構 優點 遞迴 的表達力很強,寫起來非常簡潔。缺點 空間複雜度高。有堆疊溢位的風險。存在重複計算。過多的函式呼叫會耗時較多。遞迴需要滿足的兩個條件 乙個問題可以...

資料結構(非遞迴先序遍歷)

要求實現下列函式 void preorder bitree bt,void visit telemtype 使用棧,非遞迴先序遍歷二叉樹bt,對每個結點的元素域data呼叫函式visit 二叉鍊錶型別定義 typedef struct bitnode bitnode,bitree status in...