日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《软件技术基础》实验指导 实验五

發(fā)布時間:2025/3/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《软件技术基础》实验指导 实验五 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

實驗五 樹

一、實驗目的

  • 熟悉二叉樹的鏈式存儲結構
  • 掌握二叉樹的建立、深度優(yōu)先遞歸遍歷等算法
  • 能夠利用遍歷算法實現(xiàn)一些應用
  • 二、實驗內容

  • 已知二叉樹采用二叉鏈表存儲結構,如果左、右子樹非空,且左子樹根結點大于右子樹根結點,則交換根結點的左、右子樹。即按要求交換二叉樹及子樹的左、右子樹。
  • 采用二叉鏈表結構存儲一棵二叉樹,編寫一個算法統(tǒng)計該二叉樹中結點總數(shù)及葉子結點總數(shù)。
  • Tips

  • 使用遞歸算法,判斷條件交換左右字數(shù)即可
  • 遍歷過程中統(tǒng)計結點數(shù),判斷葉子結點數(shù)
  • Answer

    5.1

    //交換左右子樹的程序代碼 #include<stdio.h> //#include<malloc.h> #include<stdlib.h> //二叉鏈表的結構類型定義 const int maxsize=1024; typedef char datatype; typedef struct node {datatype data;struct node *lchild,*rchild; }bitree;bitree* creattree(); void preorder(bitree*); void swap(bitree*); void swap2(bitree*);//void main() int main() {bitree*pb;pb=creattree();preorder(pb);printf("\n");swap(pb);preorder(pb);printf("\n");return 0; }//二叉樹的建立 bitree*creattree() {char ch;bitree*Q[maxsize];int front,rear;bitree*root,*s;root=NULL;front=1;rear=0;printf("按層次輸入二叉樹,虛結點輸入'@',以'#'結束輸入:\n");while((ch=getchar())!='#'){s=NULL;if(ch!='@'){s=(bitree*)malloc(sizeof(bitree));s->data=ch;s->lchild=NULL;s->rchild=NULL;}rear++;Q[rear]=s;if(rear==1){root=s;}else{if(s&&Q[front]){if(rear%2==0){Q[front]->lchild=s;}else{Q[front]->rchild=s;}}if(rear%2==1){front++;}}}return root; }//先序遍歷按層次輸出二叉樹 void preorder(bitree*p) {if(p!=NULL){printf("%c",p->data);if(p->lchild!=NULL||p->rchild!=NULL){printf("(");preorder(p->lchild);if(p->rchild!=NULL)printf(",");preorder(p->rchild);printf(")");}} }//交換左右子樹 void swap(bitree*p) {bitree*t;if(p!=NULL){if(p->lchild!=NULL && p->rchild!=NULL && p->lchild->data > p->rchild->data){t=p->lchild;p->lchild=p->rchild;p->rchild=t;}swap(p->lchild);swap(p->rchild);} }

    5.2

    //統(tǒng)計結點總數(shù)及葉子結點總數(shù)的程序代碼 #include<stdio.h> //#include<malloc.h> #include<stdlib.h> //二叉鏈表的結構類型定義 const int maxsize=1024; typedef char datatype; typedef struct node {datatype data;struct node *lchild,*rchild; }bitree;bitree*creattree(); void preorder(bitree*); int countnode(bitree*); int countleaf(bitree*);//void main() int main() {bitree*root;int leafnum,nodenum;root=creattree();printf("刪除子樹之前的二叉樹:");preorder(root);printf("\n");nodenum=countnode(root);printf("結點總數(shù)是:%d\n",nodenum);leafnum=countleaf(root);printf("葉子結點總數(shù)是:%d\n",leafnum);return 0; }//建立二叉樹 bitree*creattree() {datatype ch;bitree*Q[maxsize];int front,rear;bitree*root,*s;root=NULL;front=1;rear=0;printf("按層次輸入結點值,虛結點輸入'@',以換行符結束:");while((ch=getchar())!='\n'){s=NULL;if(ch!='@'){s=(bitree*)malloc(sizeof(bitree));s->data=ch;s->lchild=NULL;s->rchild=NULL;}rear++;Q[rear]=s;if(rear==1){root=s;}else{if(s&&Q[front]){if(rear%2==0){Q[front]->lchild=s;}else{Q[front]->rchild=s;}}if(rear%2==1){front++;}}}return root; }//先序遍歷輸出二叉樹 void preorder(bitree*p) {if(p!=NULL){printf("%c",p->data);if(p->lchild!=NULL||p->rchild!=NULL){printf("(");preorder(p->lchild);if(p->rchild!=NULL) printf(",");preorder(p->rchild);printf(")");}} }//統(tǒng)計結點個數(shù) int countnode(bitree *p) {static int node=0;if(p!=NULL){node++;node = countnode(p->lchild);node = countnode(p->rchild);}return node; }//統(tǒng)計葉子結點個數(shù) int countleaf(bitree *p) {static int leaf=0;{if(p!=NULL){leaf = countleaf(p->lchild);if((p->lchild==NULL) && (p->rchild==NULL)){leaf = countleaf(p->rchild);}}}return leaf; }

    轉載于:https://www.cnblogs.com/vanlion/p/datastructure-exp-5.html

    總結

    以上是生活随笔為你收集整理的《软件技术基础》实验指导 实验五的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。