《软件技术基础》实验指导 实验五
生活随笔
收集整理的這篇文章主要介紹了
《软件技术基础》实验指导 实验五
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
樹
實驗五 樹
一、實驗目的
二、實驗內(nèi)容
Tips
Answer
5.1
//交換左右子樹的程序代碼 #include<stdio.h> //#include<malloc.h> #include<stdlib.h> //二叉鏈表的結(jié)構(gòu)類型定義 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("按層次輸入二叉樹,虛結(jié)點輸入'@',以'#'結(jié)束輸入:\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)計結(jié)點總數(shù)及葉子結(jié)點總數(shù)的程序代碼 #include<stdio.h> //#include<malloc.h> #include<stdlib.h> //二叉鏈表的結(jié)構(gòu)類型定義 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("結(jié)點總數(shù)是:%d\n",nodenum);leafnum=countleaf(root);printf("葉子結(jié)點總數(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("按層次輸入結(jié)點值,虛結(jié)點輸入'@',以換行符結(jié)束:");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)計結(jié)點個數(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)計葉子結(jié)點個數(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; }轉(zhuǎn)載于:https://www.cnblogs.com/vanlion/p/datastructure-exp-5.html
總結(jié)
以上是生活随笔為你收集整理的《软件技术基础》实验指导 实验五的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ruby编程实践
- 下一篇: 自动化来势汹汹,未来的程序员该何去何从?