#include<stdio.h>
#define TREEMAX 100
typedef struct BT
{
char data;
BT *lchild;
BT *rchild;
}BT;
BT *CreateTree();
void Preorder(BT *T);
void Postorder(BT *T);
void Inorder(BT *T);
void Leafnum(BT *T);
void Nodenum(BT *T);
int TreeDepth(BT *T);
int count=0;
void main()
{
BT *T=NULL;
char ch1,ch2,a;
ch1='y';
while(ch1=='y'||ch1=='y')
{
printf("\n");
printf("\n\t\t 二叉樹子系統(tǒng)");
printf("\n\t\t*****************************************");
printf("\n\t\t 1---------建二叉樹 ");
printf("\n\t\t 2---------先序遍歷 ");
printf("\n\t\t 3---------中序遍歷 ");
printf("\n\t\t 4---------后序遍歷 ");
printf("\n\t\t 5---------求葉子數(shù) ");
printf("\n\t\t 6---------求結(jié)點數(shù) ");
printf("\n\t\t 7---------求樹深度 ");
printf("\n\t\t 0---------返 回 ");
printf("\n\t\t*****************************************");
printf("\n\t\t 請選擇菜單號 (0--7)");
scanf("%c",&ch2);
getchar();
printf("\n");
switch(ch2)
{
case'1':
printf("\n\t\t請按先序序列輸入二叉樹的結(jié)點:\n");
printf("\n\t\t說明:輸入結(jié)點(‘0’代表后繼結(jié)點為空)后按回車。\n");
printf("\n\t\t請輸入根結(jié)點:");
T=CreateTree();
printf("\n\t\t二叉樹成功建立!\n");break;
case'2':
printf("\n\t\t該二叉樹的先序遍歷序列為:");
Preorder(T);break;
case'3':
printf("\n\t\t該二叉樹的中序遍歷序列為:");
Inorder(T);break;
case'4':
printf("\n\t\t該二叉樹的后序遍歷序列為:");
Postorder(T);break;
case'5':
count=0;Leafnum(T);
printf("\n\t\t該二叉樹有%d個葉子。\n",count);break;
case'6':
count=0;Nodenum(T);
printf("\n\t\t該二叉樹總共有%d個結(jié)點。\n",count);break;
case'7':
printf("\n\t\t該樹的深度為:%d",TreeDepth(T));
break;
case'0':
ch1='n';break;
default:
printf("\n\t\t***請注意:輸入有誤!***");
}
if(ch2!='0')
{
printf("\n\n\t\t按【Enter】鍵繼續(xù),按任意鍵返回主菜單!\n");
a=getchar();
if(a!='\xA')
{
getchar();
ch1='n';
}
}
}
}
BT *CreateTree()
{
BT *t;
char x;
scanf("%c",&x);
getchar();
if(x=='0')
t=NULL;
else
{
t=new BT;
t->data=x;
printf("\n\t\t請輸入%c結(jié)點的左子結(jié)點:",t->data);
t->lchild=CreateTree();
printf("\n\t\t請輸入%c結(jié)點的右子結(jié)點:",t->data);
t->rchild=CreateTree();
}
return t;
}
void Preorder(BT *T)
{
if(T)
{
printf("%3c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Inorder(BT *T)
{
if(T)
{
Inorder(T->lchild);
printf("%3c",T->data);
Inorder(T->rchild);
}
}
void Postorder(BT *T)
{
if(T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%3c",T->data);
}
}
void Leafnum(BT *T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
count++;
Leafnum(T->lchild);
Leafnum(T->rchild);
}
}
void Nodenum(BT *T)
{
if(T)
{
count++;
Nodenum(T->lchild);
Nodenum(T->rchild);
}
}
int TreeDepth(BT *T)
{
int ldep,rdep;
if(T==NULL)
return 0;
else
{
ldep=TreeDepth(T->lchild);
rdep=TreeDepth(T->rchild);
if(ldep>rdep)
return ldep+1;
else
return rdep+1;
}
}
標簽:
二叉樹
子系統(tǒng)
上傳時間:
2020-06-11
上傳用戶:ccccy
#include <stdio.h>
#include <stdlib.h>
#define SMAX 100
typedef struct SPNode
{
int i,j,v;
}SPNode;
struct sparmatrix
{
int rows,cols,terms;
SPNode data [SMAX];
};
sparmatrix CreateSparmatrix()
{
sparmatrix A;
printf("\n\t\t請輸入稀疏矩陣的行數(shù),列數(shù)和非零元素個數(shù)(用逗號隔開):");
scanf("%d,%d,%d",&A.cols,&A.terms);
for(int n=0;n<=A.terms-1;n++)
{
printf("\n\t\t輸入非零元素值(格式:行號,列號,值):");
scanf("%d,%d,%d",&A.data[n].i,&A.data[n].j,&A.data[n].v);
}
return A;
}
void ShowSparmatrix(sparmatrix A)
{
int k;
printf("\n\t\t");
for(int x=0;x<=A.rows-1;x++)
{
for(int y=0;y<=A.cols-1;y++)
{
k=0;
for(int n=0;n<=A.terms-1;n++)
{
if((A.data[n].i-1==x)&&(A.data[n].j-1==y))
{
printf("%8d",A.data[n].v);
k=1;
}
}
if(k==0)
printf("%8d",k);
}
printf("\n\t\t");
}
}
void sumsparmatrix(sparmatrix A)
{
SPNode *p;
p=(SPNode*)malloc(sizeof(SPNode));
p->v=0;
int k;
k=0;
printf("\n\t\t");
for(int x=0;x<=A.rows-1;x++)
{
for(int y=0;y<=A.cols-1;y++)
{
for(int n=0;n<=A.terms;n++)
{
if((A.data[n].i==x)&&(A.data[n].j==y)&&(x==y))
{
p->v=p->v+A.data[n].v;
k=1;
}
}
}
printf("\n\t\t");
}
if(k==1)
printf("\n\t\t對角線元素的和::%d\n",p->v);
else
printf("\n\t\t對角線元素的和為::0");
}
int main()
{
int ch=1,choice;
struct sparmatrix A;
A.terms=0;
while(ch)
{
printf("\n");
printf("\n\t\t 稀疏矩陣的三元組系統(tǒng) ");
printf("\n\t\t*********************************");
printf("\n\t\t 1------------創(chuàng)建 ");
printf("\n\t\t 2------------顯示 ");
printf("\n\t\t 3------------求對角線元素和");
printf("\n\t\t 4------------返回 ");
printf("\n\t\t*********************************");
printf("\n\t\t請選擇菜單號(0-3):");
scanf("%d",&choice);
switch(choice)
{
case 1:
A=CreateSparmatrix();
break;
case 2:
ShowSparmatrix(A);
break;
case 3:
SumSparmatrix(A);
break;
default:
system("cls");
printf("\n\t\t輸入錯誤!請重新輸入!\n");
break;
}
if (choice==1||choice==2||choice==3)
{
printf("\n\t\t");
system("pause");
system("cls");
}
else
system("cls");
}
}
標簽:
數(shù)組
子系統(tǒng)
上傳時間:
2020-06-11
上傳用戶:ccccy