构造一棵表达树
把后缀表达式转变成表达树,然后再后序遍历表达数,把后序遍历后的结果输出。这里,要配合两种数据结构,第一就是栈,第二就是二叉树。所以,我把这些不同的数据结构分别放进不同的.c文件作为不同的模块,还有一个模块是核心模块,就是把后缀表达式转变成表达树的功能。最后,main.c里面存放的只是测试代码。
栈的头文件:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#ifndef _STACK_H
#define _STACK_H
#include "btree.h"
typedef Tree StackType;
struct StackRecord;
typedef struct StackRecord *Stack;
int IsEmpty(Stack s);
Stack CreateStack(void);
void DestroyStack(Stack s);
void MakeEmpty(Stack s);
void Push(StackType x, Stack s);
StackType Pop(Stack s);
StackType Top(Stack s);
void PrintStack(Stack s);
#endif
栈的实现函数:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stack.h"
#define MAXSIZE 100
#define SAFE_FREE(s) {if (s) {free(s); s = NULL;} }
struct StackRecord
{
int capacity;
int topOfStack;
StackType *array;
};
int IsEmpty(Stack s)
{
return s->topOfStack == -1;
}
Stack CreateStack(void)
{
Stack s = malloc(sizeof(struct StackRecord));
if (s == NULL)
{
fprintf(stderr, "create stack failed");
return NULL;
}
s->array = malloc(MAXSIZE * sizeof(StackType));
if (s->array == NULL)
{
fprintf(stderr, "create stack failed");
return NULL;
}
s->capacity = MAXSIZE;
s->topOfStack = -1;
return s;
}
void DestroyStack(Stack s)
{
if (s == NULL)
{
fprintf(stderr, "stack is empty");
return;
}
SAFE_FREE(s->array);
SAFE_FREE(s);
}
void MakeEmpty(Stack s)
{
s->topOfStack = -1;
}
void Push(StackType x, Stack s)
{
if (s == NULL)
{
fprintf(stderr, "stack is empty");
return;
}
if ((s->topOfStack + 1) == s->capacity)
{
fprintf(stderr, "stack is full.");
return;
}
s->topOfStack++;
s->array[s->topOfStack] = x;
}
StackType Pop(Stack s)
{
if (s->topOfStack == -1)
{
fprintf(stderr, "stack is empty.");
return NULL;
}
StackType topValue = s->array[s->topOfStack];
s->array[s->topOfStack] = 0;
s->topOfStack--;
return topValue;
}
StackType Top(Stack s)
{
if (s->topOfStack == -1)
{
fprintf(stderr, "stack is empty.");
return NULL;
}
return s->array[s->topOfStack];
}
二叉树的头文件:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#ifndef BTREE_H
#define BTREE_H
typedef int ElementType;
struct TreeNode;
typedef struct TreeNode *PtrToNode;
typedef PtrToNode Tree;
Tree CreateTree(ElementType value);
void SetRightChild(Tree t, PtrToNode child);
void SetLeftChild(Tree t, PtrToNode child);
void postfixTree(Tree t, char *postfix);
void DestroyTree(Tree *t);
#endif
二叉树实现文件:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btree.h"
#define SAFE_FREE(t) {if(t) {free(t); t = NULL;}}
struct TreeNode
{
ElementType x;
Tree left;
Tree right;
};
Tree CreateTree(ElementType value)
{
Tree t = malloc(sizeof(struct TreeNode));
if (t == NULL)
{
fprintf(stderr, "create tree failed.");
return NULL;
}
t->x = value;
t->left = t->right = NULL;
return t;
}
void SetLeftChild(Tree t, PtrToNode child)
{
t->left = child;
}
void SetRightChild(Tree t, PtrToNode child)
{
t->right = child;
}
void postfixTree(Tree t, char *postfix)
{
static int size = 0;
if (t != NULL)
{
postfixTree(t->left, postfix);
postfixTree(t->right, postfix);
postfix[size++] = t->x;
}
return;
}
void DestroyTree(Tree *t)
{
if (*t != NULL)
{
DestroyTree(&(*t)->left);
DestroyTree(&(*t)->right);
SAFE_FREE(*t);
}
}
表达式转变成表达树的接口文件:
#ifndef EXPRESSION_TREE_H
#define EXPRESSION_TREE_H
Tree CreateExpressionTree(const char *postfix, const int len);
#endif
表达式转变成表达树的实现文件:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <ctype.h>
#include "stack.h"
#include "btree.h"
static int IsSymbol(const char ch)
{
if (ch == '+' || ch == '*')
{
return 1;
}
return 0;
}
Tree CreateExpressionTree(const char *postfix, const int len)
{
Stack s = CreateStack();
for (int i = 0; i < len; ++i)
{
if (isdigit(postfix[i]) )
{
Tree t = CreateTree(postfix[i]);
Push(t, s);
}
else if (IsSymbol(postfix[i]) )
{
Tree root = CreateTree(postfix[i]);
SetRightChild(root, Pop(s));
SetLeftChild(root, Pop(s));
Push(root, s);
}
}
Tree expTree = Pop(s);
DestroyStack(s);
return expTree;
}
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <stdio.h>
#include <string.h>
#include "btree.h"
#include "expression_tree.h"
#define MAXSIZE 100
int main(void)
{
char result[MAXSIZE] = {0};
char postfix[MAXSIZE] = {0};
printf("Enter postfix expression('quit' to exit):\n");
memset(postfix, 0x00, sizeof(postfix));
fgets(postfix, MAXSIZE, stdin);
postfix[strlen(postfix)] = 0;
Tree t = CreateExpressionTree(postfix, strlen(postfix));
memset(result, 0x00, sizeof(result));
postfixTree(t, result);
printf("postfix the tree: %s\n", result);
DestroyTree(&t);
return 0;
}
ken@Linux:~/DSAA/chap4/binary_tree$ ./expression
Enter postfix expression('quit' to exit):
12+345+**
postfix the tree: 12+345+**