數據結構實驗:二叉樹的應用


#include<cstring> #include<cstdio> #include<iostream>
using namespace std; typedef struct treepoint* tNode; typedef struct queue* qNode; typedef struct stack* sNode; struct treepoint { char date; tNode lson, rson; }; char s[110]; int pos; tNode build() {//建立二叉樹
    tNode bt = (tNode)malloc(sizeof(treepoint)); pos++; if (s[pos] == '#')bt = NULL; else { bt->date = s[pos]; bt->lson = build(); bt->rson = build(); } return bt; } void pre_order_travel(tNode bt) {//前序遍歷
    printf("%c", bt->date); if (bt->lson != NULL) pre_order_travel(bt->lson); if (bt->rson != NULL) pre_order_travel(bt->rson); //free(bt);
} void mid_order_travel(tNode bt) {//中序遍歷
    if (bt->lson != NULL) mid_order_travel(bt->lson); printf("%c", bt->date); if (bt->rson != NULL) mid_order_travel(bt->rson); //free(bt);
} void post_order_travel(tNode bt) {//后序遍歷
    if (bt->lson != NULL) post_order_travel(bt->lson); if (bt->rson != NULL) post_order_travel(bt->rson); printf("%c", bt->date); //free(bt);
} struct stack { tNode value; sNode next; }; sNode top; void Stack_creat() { top = (sNode)malloc(sizeof(stack)); top->next = NULL; } void Stack_push(tNode item) { sNode temp = (sNode)malloc(sizeof(stack)); temp->next = top->next; temp->value = item; top->next = temp; } bool Stack_empty() { return top->next == NULL; } void Stack_pop() { sNode temp = top->next; top->next = temp->next; } void iter_Inorder(tNode root) {//非遞歸遍歷二叉樹
 Stack_creat(); if (root != NULL) { Stack_push(root); } while (!Stack_empty()) { sNode temp = top->next; cout << temp->value->date; Stack_pop(); if (temp->value->rson) { Stack_push(temp->value->rson); } if (temp->value->lson) { Stack_push(temp->value->lson); } } cout << endl; } qNode front, rear; struct queue { tNode value; qNode next; }; void Queue_creat() { front = (qNode)malloc(sizeof(queue)); rear = (qNode)malloc(sizeof(queue)); front->next = rear; } bool Queue_empty() { return front->next == rear; } void Queue_delete() { qNode temp = front->next; front->next = temp->next; } void Queue_push(tNode item) { qNode temp = (qNode)malloc(sizeof(queue)); rear->value = item; rear->next = temp; temp->next = NULL; rear = temp; } void level_order(tNode root) {//二叉樹的層序遍歷
    if (root == NULL) { cout << endl; } Queue_creat(); Queue_push(root); while (!Queue_empty()) { qNode temp = front->next; Queue_delete(); cout << temp->value->date; if (temp->value->lson != NULL) { Queue_push(temp->value->lson); } if (temp->value->rson != NULL) { Queue_push(temp->value->rson); } } cout << endl; } int leaf(tNode bt) {//計算葉子節點數量
    int flag = 0; int cnt = 0; if (bt->lson != NULL) cnt += leaf(bt->lson); else flag++; if (bt->rson != NULL) cnt += leaf(bt->rson); else flag++; if (flag == 2) cnt = 1; return cnt; } int maxheight = 0; void height(tNode bt, int now) {//計算樹的深度
    if (now > maxheight)maxheight = now; if (bt->lson != NULL) height(bt->lson, now + 1); if (bt->rson != NULL) height(bt->rson, now + 1); } int main() { while (~scanf("%s", s + 1)) {//輸入二叉樹的前序遍歷來建樹 pos = 0; tNode root = build(); cout << "先序遍歷: "; pre_order_travel(root); cout << endl; cout << "中序遍歷: "; mid_order_travel(root); cout << endl; cout << "后序遍歷: "; post_order_travel(root); cout << endl; cout << "二叉樹的非遞歸遍歷: "; iter_Inorder(root); cout << "二叉樹的層序遍歷: "; level_order(root); printf("葉子結點數: %d\n", leaf(root)); maxheight = 0; height(root, 1); printf("樹的深度為: %d\n", maxheight); } }


免責聲明!

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



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