生成表達式樹

棧及中綴表達式轉後綴表達式的實現看之前的日誌

 

 

//>>>>>>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;
}
 

 

發佈了87 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章