c语言创建一棵树函数 c语言创建一颗二叉树( 二 )


{
charComResult,GOP;
inti,j,a,b,c,d;
charStoreOP[4][2]={{'+','-'},{'*','/'},{'(',')'},{'#','#'}};
GOP=GetTopOP();
for(i=0;i4;++i)
{
for(j=0;j2;++j)
{
if(GOP==StoreOP[i][j])
{
a=i;
b=j;
}
}
}
for(i=0;i4;++i)
{
for(j=0;j2;++j)
{
if(ComChar==StoreOP[i][j])
{
c=i;
d=j;
}
}
}
if((a==0c==0)||(a==1b==0)||(a==1c==1)||(GOP==')'ComChar!='(')||(ComChar==')'GOP!=')')||(ComChar=='#'GOP!='#'))
ComResult='';
elseif((GOP==')'ComChar=='(')||(ComChar=='#'GOP!='#'))
{
printf("表达式输入错误,请重新输入!\n");
exit(-1);
}
elseif(GOP=='('ComChar==')')
ComResult='=';
else
ComResult='';
return(ComResult);
}
intIsOP(charisop)
{
switch(isop)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
return(1);
default:
return(0);
}
}
intOperate(charGetOP)
{
inta,b;
a=PopOPND();
b=PopOPND();
switch(GetOP)
{
case'+':
return(b+a);
case'-':
return(b-a);
case'*':
return(b*a);
case'/':
{
if(a==0)
{
printf("除数不能为0,请重新输入!\n");
exit(-1);
}
else
return(b/a);
}
}
}
voidmain()
{
InitStackNum();
InitStackOP();
PushOPTR('#');
printf("请输入表达式,以\"#\"结尾.\n");
charch;
ch=getchar();
while(ch!='#'||GetTopOP()!='#')
{
intsum=0;
while(IsOP(ch)==0)//是操作数
{
if(ch=='#')break;
sum=sum*10+ch-'0';
ch=getchar();
if(IsOP(ch)!=0)
{
PushOPND(sum);
}
}
switch(CompareOP(ch))
{
case'':
{
PushOPTR(ch);
ch=getchar();
break;
}
case'=':
{
PopOPTR();
ch=getchar();
break;
}
case'':
{
PushOPND(Operate(PopOPTR()));
//ch=getchar();
break;
}
}//switch
printf("所求结果是:%d\n",GetTopNum());
free(nbase);
free(obase);
}
请问C语言如何创建二叉树????创建二叉树的源程序如下:
#include cstdlib
#include stdio.h
typedef struct node
{ //树的结点
int data;
struct node* left;
struct node* right;
} Node;
typedef struct
{ //树根
Node* root;
} Tree;
void insert(Tree* tree, int value)//创建树
{
Node* node=(Node*)malloc(sizeof(Node));//创建一个节点
node-data = https://www.04ip.com/post/value;
node-left = NULL;
node-right = NULL;
if (tree-root == NULL)//判断树是不是空树
{
tree-root = node;
}
else
{//不是空树
Node* temp = tree-root;//从树根开始
while (temp != NULL)
{
if (valuetemp-data)//小于就进左儿子
{
if (temp-left == NULL)
{
temp-left = node;
return;
}
else
{//继续判断
temp = temp-left;
}
}
else {//否则进右儿子
if (temp-right == NULL)
{
temp-right = node;
return;
}
else {//继续判断
temp = temp-right;
}
}
}
}
return;
}
void inorder(Node* node)//树的中序遍历
{
if (node != NULL)
{
inorder(node-left);
printf("%d ",node-data);
inorder(node-right);
}
}
int main()
{
Tree tree;
tree.root = NULL;//创建一个空树
int n;
scanf("%d",n);
for (int i = 0; in; i++)//输入n个数并创建这个树
{
int temp;
scanf("%d",temp);

推荐阅读