Study

tree(queue로 구현)

슈라。 2008. 12. 16. 16:40

//tree.h
#ifndef TREE_DEFINE
#define TREE_DEFINE

#include <stdio.h>
#include <malloc.h>
#include "Queue.h"
#include <conio.h>
#include <windows.h>

typedef struct _TREENODE
{
 void * m_pData;
 int      m_iLevel;
 int      m_xPos;
 struct _TREENODE * m_pLeft;
 struct _TREENODE * m_pRight;
}TREENODE;

void GotoXY(int x,int y);
void InsertTreeData(TREENODE ** ppRoot,void * pData,int (* CompareFunc)(void *,void *));
TREENODE * SearchTreeData(TREENODE * pStart,void * pData,int (* CompareFunc)(void *,void *));
void PreOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ));
void InOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ));
void PostOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ));
void LevelOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ));
#endif

//tree.cpp
#include "Tree.h"

void GotoXY(int x,int y)
{
 HANDLE hOut;
 COORD Cur;
 
 hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 Cur.X = x;
 Cur.Y = y;
 SetConsoleCursorPosition(hOut,Cur);
}

void InsertTreeData(TREENODE ** ppRoot,void * pData,int (* CompareFunc)(void *,void *))
{
 TREENODE * pNode = (TREENODE *)malloc(sizeof(TREENODE));
 pNode->m_pLeft = NULL;
 pNode->m_pRight = NULL;
 pNode->m_pData = pData;

 if (*ppRoot == NULL)
 {
  *ppRoot = pNode;
  return;
 }
 TREENODE * pStart = *ppRoot;
 while (pStart)
 {
  if (CompareFunc(pStart->m_pData,pData) > 0)
  {
   if (pStart->m_pLeft == NULL)
   {
    pStart->m_pLeft = pNode;
    return;
   }
   pStart = pStart->m_pLeft;
  }
  else
  {
   if (pStart->m_pRight == NULL)
   {
    pStart->m_pRight = pNode;
    return;
   }
   pStart = pStart->m_pRight;
  }
 }
}

TREENODE * SearchTreeData(TREENODE * pStart,void * pData,int (* CompareFunc)(void *,void *))
{
 while (pStart)
 {
  int iResult = CompareFunc(pStart->m_pData,pData);
  if ( iResult == 0)
  {
   return pStart;
  }
  else if (iResult > 0)
  {
   pStart = pStart->m_pLeft;
  }
  else
  {
   pStart = pStart->m_pRight;
  }
 }
 return NULL;
}

void PreOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ))
{
 if (pStart == NULL)
 {
  return;
 }
 PrintFunc(pStart->m_pData);
 PreOrderPrint(pStart->m_pLeft,PrintFunc);
 PreOrderPrint(pStart->m_pRight,PrintFunc);
}

void InOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ))
{
 if (pStart == NULL)
 {
  return;
 }
 InOrderPrint(pStart->m_pLeft,PrintFunc);
 PrintFunc(pStart->m_pData);
 InOrderPrint(pStart->m_pRight,PrintFunc);
}

void PostOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ))
{
 if (pStart == NULL)
 {
  return;
 }
 PostOrderPrint(pStart->m_pLeft,PrintFunc);
 PostOrderPrint(pStart->m_pRight,PrintFunc);
 PrintFunc(pStart->m_pData);
}

void LevelOrderPrint(TREENODE * pStart,void (* PrintFunc)(void * ))
{
 system("cls");
 NODE * pQueueTop = NULL;
 NODE * pQueueBottom = NULL;
 if (pStart)
 {
  pStart->m_iLevel = 1;
  pStart->m_xPos = 32;
  EnQueue(&pQueueTop,&pQueueBottom,pStart);
 }
 void * pData;
 int xPos = 32;
 int yPos = 0;
 int iLevel = 1;
 while (DeQueue(&pQueueTop,&pQueueBottom,&pData))
 {
  TREENODE * pNode = (TREENODE *)pData;
  if (iLevel != pNode->m_iLevel)
  {
   yPos += 2;
   iLevel = pNode->m_iLevel;
   xPos /= 2;
  }
  GotoXY(pNode->m_xPos,yPos);
  PrintFunc(pNode->m_pData);
  if (pNode->m_pLeft)
  {
   pNode->m_pLeft->m_iLevel = pNode->m_iLevel + 1;
   pNode->m_pLeft->m_xPos = pNode->m_xPos - xPos / 2;
   EnQueue(&pQueueTop,&pQueueBottom,pNode->m_pLeft);
  }
  if (pNode->m_pRight)
  {
   pNode->m_pRight->m_iLevel = pNode->m_iLevel + 1;
   pNode->m_pRight->m_xPos = pNode->m_xPos + xPos / 2;
   EnQueue(&pQueueTop,&pQueueBottom,pNode->m_pRight);
  }
 }
 getch();
 puts("");
}

'Study' 카테고리의 다른 글

yum을 이용한 패키지 관리  (0) 2009.01.12
리눅스 텔넷 서버  (0) 2009.01.11
queue(double linked list로 구현)  (0) 2008.12.16
stack (double linked list로 구현)  (0) 2008.12.16
double linked list  (0) 2008.12.16