生活随笔
收集整理的這篇文章主要介紹了
数据结构源码笔记(C语言):二叉树遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<stdio.h>
#include<malloc.h>
#include<malloc.h>
#define MaxSize 100typedef char ElemType
;typedef struct node
{ElemType data
;struct node
* lchild
;struct node
* rchild
;
}BTNode
;void CreateBTNode(BTNode
* &b
,char *str
)
{BTNode
*St
[MaxSize
],*p
=NULL;int top
=-1,k
,j
=0;char ch
;b
=NULL;ch
=str
[j
];while(ch
!='\0'){switch(ch
){case '(':top
++;St
[top
]=p
;k
=1;break;case ')':top
--;break; case ',':k
=2;break; default: p
=(BTNode
*)malloc(sizeof(BTNode
));p
->data
=ch
;p
->lchild
=p
->rchild
=NULL;if(b
==NULL) b
=p
;else {switch(k
){case 1:St
[top
]->lchild
=p
;break;case 2:St
[top
]->rchild
=p
;break;}}}j
++;ch
=str
[j
];}
}void DispBTNode(BTNode
* b
) {if(b
!=NULL){printf("%c ",b
->data
);if(b
->lchild
!=NULL||b
->rchild
!=NULL){printf("(");DispBTNode(b
->lchild
);if(b
->rchild
!=NULL)printf(",");DispBTNode(b
->rchild
);printf(")");}}}void PreOrder(BTNode
*b
)
{if(b
!=NULL){printf("%c ",b
->data
);PreOrder(b
->lchild
);PreOrder(b
->rchild
);}
}void PreOrder1(BTNode
*b
)
{BTNode
* p
;struct{BTNode
* pt
;int tag
;} St
[MaxSize
];int top
=-1;top
++;St
[top
].pt
=b
;St
[top
].tag
=1;while(top
>-1){if(St
[top
].tag
==1){p
=St
[top
].pt
;top
--;if(p
!=NULL){top
++;St
[top
].pt
=p
->rchild
;St
[top
].tag
=1;top
++;St
[top
].pt
=p
->lchild
;St
[top
].tag
=1;top
++;St
[top
].pt
=p
;St
[top
].tag
=0;}}if(St
[top
].tag
==0){printf("%c ",St
[top
].pt
->data
);top
--;}}
}void PreOrder2(BTNode
*b
)
{BTNode
* St
[MaxSize
],*p
;int top
=-1;if(b
!=NULL){top
++;St
[top
]=b
;while(top
>-1){p
=St
[top
];top
--;printf("%c ",p
->data
);if(p
->rchild
!=NULL){top
++;St
[top
]=p
->rchild
;}if(p
->lchild
!=NULL){top
++;St
[top
]=p
->lchild
;}}printf("\n");}
}void InOrder(BTNode
*b
)
{if(b
!=NULL){InOrder(b
->lchild
);printf("%c ",b
->data
);InOrder(b
->rchild
);}
}void InOrder1(BTNode
*b
)
{BTNode
*p
;struct{BTNode
*pt
;int tag
;}St
[MaxSize
];int top
=-1;top
++;St
[top
].pt
=b
;St
[top
].tag
=1;while(top
>-1) {if(St
[top
].tag
==1) {p
=St
[top
].pt
;top
--;if(p
!=NULL){top
++; St
[top
].pt
=p
->rchild
;St
[top
].tag
=1;top
++; St
[top
].pt
=p
;St
[top
].tag
=0;top
++; St
[top
].pt
=p
->lchild
;St
[top
].tag
=1;}}if(St
[top
].tag
==0){printf("%c ",St
[top
].pt
->data
);top
--;}}
}void InOrder2(BTNode
*b
)
{BTNode
*St
[MaxSize
], *p
;int top
=-1; if(b
!=NULL) {p
=b
;while(top
>-1||p
!=NULL){while(p
!=NULL){top
++; St
[top
]=p
;p
=p
->lchild
;}if(top
>-1){p
=St
[top
];top
--;printf("%c ",p
->data
);p
=p
->rchild
;}}printf("\n"); }
}void PostOrder(BTNode
*b
)
{if(b
!=NULL){PostOrder(b
->lchild
);PostOrder(b
->rchild
);printf("%c ",b
->data
);}
}void PostOrder1(BTNode
*b
)
{BTNode
*p
;struct{BTNode
*pt
;int tag
;}St
[MaxSize
];int top
=-1;top
++;St
[top
].pt
=b
;St
[top
].tag
=1;while(top
>-1){if(St
[top
].tag
==1){p
=St
[top
].pt
;top
--;if(p
!=NULL){top
++;St
[top
].pt
=p
;St
[top
].tag
=0;top
++;St
[top
].pt
=p
->rchild
;St
[top
].tag
=1;top
++;St
[top
].pt
=p
->lchild
;St
[top
].tag
=1;}}if(St
[top
].tag
==0){printf("%c ",St
[top
].pt
->data
);top
--;}}
}void PostOrder2(BTNode
*b
)
{BTNode
* St
[MaxSize
];BTNode
*p
;int flag
,top
=-1;if(b
!=NULL){do{while(b
!=NULL){top
++;St
[top
]=b
;b
=b
->lchild
;}p
=NULL;flag
=1;while(top
!=-1&&flag
){b
=St
[top
];if(b
->rchild
==p
){printf("%c ",b
->data
);top
--;p
=b
;}else{b
=b
->rchild
;flag
=0;}}}while(top
!=-1);printf("\n");}
}void TravLevel(BTNode
*b
)
{BTNode
*Qu
[MaxSize
];int front
,rear
;front
=rear
=0;if(b
!=NULL)printf("%c ",b
->data
);rear
++;Qu
[rear
]=b
;while(rear
!=front
){front
=(front
+1)%MaxSize
;b
=Qu
[front
];if(b
->lchild
!=NULL){printf("%c ",b
->lchild
->data
);rear
=(rear
+1)%MaxSize
;Qu
[rear
]=b
->lchild
;}if(b
->rchild
!=NULL){printf("%c ",b
->rchild
->data
);rear
=(rear
+1)%MaxSize
;Qu
[rear
]=b
->rchild
;}}printf("\n");
}int main()
{BTNode
*b
;CreateBTNode(b
,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");printf(" 二叉樹B:"); DispBTNode(b
); printf("\n\n");printf("層次遍歷序列: "); TravLevel(b
); printf("\n");printf("先序遍歷序列:\n");printf(" 遞歸算法: "); PreOrder(b
); printf("\n");printf("非遞歸算法1: "); PreOrder1(b
); printf("\n");printf("非遞歸算法2: "); PreOrder2(b
); printf("\n");printf("中序遍歷序列:\n");printf(" 遞歸算法: "); InOrder(b
); printf("\n");printf("非遞歸算法1: "); InOrder1(b
); printf("\n");printf("非遞歸算法2: "); InOrder2(b
); printf("\n");printf("后序遍歷序列:\n");printf(" 遞歸算法: "); PostOrder(b
); printf("\n");printf("非遞歸算法1: "); PostOrder1(b
); printf("\n");printf("非遞歸算法2: "); PostOrder2(b
); printf("\n");return 0;
}
數據結構源碼筆記(C語言描述)匯總:
數據結構源碼筆記(C語言):英文單詞按字典序排序的基數排序
數據結構源碼筆記(C語言):直接插入排序
數據結構源碼筆記(C語言):直接選擇排序
數據結構源碼筆記(C語言):置換-選擇算法
數據結構源碼筆記(C語言):Huffman樹字符編碼
數據結構源碼筆記(C語言):Josephus問題之順序表
數據結構源碼筆記(C語言):Josephus問題之循環鏈接表
數據結構源碼筆記(C語言):多項式合并
數據結構源碼筆記(C語言):二叉樹之葉子結點旋轉銷毀
數據結構源碼筆記(C語言):哈夫曼樹
數據結構源碼筆記(C語言):集合的位向量表示
數據結構源碼筆記(C語言):鏈接隊列
數據結構源碼筆記(C語言):鏈接棧
數據結構源碼筆記(C語言):線性表的單鏈表示
數據結構源碼筆記(C語言):線性表的順序表示
數據結構源碼筆記(C語言):棧的基本操作
數據結構源碼筆記(C語言):中綴表達式
數據結構源碼筆記(C語言):希爾插入排序
數據結構源碼筆記(C語言):索引文件建立和查找
數據結構源碼筆記(C語言):冒泡排序
數據結構源碼筆記(C語言):快速排序
數據結構源碼筆記(C語言):可變長度字符串的快速排序
數據結構源碼筆記(C語言):基數排序
數據結構源碼筆記(C語言):二路歸并排序
數據結構源碼筆記(C語言):堆排序
數據結構源碼筆記(C語言):二叉樹搜索樹Kruskal
數據結構源碼筆記(C語言):二叉搜索樹Prim
數據結構源碼筆記(C語言):最短路徑弗洛伊德算法
數據結構源碼筆記(C語言):深度、廣度優先生成樹
數據結構源碼筆記(C語言):鄰接矩陣轉化鄰接表
數據結構源碼筆記(C語言):統計字符串中出現的字符及其次數
數據結構源碼筆記(C語言):順序查找
數據結構源碼筆記(C語言):哈希表的相關運算算法
數據結構源碼筆記(C語言):分塊法查找
數據結構源碼筆記(C語言):二分查找
數據結構源碼筆記(C語言):二叉樹遍歷
數據結構源碼筆記(C語言):二叉平衡樹的相關操作算法
數據結構源碼筆記(C語言):二叉排序樹的基本操作算法
數據結構源碼筆記(C語言):B樹的相關運算算法
總結
以上是生活随笔為你收集整理的数据结构源码笔记(C语言):二叉树遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。