![二叉樹中所有節(jié)點左右字?jǐn)?shù)節(jié)點的交換及遍歷_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/9/364f9b4e-8ec0-4d10-b95a-691c6c77c3f0/364f9b4e-8ec0-4d10-b95a-691c6c77c3f01.gif)
![二叉樹中所有節(jié)點左右字?jǐn)?shù)節(jié)點的交換及遍歷_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/9/364f9b4e-8ec0-4d10-b95a-691c6c77c3f0/364f9b4e-8ec0-4d10-b95a-691c6c77c3f02.gif)
![二叉樹中所有節(jié)點左右字?jǐn)?shù)節(jié)點的交換及遍歷_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/9/364f9b4e-8ec0-4d10-b95a-691c6c77c3f0/364f9b4e-8ec0-4d10-b95a-691c6c77c3f03.gif)
![二叉樹中所有節(jié)點左右字?jǐn)?shù)節(jié)點的交換及遍歷_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/9/364f9b4e-8ec0-4d10-b95a-691c6c77c3f0/364f9b4e-8ec0-4d10-b95a-691c6c77c3f04.gif)
![二叉樹中所有節(jié)點左右字?jǐn)?shù)節(jié)點的交換及遍歷_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/9/364f9b4e-8ec0-4d10-b95a-691c6c77c3f0/364f9b4e-8ec0-4d10-b95a-691c6c77c3f05.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、二叉樹中所有節(jié)點的左右子樹相互交換 遞歸與非遞歸程序(2006-10-11 11:24:09) 轉(zhuǎn)載分類: 數(shù)據(jù)結(jié)構(gòu) /將二叉樹中所有節(jié)點的左右子樹相互交換BiNode* Exchange(BiNode* T) BiNode* p; if(NULL=T | (NULL=T->lchild && NULL=T->rchild) return T; p = T->lchild; T->lchild = T->rchild; T->rchild = p; i
2、f(T->lchild) T->lchild = Exchange(T->lchild); if(T->rchild) T->rchild = Exchange(T->rchild); return T;/將二叉樹中所有節(jié)點的左右子樹相互交換/不使用遞歸void NonRecursive_Exchange(BiNode* T) Stack s; BiNode* p; if(NULL=T) retu
3、rn; InitStack(&s); Push(&s,T); while(!isEmpty(&s) T = Pop(&s); p = T->lchild; T->lchild = T->rchild; T->rchild = p; if(T->rchild) Push(&s,T->rchild); if
4、(T->lchild) Push(&s,T->lchild); DestroyStack(&s); /遞推形式的前續(xù)遍歷,不使用遞歸和堆棧,/但每個節(jié)點增加了一個parent指針和Mark標(biāo)記void Iteration_Mark_PreOrderTraverse(BiPMNode* T) if(NULL = T) return; while(NULL != T) if(0 =
5、T->mark) T->mark +; printf("%d ",T->data); while(NULL != T->lchild) T = T->lchild; T->mark +;
6、0;printf("%d ",T->data); T->mark +; if(NULL != T->rchild) T = T->rchild; continue; if(1 = T->mark) T->mark +; &
7、#160;if(NULL != T->rchild) T = T->rchild; continue; if(2 = T->mark) T = T->parent; /遞推形式的中續(xù)遍歷,不使用遞歸和堆棧,/但每個節(jié)點增加了一個paren
8、t指針和Mark標(biāo)記void Iteration_Mark_InOrderTraverse(BiPMNode* T) if(NULL = T) return; while(NULL != T) if(0 = T->mark) T->mark +; while(NULL != T->lchild)
9、; T = T->lchild; T->mark +; printf("%d ",T->data); T->mark +; if(NULL != T->rchild) T = T
10、->rchild; continue; if(1 = T->mark) printf("%d ",T->data); T->mark +; if(NULL != T->rchild) T = T->rchild; cont
11、inue; if(2 = T->mark) T = T->parent; /遞推形式的后續(xù)遍歷,不使用遞歸和堆棧,/但每個節(jié)點增加了一個parent指針和Mark標(biāo)記void Iteration_Mark_PostOrderTraverse(BiPMNode* T) if(NULL = T) return;&
12、#160; while(NULL != T) if(0 = T->mark) T->mark +; while(NULL != T->lchild) T = T->lchild; T->mark +;
13、; T->mark +; if(NULL != T->rchild) T = T->rchild; continue; if(1 = T->mark) T->mark +; if(NULL != T->rchild) T = T-&
14、gt;rchild; continue; if(2 = T->mark) printf("%d ",T->data); T = T->parent; 二叉樹 層次遍歷(2006-10-10 11:12:31) 轉(zhuǎn)載分類: 數(shù)據(jù)結(jié)構(gòu) /二叉樹的層次遍
15、歷,使用隊列實現(xiàn)void LayerOrderTraverse(BiNode* T) Queue q; if(NULL = T) return; InitQueue(&q); EnQueue(&q,T); while(!isQueueEmpty(&q) T = DeQueue(&q); printf("%d ",T->data); if(T->lchild)&
16、#160; EnQueue(&q,T->lchild); if(T->rchild) EnQueue(&q,T->rchild); DestroyQueue(&q); typedef struct _QNode BiNode* data; struct _QNode* next;QNode;typedef struct _queue QNode* front; QNode* rear;Queu
17、e;void InitQueue(Queue* q) q->front = q->rear = (QNode*)malloc(sizeof(QNode); q->front->next = NULL;bool isQueueEmpty(Queue* q) if(q->front = q->rear) return true; else return false;void EnQueue(Queue* q, BiNode* data) QNode* pNo
18、de; pNode = (QNode*)malloc(sizeof(QNode); pNode->data = data; pNode->next = NULL; q->rear->next = pNode; q->rear = pNode;BiNode* DeQueue(Queue* q) QNode* pNode; BiNode* pData; assert(q->front != q->rear); pNode = q->front->next;
19、 q->front->next = pNode->next; if(q->rear = pNode) q->rear = q->front; pData = pNode->data; free(pNode); return pData;void DestroyQueue(Queue* q) while(NULL != q->front) q->rear = q->front->next;
20、160; free(q->front); q->front = q->rear; 二叉排序樹-創(chuàng)建(2006-10-10 10:05:04) 轉(zhuǎn)載分類: 數(shù)據(jù)結(jié)構(gòu) typedef struct BiNode int data; struct BiNode *lchild; struct BiNode *rchild;BiNode; BiNode* Insert(BiNode* T, int data) if(NULL = T)
21、0; T = (BiNode*)malloc(sizeof(BiNode); T->data = data; T->lchild = NULL; T->rchild = NULL; return T; if(data <= T->data) T->lchild = Insert(T->lchild, data); else T->rchild = Insert(T-&
22、gt;rchild, data); return T; /創(chuàng)建一個二叉排序樹, input -1 to endBiNode* createBiSortTree() BiNode *root = NULL; int data; while(1) scanf("%d",&data); if(-1 = data) break; root = Insert(root, data); retu
23、rn root;查看文章 交換二叉樹所有節(jié)點的左右子樹 2010-12-03 21:44/實驗題目:已知二叉樹以二叉鏈表作為存儲結(jié)構(gòu),寫一個算法來交換二叉樹的所有節(jié)點的左右子樹/先建立二叉樹的二叉鏈表存儲結(jié)構(gòu),再完成算法,注意結(jié)果的輸出形式#include <stdio.h>#include <malloc.h>#include <windows.h>#define STACK_INIT_SIZE 100;#define STACKINCREMENT 10;/定義二叉樹數(shù)據(jù)類型typedef char TElemtype;typedef struct BiT
24、Node TElemtype data;struct BiTNode *lchild,*rchild;BiTNode,*BiTree;/-二叉樹基本操作-/初始化二叉樹bool InitBiTree(BiTree &T)T=(BiTree)malloc(sizeof(BiTNode);T->data=NULL;T->lchild=NULL;T->rchild=NULL;return true;/創(chuàng)建二叉樹void CreateBiTree(BiTree &T) TElemtype c=
25、39; 'c=getchar();getchar();if(c=' ') T=NULL;else InitBiTree(T); T->data=c; CreateBiTree(T->lchild); CreateBiTree(T->rchild);/操作函數(shù)-輸出bool Visit(TElemtype e)if(e
26、!=NULL) printf("%c ",e); return true;else return false;/先序遍歷二叉樹bool PreOrderTraverse(BiTree T,bool Visit(TElemtype) if(T) if(Visit(T->data) if (PreOrderTr
27、averse(T->lchild,Visit) if (PreOrderTraverse(T->rchild,Visit) return true; return false;else ret
28、urn true;/-/定義棧的數(shù)據(jù)類型typedef structTElemtype *base;TElemtype *top;int stacksize;SqStack;/交換左右子樹void exchange(BiTree &rt)BiTree temp = NULL;if(rt->lchild = NULL && rt->rchild = NULL) return;else temp = rt-&
29、gt;lchild; rt->lchild = rt->rchild; rt->rchild = temp;if(rt->lchild) exchange(rt->lchild);if(rt->rchild) exchange(rt->rchild);/-Main函數(shù)-void main()B
30、iTree T; MessageBox(NULL,"請按照先序遍歷輸入二叉樹!","提示",MB_OK|MB_ICONWARNING);MessageBox(NULL,"請輸入數(shù)據(jù)!(空格表示結(jié)束)","提示",MB_OK|MB_ICONWARNING);CreateBiTree(T);MessageBox(NULL,"輸入結(jié)束!","提示",MB_OK|MB_ICONWARNING);printf("n按先序輸出n")
31、;PreOrderTraverse(T,Visit);MessageBox(NULL,"輸出結(jié)束!","提示",MB_OK|MB_ICONWARNING); printf("n交換后n");exchange(T); PreOrderTraverse(T,Visit);窗體頂端隨筆- 8 文章- 4 評論- 0 二叉樹左右子樹交換(麻煩)/*對任意一顆二叉樹,是將其說有結(jié)點的左右子樹交換,并將交換前后不同二叉樹分別用層序、前序,中序三
32、種不同的方法進(jìn)行遍歷。算法描述:分四步 (1) 創(chuàng)建二叉樹 用遞歸的方法創(chuàng)建樹根結(jié)點,然后分別創(chuàng)建左右子樹。在創(chuàng)建二叉樹時結(jié)點可以提前確定,也可以不確定,有數(shù)據(jù)輸入控制。此方法中樹的節(jié)點有輸入端輸入,然后再輸入一個字符串,然后從字符串中讀入數(shù)據(jù)創(chuàng)建二叉樹。(2)用三種不同的方法遍歷交換前的二叉樹。層次遍歷,先根遍歷,中跟遍歷。層次遍歷用一個指針棧來實現(xiàn)。另外兩種方法用遞歸即可。(3)交換二叉樹中所有的結(jié)點的左右子樹。交換過程中需要用一個指針站來實現(xiàn)。(4)遍歷二叉樹。實現(xiàn)過程跟第二步差不多。*/#include<stdio.h>#include<stdli
33、b.h>#define max 20/定義樹的結(jié)點數(shù) #define null 0typedef struct btnode/定義二叉樹結(jié)點類型 char date;/結(jié)點數(shù)據(jù)類型 struct btnode *lc,*rc;/左右指針 bttree;bttree *createtree(char *str,int i,int m)/將字符串str中第i到第m個字符創(chuàng)建樹 bttree *p; if(i>=m) return null; p=(
34、bttree*)malloc(sizeof(bttree);/生成新結(jié)點 p->date=stri;/將結(jié)點的第一個數(shù)據(jù)付給根 p->lc=createtree(str,2*i+1,m);/創(chuàng)建左子樹 p->rc=createtree(str,2*i+2,m);/創(chuàng)建右子樹 return (p); bttree *jiaohuan(bttree *p)/將p指針指向的二叉樹的左右子樹進(jìn)行互換。 bttree *stackmax;/指針類型的堆棧 int k; k=0;
35、60;stackk=null; if(p!=null)/交換p結(jié)點的左右孩子 k+; stackk=p->lc; p->lc=p->rc; p->rc=stackk; p->lc=jiaohuan(p->lc); p->rc=jiaohuan(p->rc); return (p); void xiangen(bttree *t)/先跟遍歷 &
36、#160;if(t!=null) printf("%c",t->date); if(t->lc) printf("->"); xiangen(t->lc); if(t->rc) printf("->");
37、xiangen(t->rc); void zhonggen(bttree *p)/中根遍歷即中序遍歷 if(p!=null) zhonggen(p->lc); printf("%c",p->date); printf("->"); zhonggen(p->rc); void cengci(bttree
38、*t,int m)/按層次遍歷 bttree *queuemax; bttree *p; int front=0,rear=0,i; queuerear+=t; while(front!=rear) p=queuefront; front=(front+1)%m; printf("%c->",p->date);/輸出根結(jié)點 if(p->lc!=null)/遍歷左子樹 if(rear+1)%m!=front) queuerear=p->lc; rear=(rear+1)%m; if(p->rc!=null)/遍歷右子樹 if(rear+1)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘教版數(shù)學(xué)八年級下冊第三章《圖形與坐標(biāo)》聽評課記錄
- 蘇科版數(shù)學(xué)七年級上冊2.5.3《有理數(shù)的加法與減法》聽評課記錄
- 【人教版】八年級地理上冊第三章《土地資源》聽課評課記錄及優(yōu)化訓(xùn)練答案
- 2017-2022年中國家居收納用品產(chǎn)業(yè)深度調(diào)研與投資前景分析報告(目錄)
- 《成語的運用》說課稿-2024-2025學(xué)年統(tǒng)編版語文五年級上冊
- 2025年度新材料研發(fā)合作投資合同協(xié)議書
- 2025年度跨境貿(mào)易融資合同保證書規(guī)范
- 2025年度建筑工程設(shè)計咨詢合同實施細(xì)則
- 小學(xué)英語聽評課記錄本
- 現(xiàn)代漢語詞匯學(xué)精選課件
- PCB行業(yè)安全生產(chǎn)常見隱患及防范措施課件
- 上海音樂學(xué)院 樂理試題
- SAP中國客戶名單
- DB32∕T 186-2015 建筑消防設(shè)施檢測技術(shù)規(guī)程
- 2022年福建泉州中考英語真題【含答案】
- 汽車座椅骨架的焊接夾具畢業(yè)設(shè)計說明書(共23頁)
- 露天礦山職業(yè)危害預(yù)先危險分析表
- 淺談固定資產(chǎn)的審計
- WZCK-20系列微機(jī)直流監(jiān)控裝置使用說明書(v1.02)
- 模糊推理方法
評論
0/150
提交評論