Room No. 10-414
Name:
Date: 2018/11/21
Program Name: binary tree traversal and the expression evaluates
Experiment: (b) binary tree traversal
First, the purpose and requirements (needs analysis):
1, the deposit of the binary expression arrays, recursively expression of the binary tree structure, respectively, and then outputs
a preamble, in the sequence and postorder traversals result, and calculate the result of the expression.
Second, the basic ideas, principles and design program description:
(Including the structure of the program, data structures, input / output design, etc. Symbolic Name Description)
1. Program structure: a preorder, preorder, postorder, preorder binary construct, the expression is evaluated configuration function, main constituting the main function.
2. Principle:
1. preorder traversal of a binary tree operation: If the binary tree is empty, the empty; otherwise (1) access to the root node; (2) preorder left subtree; (3) preorder right subtree .
the printf ( "% C", T-> Data);
FirstTransverse (T-> lchild);
FirstTransverse (T-> rchild);
2. binary tree in preorder operation: If the binary tree is empty, the empty; otherwise (1 ) inorder traversal left subtree; (2) access to the root node; (3) in order to traverse the right subtree.
MidTransverse (T-> lchild);
the printf ( "% C", T-> Data);
MidTransverse (T-> rchild);
3. postorder traversal of a binary tree: if the binary tree is empty, the empty; otherwise; ( ) after a preorder left subtree; (2) the right subtree preorder; (3) access to the root node.
FirstTransverse (T-> lchild);
FirstTransverse (T-> rchild);
the printf ( "% C", T-> Data);
Third, the problem of debugging and running programs generated in the process and the measures taken:
1. After traversing the timing, because the wrong position about the sub-tree, causing the output error, debugging for a long time, finally found the error and corrected.
2. When calculating the value of the expression written, encountered a bottleneck, the problem still can not find a program where, also tried other methods, did not run successfully evaluate the expression.
Fourth, the source code and comments:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
char data;
struct Node *lchild, *rchild;
}Node;//构建结构体
void FirstTransverse(Node *t){
if(t==NULL){
return;
}
printf("%c",t->data);
FirstTransverse(t->lchild);
FirstTransverse(t->rchild);
}//先序遍历
void MidTransverse(Node *t){
if(t==NULL){
return;
}
MidTransverse(t->lchild);
printf("%c",t->data);
MidTransverse(t->rchild);
} //中序遍历
void LastTransverse(Node *t){
if(t==NULL){
return;
}
FirstTransverse(t->lchild);
FirstTransverse(t->rchild);
printf("%c",t->data);
} //后序遍历
Node *PreCreateBitree(Node *t){
char ch;
ch=getchar();
if(ch=='#'){ //空标志
t=NULL;
}
else{
t=(Node *)malloc(sizeof(Node));
t->data=ch;
t->lchild=PreCreateBitree(t->lchild);
t->rchild=PreCreateBitree(t->rchild);
}
return t;
}//利用先序遍历构建二叉树
int calculate(Node *t)
{
printf("%c",t->data);
FirstTransverse(t->lchild);
FirstTransverse(t->rchild);
printf("=");
if(t==NULL)
{
return 0;
}
if(t->data <='9'&&t->data >='0')//数据为数字时
{
return (t->data-'0');
}
else
{
switch(t->data)//运算符号的情况
{
case'+': return calculate(t->lchild) + calculate(t->rchild); // +
case'-': return calculate(t->lchild) - calculate(t->rchild); // -
case'*': return calculate(t->lchild) * calculate(t->rchild); // *
case'/': return calculate(t->lchild) / calculate(t->rchild); // /
}
}
printf("%c",t->lchild);
}
int main()
{
Node *t=NULL;
t=PreCreateBitree(t);
printf("\n先序遍历:\n");
FirstTransverse(t); //先序遍历
printf("\n\n中序遍历:\n");
MidTransverse(t);//中序遍历
printf("\n\n后序遍历:\n");
LastTransverse(t);// 后序遍历
printf("\n\n计算表达式:\n");
calculate(t); //计算表达式
printf("\n");
return 0;
}
6.3.5 Operating output:
Sixth, ideas and experience:
Failed to complete the calculation of expression