728x90
반응형
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TRUE	1
#define FALSE	0

/* Binary Tree Structure */
typedef struct _treeNode
{
	int data;
	struct _treeNode* left;
	struct _treeNode* right;
}TreeNode;

/* Stack Structure */
typedef struct _node
{
	int data;
	struct _node* next;
}Node;

typedef struct _stack
{
	Node* head;
}Stack;

/* Binary Tree Function */
TreeNode* MakeNode(void)
{
	TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
	newNode->left = NULL;
	newNode->right = NULL;
	return newNode;
}

void SetData(TreeNode* copy, int data)
{
	copy->data = data;
}

void LinkLeft(TreeNode* from, TreeNode* to)
{
	if (from->left != NULL)
	{
		free(from->left);
	}
	from->left = to;
}

void LinkRight(TreeNode* from, TreeNode* to)
{
	if (from->right != NULL)
	{
		free(from->right);
	}
	from->right = to;
}

int GetData(TreeNode* copy)
{
	return copy->data;
}

TreeNode* GetLeft(TreeNode* copy)
{
	return copy->left;
}

TreeNode* GetRight(TreeNode* copy)
{
	return copy->right;
}

typedef void (*VisitFuncPtr)(int data);

void PreorderTraverse(TreeNode* copy, VisitFuncPtr action)
{
	if (copy == NULL)
		return;
	//Unhandled exception thrown: read access violation.
	//copy was 0x36F9DBE0.
	action(copy->data);
	PreorderTraverse(copy->left, action);
	PreorderTraverse(copy->right, action);
}

void InorderTraverse(TreeNode* copy, VisitFuncPtr action)
{
	if (copy == NULL)
		return;

	PreorderTraverse(copy->left, action);
	action(copy->data);
	PreorderTraverse(copy->right, action);
}

void PostorderTraverse(TreeNode* copy, VisitFuncPtr action)
{
	if (copy == NULL)
		return;

	PreorderTraverse(copy->left, action);
	PreorderTraverse(copy->right, action);
	action(copy->data);
}

void ShowIntData(int data)
{
	printf("%d ", data);
}

/* Stack Function */

void StackInit(Stack* copy)
{
	copy->head = NULL;
}

void Push(Stack* copy, int data)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = data;
	newNode->next = copy->head;

	copy->head = newNode;
}

int Empty(Stack* copy)
{
	if (copy->head == NULL)
		return TRUE;
	else
		return FALSE;
}

int Pop(Stack* copy)
{
	Node* temp;
	int temp_data;

	if (Empty(copy))
	{
		printf("Stack Memory Error");
		exit(-1);
	}

	temp = copy->head;
	temp_data = temp->data;

	copy->head = temp->next;
	free(temp);

	return temp_data;
}

int Peek(Stack* copy)
{
	if (Empty(copy))
	{
		printf("Stack Memory Error");
		exit(-1);
	}
	return copy->head->data;
}

/* Expression Tree Function */
TreeNode* ExpTree(char exp[])
{
	Stack stack;
	TreeNode* newNode;

	int expLen = strlen(exp);
	int i;

	StackInit(&stack);

	for (i = 0; i < expLen; i++)
	{
		newNode = MakeNode();

		if (isdigit(exp[i]))
		{
			SetData(newNode, exp[i]);
		}
		else
		{
			LinkRight(newNode, Pop(&stack));
			LinkLeft(newNode, Pop(&stack));
			SetData(newNode, exp[i]);
		}
		Push(&stack, newNode);
	}
	return Pop(&stack);
}

int EvaluateExpTree(TreeNode* copy)
{
	int op1;
	int op2;

	if (GetLeft(copy) == NULL && GetRight(copy) == NULL)
	{
		return GetData(copy);
	}

	op1 = EvaluateExpTree(GetLeft(copy));
	op2 = EvaluateExpTree(GetRight(copy));

	switch (GetData(copy))
	{
	case '+':
		return op1 + op2;
	case '-':
		return op1 - op2;
	case '*':
		return op1 * op2;
	case '/':
		return op1 / op2;
	}
	return 0;
}

void ShowNodeData(int data)
{
	if (0 <= data && data <= 9)
		printf("%d ", data);
	else
		printf("%c ", data);
}

void ShowPrefixTypeExp(TreeNode* copy)
{
	PreorderTraverse(copy, ShowNodeData);
}

void ShowInfixTypeExp(TreeNode* copy)
{
	InorderTraverse(copy, ShowNodeData);
}

void ShowPostfixTypeExp(TreeNode* copy)
{
	PostorderTraverse(copy, ShowNodeData);
}



int main(void)
{
	char exp[] = "12+7*";
	TreeNode* tree = ExpTree(exp);

	printf("전위 표기법의 수식: ");
	ShowPrefixTypeExp(tree);
	printf("\n");

	printf("중위 표기법의 수식: ");
	ShowInfixTypeExp(tree);
	printf("\n");

	printf("후위 표기법의 수식: ");
	ShowPostfixTypeExp(tree);
	printf("\n");

	printf("연산의 결과: %d \n", EvaluateExpTree(tree));

	return 0;
}
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

[Sort]Bubble, Selection, Insertion  (0) 2022.10.31
Basic Heap.(Priority Queue and Heap)  (0) 2022.10.31
exp_tree  (0) 2022.10.31
Expression Tree  (0) 2022.10.31
Binary Tree  (0) 2022.10.30

+ Recent posts