728x90
반응형
/* Basic Binary Tree */

#include <stdio.h>
#include <stdlib.h>

typedef struct _treeNode
{
	int data;
	struct _treeNode* left;
	struct _treeNode* right;
}TreeNode;

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

/* InorderTraverse */
//void InorderTraverse(TreeNode* copy)
//{
//	if (copy == NULL)
//		return;
//
//	InorderTraverse(copy->left);
//	printf("%d \n", copy->data);
//	InorderTraverse(copy->right);
//}

/* PreorderTraverse */
//void PreorderTraverse(TreeNode* copy)
//{
//	if (copy == NULL)
//		return;
//
//	pritnf("%d \n", copy->data);
//	PreorderTraverse(copy->left);
//	PreorderTraverse(copy->right);
//}

/* PostorderTraverse */
//void PostorderTraverse(TreeNode* copy)
//{
//	if (copy == NULL)
//		return;
//
//	PostorderTraverse(copy->left);
//	PostorderTraverse(copy->right);
//	printf("%d \n", copy->data);
//}

typedef void (*VisitFuncPtr)(int data);
/* InorderTraverse + Function pointer */
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);
}


int main(void)
{
	TreeNode* a = MakeNode();
	TreeNode* b = MakeNode();
	TreeNode* c = MakeNode();
	TreeNode* d = MakeNode();
	TreeNode* e = MakeNode();
	TreeNode* f = MakeNode();

	SetData(a, 1);
	SetData(b, 2);
	SetData(c, 3);
	SetData(d, 4);
	SetData(e, 5);
	SetData(f, 6);

	LinkLeft(a, b);
	LinkRight(a, c);
	LinkLeft(b, d);
	LinkRight(b, e);
	LinkRight(c, f);

	//printf("%d\n", GetData(a));			// 1
	//printf("%d\n", GetData(GetLeft(a)));	// 2
	//printf("%d\n", GetData(GetRight(a)));	// 3
	//printf("%d\n", GetData(GetLeft(GetLeft(a))));	// 4

	// Inorder Traverse (4 2 5 1 3 6)
	//InorderTraverse(a);

	PreorderTraverse(a, ShowIntData);	//1 2 4 5 3 6
	printf("\n");
	InorderTraverse(a, ShowIntData);	//2 4 5 1 3 6
	printf("\n");
	PostorderTraverse(a, ShowIntData);	//2 4 5 3 6 1

	return 0;
}
728x90
반응형

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

exp_tree  (0) 2022.10.31
Expression Tree  (0) 2022.10.31
Hambuger Simulator (using queue)  (0) 2022.10.29
Circular Queue(based in Array)  (0) 2022.10.29
Spinning Donuts  (0) 2022.10.28

+ Recent posts