生成表达式树

栈及中缀表达式转后缀表达式的实现看之前的日志

 

 

//>>>>>>mocro.h

#ifndef _MACRO_H_
#define _MACRO_H_

#define EmptyTOS (-1)
#define MinStackSize (5)
#define ElementType int

#endif

//>>>>>>struct.h

#ifndef _STRUCT_H_
#define _STRUCT_H_
#include "macro.h"

/*< stack struct */
typedef struct StackRecord
{
       int Capacity;
       int TopOfStack;
       ElementType * Array;
}STACK_RECORD;

typedef STACK_RECORD * Stack;

/*< tree struct*/
typedef struct TreeNode
{
       int        value;
       TreeNode   *     Left;
       TreeNode   *     Right;
}TreeNode;

typedef TreeNode * Tree;

#endif

//>>>>>>stack.h

#ifndef _STACK_H_
#define _STACK_H_
#include "macro.h"
#include "struct.h"

//清空栈
void MakeEmpty(Stack S);

//生成容量为MaxElements的栈
Stack CreateStack(int MaxElements);

//判断栈是否为空
int IsEmpty(Stack S);

//判断栈是否已满
int IsFull(Stack S);

//释放所有栈空间
void DisposeStack(Stack S);

//进栈
void Push(ElementType X,Stack S);

//出栈
void Pop(Stack S);

//返回栈顶数据
ElementType Top(Stack S);

//出栈并返回数值
ElementType TopAndPop(Stack S);

#endif

//>>>>>>tree.h
#ifndef _TREE_H_
#define _TREE_H_
#include "macro.h"
#include "struct.h"
#include "stack.h"

//清空树
void ClearTree(Tree t);

//创建节点
Tree CreateNode(int x);

//先序遍历
void Preorder_TreePrint(Tree t);

//中序遍历
void Inorder_TreePrint(Tree t);

//后续遍历
void Postorder_TreePrint(Tree t);

#endif

//>>>>>infix_suffix_conv.h

#ifndef _INFIX_SUFFIX_CONV_
#define _INFIX_SUFFIX_CONV_

#include "macro.h"
#include "struct.h"
#include "stack.h"

//获得符号优先级
int getLevel(char symbol);

//判断字符是否为符号
int isSymbol(char ch);

//中缀表达式转后缀表达式
void infix_suffix_convert(char * infixStr,char * suffixStr);

#endif

//>>>>>>expTree.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"tree.h"
#include "infix_suffix_conv.h"

//清空树
void ClearTree(Tree t)
{
	if(t!=NULL)
	{
		ClearTree(t->Left);
		ClearTree(t->Right);
		free(t);
	}
}
//创建节点
Tree CreateNode(int x)
{
	Tree tree = (Tree)malloc(sizeof(TreeNode));
	tree->value = x;
	tree->Left = NULL;
	tree->Right = NULL;
	return tree;
}
//先序遍历
void Preorder_TreePrint(Tree root)
{
	if(root!=NULL)
	{
		if(root->Left == NULL && root->Right == NULL)
			printf("%d ",root->value);
		else
			printf("%c ",root->value);
		Preorder_TreePrint(root->Left);
		Preorder_TreePrint(root->Right);
	}
}
//中序遍历
void Inorder_TreePrint(Tree root)
{
	if(root!=NULL)
	{
		Inorder_TreePrint(root->Left);
		if(root->Left == NULL && root->Right == NULL)
			printf("%d ",root->value);
		else
			printf("%c ",root->value);
		Inorder_TreePrint(root->Right);
	}
}
//后续遍历
void Postorder_TreePrint(Tree root)
{
	if(root!=NULL)
	{
		Postorder_TreePrint(root->Left);
		Postorder_TreePrint(root->Right);
		if(root->Left == NULL && root->Right == NULL)
			printf("%d ",root->value);
		else
			printf("%c ",root->value);
	}
}

//生成表达式树
Tree expTree(char * suffixStr)
{
	Stack treeStack = CreateStack(20);
    int iCount = strlen(suffixStr),i,j,k,flag,n;
     i=j=k=n=flag=0;
     for(i=0;i<=iCount;i++)
     {
         if(isSymbol(suffixStr[i]))
         {
			Tree tree = (Tree)malloc(sizeof(TreeNode));
            tree->value = suffixStr[i];

			if(!IsEmpty(treeStack))
				tree->Right = (Tree)TopAndPop(treeStack);
			else
				tree->Right = NULL;

			if(!IsEmpty(treeStack))
				tree->Left = (Tree)TopAndPop(treeStack);
			else
				tree->Left = NULL;

			if(!IsFull(treeStack))
				Push((int)tree,treeStack);
			else
				printf("The stack is full!\n");

			flag=2;
         }
         else if(suffixStr[i]>='0' && suffixStr[i]<='9')
         { 
			 if(flag == 2 || flag == 0)
			 {
				Tree node = CreateNode(atoi(&suffixStr[i]));
				if(!IsFull(treeStack))
					Push((int)node,treeStack);
				else
					printf("The stack is full!\n");
				flag = 3;
			 }
         }
         else
         {
             flag = 2;
         }
     } 

	 if(!IsEmpty(treeStack))
		 return (Tree)TopAndPop(treeStack);
	 else
		 return NULL;
}
main
int main(void)
{
	char * infix = "(1+2323)*55/26+83-(77+2)*32";
	char suffix[100];
	memset(suffix,0,sizeof(suffix));
	infix_suffix_convert(infix, suffix);
	puts(suffix);
	///----
	Tree root = expTree(suffix);
	Postorder_TreePrint(root);
	putchar('\n');
	ClearTree(root);
    return 0;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值