数据结构——二叉树的层次遍历进阶
生活随笔
收集整理的這篇文章主要介紹了
数据结构——二叉树的层次遍历进阶
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前的一個博客
數據結構——二叉樹的層次遍歷看完這個,可以簡單實現下面的問題
問題:
1.計算二叉樹的最大寬度(二叉樹的最大寬度是指二叉樹所有層中結點個數的最大值。
2.用按層次順序遍歷二叉樹的方法,統計樹中具有度為1的結點數目。
解決問題的思路:
核心代碼
int BiTree_height1(BiTree T)//求樹的深度算法1 {if(T==NULL)return 0;else{if(BiTree_height1(T->lchild)>BiTree_height1(T->rchild))return 1+BiTree_height1(T->lchild);elsereturn 1+BiTree_height1(T->rchild);}} void printNodeAtLevel(BiTree T,int level,int *width,int static_level) { if(T==NULL||level<0) return; if(level==0) { printf("%c ",T->data);width[static_level]++;return; } // 左子樹的 level - 1 級 printNodeAtLevel(T->lchild,level-1,width,static_level); // 右子樹的 level - 1 級 printNodeAtLevel(T->rchild,level-1,width,static_level); }void levelOrder( BiTree T) {if(T==NULL)return;int totalLevel = BiTree_height1(T);int width[totalLevel]={0};for(int i = 0; i< totalLevel; i++){printNodeAtLevel(T, i,width,i);printf("\t\t\t第%d層的寬度為:%d",i,width[i]);printf("\n");//打印完一層,換行}int max_width=width[0],max=1;for(int j=1;j<totalLevel;j++){if(width[j]>max_width){max_width=width[j];max=j+1;}}printf("最大寬度為%d,首先出現最大寬度的是第%d層",max_width,max);} void printNodeAtLevel2(BiTree T,int level,int *single_branch) { if(T==NULL||level<0) return; if(level==0) { printf("%c ",T->data);if(T->lchild==NULL&&T->rchild!=NULL||T->lchild!=NULL&&T->rchild==NULL)//單分支時 single_branch[1]++; return; } // 左子樹的 level - 1 級 printNodeAtLevel2(T->lchild,level-1,single_branch); // 右子樹的 level - 1 級 printNodeAtLevel2(T->rchild,level-1,single_branch); }void levelOrder2(const BiTree T) {if(T==NULL)return;int totalLevel = BiTree_height1(T);int single_branch[1]={0};for(int i = 0; i< totalLevel; i++){printNodeAtLevel2(T, i,single_branch);printf("\n");//打印完一層,換行}printf("該樹的單分支節點(即度為1的節點)數為:%d",single_branch[1]);}全部代碼(可直接執行)
#include<stdio.h> #include<bits/stdc++.h> typedef char TElemType; typedef int status; typedef struct BiNode {TElemType data;struct BiNode *lchild;struct BiNode *rchild; }BiNode,*BiTree; void CreateBiTree(BiTree &T)//二叉樹的先序創建 {TElemType ch;scanf("%c",&ch);if(ch=='#')T=NULL;else {T=(BiNode*)malloc(sizeof(BiNode));if(!T)exit(-1);T->data=ch;CreateBiTree(T->lchild);CreateBiTree(T->rchild);} }void DestroyBiTree(BiTree &T)//二叉樹的銷毀算法 {if(T==NULL)exit(-1);else{DestroyBiTree(T->lchild);DestroyBiTree(T->rchild);free(T);} }int preorderTraverse(BiTree T)//二叉樹的先序遞歸遍歷算法 {if(T==NULL)return 0;else {printf("%c ",T->data);preorderTraverse(T->lchild);preorderTraverse(T->rchild);}} int InorderTraverse(BiTree T)//二叉樹的中序遞歸遍歷算法 {if(T==NULL)return 0;else {InorderTraverse(T->lchild);printf("%c ",T->data);InorderTraverse(T->rchild);}}int PostorderTraverse(BiTree T)//二叉樹的后序遞歸遍歷算法 {if(T==NULL)return 0;else {PostorderTraverse(T->lchild);PostorderTraverse(T->rchild);printf("%c ",T->data);}}int BiTree_height1(BiTree T)//求樹的深度算法1 {if(T==NULL)return 0;else{if(BiTree_height1(T->lchild)>BiTree_height1(T->rchild))return 1+BiTree_height1(T->lchild);elsereturn 1+BiTree_height1(T->rchild);}} void printNodeAtLevel(BiTree T,int level,int *width,int static_level) { if(T==NULL||level<0) return; if(level==0) { printf("%c ",T->data);width[static_level]++;return; } // 左子樹的 level - 1 級 printNodeAtLevel(T->lchild,level-1,width,static_level); // 右子樹的 level - 1 級 printNodeAtLevel(T->rchild,level-1,width,static_level); }void levelOrder( BiTree T) {if(T==NULL)return;int totalLevel = BiTree_height1(T);int width[totalLevel]={0};for(int i = 0; i< totalLevel; i++){printNodeAtLevel(T, i,width,i);printf("\t\t\t第%d層的寬度為:%d",i,width[i]);printf("\n");//打印完一層,換行}int max_width=width[0],max=1;for(int j=1;j<totalLevel;j++){if(width[j]>max_width){max_width=width[j];max=j+1;}}printf("最大寬度為%d,首先出現最大寬度的是第%d層",max_width,max);} void printNodeAtLevel2(BiTree T,int level,int *single_branch) { if(T==NULL||level<0) return; if(level==0) { printf("%c ",T->data);if(T->lchild==NULL&&T->rchild!=NULL||T->lchild!=NULL&&T->rchild==NULL)//單分支時 single_branch[1]++; return; } // 左子樹的 level - 1 級 printNodeAtLevel2(T->lchild,level-1,single_branch); // 右子樹的 level - 1 級 printNodeAtLevel2(T->rchild,level-1,single_branch); }void levelOrder2(const BiTree T) {if(T==NULL)return;int totalLevel = BiTree_height1(T);int single_branch[1]={0};for(int i = 0; i< totalLevel; i++){printNodeAtLevel2(T, i,single_branch);printf("\n");//打印完一層,換行}printf("該樹的單分支節點(即度為1的節點)數為:%d",single_branch[1]);} int main() {BiTree T;printf("創建樹輸入樹T的先序序列(其中使用#代表空節點)\n");CreateBiTree(T);printf("先序遍歷算法");preorderTraverse(T);printf("\n中序遍歷算法");InorderTraverse(T);printf("\n后序遍歷算法");PostorderTraverse(T);printf("\n二叉樹層次遍歷算法\n");levelOrder(T);printf("\n二叉樹層次遍歷算法\n");levelOrder2(T);}例如:
總結
以上是生活随笔為你收集整理的数据结构——二叉树的层次遍历进阶的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构——二叉树的双序遍历
- 下一篇: 数据结构——二叉树的最长路径问题