#include <stdio.h> #include <stdlib.h> #define MAXSIZE 50 typedef struct Node { char data; struct Node *LChild; struct Node *RChild; }BiTNode,*BiTree; typedef struct { BiTree element[MAXSIZE]; int front; int rear; }SeqQueue; /*初始化隊列*/ void InitQueue(SeqQueue *Q) { Q->front = Q->rear = 0; } /*入隊*/ int EnterQueue(SeqQueue *Q, BiTree bt) { if ((Q->rear + 1) % MAXSIZE == Q->front) { return 0; } else { Q->element[Q->rear] = bt; Q->rear = (Q->rear + 1) % MAXSIZE; return 1; } } /*出隊*/ int DeleteQueue(SeqQueue *Q, BiTree *bt) { if (Q->front == Q->rear) { return 0; } else { *bt = Q->element[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return 1; } } /*判斷隊列是否爲空*/ int IsEmpty(SeqQueue *Q) { if (Q->front == Q->rear) { return 1; } else { return 0; } } /*擴展線序遍歷建立二叉鏈表*/ void CreateBiTree(BiTree *bt) { char ch; ch = getchar(); if (ch == '\n') { return; } if (ch == '.') { *bt = NULL; } else { *bt = (BiTree)malloc(sizeof(BiTNode)); (*bt)->data = ch; CreateBiTree(&((*bt)->LChild)); CreateBiTree(&((*bt)->RChild)); } } /*先序遍歷輸出二叉樹的結點*/ void PreOrder(BiTree root) { if (root != NULL) { printf("%c ",root->data); PreOrder(root->LChild); PreOrder(root->RChild); } } /*中序遍歷輸出二叉樹的結點*/ void InOrder(BiTree root) { if (root != NULL) { InOrder(root->LChild); printf("%c ", root->data); InOrder(root->RChild); } } /*中序非遞歸遍歷輸出二叉樹的結點*/ void InOrderNo(BiTree root) { int top = 0; BiTree p = root; BiTree s[MAXSIZE] = { NULL }; do{ while (p != NULL) { if (top > MAXSIZE) { return; } else { top++; s[top] = p; p = p->LChild; } } if (top != 0) { p = s[top]; top--; printf("%c ", p->data); p = p->RChild; } } while (p != NULL || top != 0); } /*後序遍歷輸出二叉樹的結點*/ void PostOrder(BiTree root) { if (root != NULL) { PostOrder(root->LChild); PostOrder(root->RChild); printf("%c ", root->data); } } /*桉樹狀打印二叉樹,逆中序*/ void PrintTree(BiTree root, int nLayer) { if (root == NULL) { return; } else { PrintTree(root->RChild, nLayer + 1); for (int i = 0; i < nLayer; i++) { printf(" "); } printf("%c\n", root->data); PrintTree(root->LChild, nLayer + 1); } } /*層次遍歷二叉樹*/ void LayerOrder(BiTree root) { SeqQueue Q; BiTree p = NULL; InitQueue(&Q); if (root == NULL) { return; } else { EnterQueue(&Q, root); while (!IsEmpty(&Q)) { DeleteQueue(&Q, &p); printf("%c ",p->data); if (p->LChild != NULL) { EnterQueue(&Q, p->LChild); } if (p->RChild != NULL) { EnterQueue(&Q, p->RChild); } } return 1; } } /*求二叉樹的高度*/ int PostTreeDepth(BiTree root) { int hl = 0; int hr = 0; int max = 0; if (root != NULL) { hl = PostTreeDepth(root->LChild); hr = PostTreeDepth(root->RChild); max = (hl > hr) ? hl : hr; return max + 1; } else { return 0; } } /*二叉樹的結點個數*/ int RootCount(BiTree root) { int count = 1; if (root != NULL) { count += (RootCount(root->LChild) + RootCount(root->RChild)); } else { count = 0; } return count; } /*二叉樹的葉子個數*/ int LeafCount(BiTree root) { int leafCount = 0; if (root == NULL) { leafCount = 0; } else if ((root->LChild == NULL) && (root->RChild == NULL)) { leafCount = 1; } else { leafCount = (LeafCount(root->LChild) + LeafCount(root->RChild)); } return leafCount; } /*交換二叉樹每一個結點的左子樹和右子樹*/ void ChangeLeftRight(BiTree *bt) { if ((*bt)->LChild == NULL && (*bt)->RChild == NULL) { return; } else { BiTree tmp = (*bt)->LChild; (*bt)->LChild = (*bt)->RChild; (*bt)->RChild = tmp; if ((*bt)->LChild != NULL) { ChangeLeftRight(&((*bt)->LChild)); } if ((*bt)->RChild != NULL) { ChangeLeftRight(&((*bt)->RChild)); } } } void maue() { printf("\n"); printf(" ☆☆☆☆★★★★★ 歡迎使用本系統 ★★★★★☆☆☆☆\n"); printf(" ☆☆☆★★★★★ 一、創建二叉樹的二叉鏈表 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 二、二叉樹的先序遞歸遍歷 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 三、二叉樹的中序遞歸遍歷 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 四、二叉樹的非遞歸中序遞歸遍歷 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 五、二叉樹的後序遞歸遍歷 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 六、樹狀打印此二叉樹 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 七、二叉樹的層次遍歷 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 八、二叉樹的高度 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 九、二叉樹的結點個數 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 十、二叉樹的葉子個數 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 十一、交換二叉樹的左子樹和右子樹 ★★★★★☆☆☆ \n"); printf(" ☆☆☆★★★★★ 0、退出系統 ★★★★★☆☆☆ \n"); printf("\n"); } int main() { BiTree bt = NULL; int number = 0; do{ maue(); printf("請選擇您要進行的本系統的功能: > "); scanf_s("%d", &number); switch (number) { case 1: printf("請輸入二叉樹的擴展先序遍歷序列:> "); getchar(); CreateBiTree(&bt); printf("\n"); break; case 2: printf("此二叉樹的先序遍歷序列爲:> "); PreOrder(bt); printf("\n"); break; case 3: printf("此二叉樹的中序遍歷序列爲:> "); InOrder(bt); printf("\n"); break; case 4: printf("此二叉樹的非遞歸中序遍歷序列爲:> "); InOrderNo(bt); printf("\n"); break; case 5: printf("此二叉樹的後序遍歷序列爲:> "); PostOrder(bt); printf("\n"); break; case 6: printf("樹狀打印此二叉樹爲:>\n "); PrintTree(bt, 2); printf("\n"); break; case 7: printf("層次遍歷印此二叉樹爲:> "); LayerOrder(bt); printf("\n"); break; case 8: printf("此二叉樹的高度爲:> "); int heigh = PostTreeDepth(bt); printf("heigh = %d\n", heigh); break; case 9: printf("此二叉樹的結點個數爲:> "); int rootCount = RootCount(bt); printf("rootCount = %d\n", rootCount); break; case 10: printf("此二叉樹的葉子結點個數爲:> "); int leafCount = LeafCount(bt); printf("leafCount = %d\n", leafCount); break; case 11: printf("交換二叉樹每一個結點的左子樹和右子樹後二叉樹變爲(先序遍歷):>\n "); ChangeLeftRight(&bt); PreOrder(bt); printf("\n"); break; case 0: printf("感謝您使用本系統,歡迎您的再次使用!\n"); break; default: printf("請輸入正確的功能號!\n"); break; }/*switch*/ } while (number); system("pause"); return 0; }