728x90
반응형
// Expression Tree
#include <stdio.h>
#include <stdlib.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;
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;
}
int main(void)
{
return 0;
}
728x90
반응형
'Data Structure & Algorithm' 카테고리의 다른 글
Basic Heap.(Priority Queue and Heap) (0) | 2022.10.31 |
---|---|
Expression Tree(merger) (0) | 2022.10.31 |
Expression Tree (0) | 2022.10.31 |
Binary Tree (0) | 2022.10.30 |
Hambuger Simulator (using queue) (0) | 2022.10.29 |