//아카데미에서 공부한답시고 대충 만든거라 문제 많다는 것이 큰 문제...

//기본적으로 링크드 리스트의 개념에 대해서 알아야지 이해가 됨.


#include <iostream>


using namespace std;


struct Tree

{

Tree *Forward;//앞 동적 메모리 주소값을 가진다.

Tree *Backward;//뒤 동적 메모리 주소값을 가진다.

int Data;//현 메모리가 가진 데이터의 값.

};


//사용하기 위해 선언한 함수들.

void Add();

void Insert(Tree* temp, int data);

void Display();

void Print(Tree* temp);

void Serch();

void Serch(Tree* temp, int data);

void All_Delete();

void All_Delete(Tree* temp);


//위 트리 구조체의 구조체 포인터 변수를 선언!

Tree* Head;


void main()

{

int exit = 1;


while (exit)

{

int select = 0;


cout << "이진 트리 예제" << endl;

cout << "데이터 입력(1) / 데이터 출력(2) / 데이터 검색(3) " << endl;

cout << "데이터 전체 삭제(4) / 프로그램 종료(6) : ";

cin >> select;



switch (select)

{

case 1 :

Add();

break;

case 2 :

Display();

break;

case 3 :

Serch();

break;

case 4 :

All_Delete();

break;

case 6 :

All_Delete();

exit = 0;

break;

}

}

}


void Add()

{

int input = 0;


cout << "데이터 입력(정수만 가능) : ";

cin >> input;


if (Head == 0)

{

Head = new Tree;

Head->Data = input;

Head->Forward = 0, Head->Backward = 0;

}

else

{

Tree* Temp = Head;

Insert(Temp, input);

}

}


void Insert(Tree* temp, int data)

{

if (temp->Data > data)

{

if (temp->Forward != 0)

{

Insert(temp->Forward, data);

}

else

{

Tree* Temp = new Tree;

Temp->Data = data;

temp->Forward = Temp, Temp->Forward = 0, Temp->Backward = 0;

}

}

else

{

if (temp->Backward != 0)

{

Insert(temp->Backward, data);

}

else

{

Tree* Temp = new Tree;

Temp->Data = data;

temp->Backward = Temp, Temp->Forward = 0, Temp->Backward = 0;

}

}

}


void Display()

{

// int display_out = 1;

if (Head == 0)

{

cout << "데이터가 없습니다." << endl;

}

else

{

cout << "입력된 데이터" << endl;

Print(Head);

cout << "데이터 출력 완료" << endl;

}

}


void Print(Tree* temp)

{

if (temp != 0)

{

Print(temp->Forward);

cout << temp->Data << endl;

Print(temp->Backward);

}

}


void Serch()

{

int input = 0;


cout << "찾을 데이터를 입력해주십시오" << endl;

cout << "입력 : ";

cin >> input;


Serch(Head, input);

}


void Serch(Tree* temp, int data)

{

if(temp->Data > data)

{

if (temp->Forward != 0)

{

Serch(temp->Forward, data);

}

else

{

cout << "데이터 검색 완료 " << endl;

cout << "데이터값 " << data << "의 존재가 확인되지 않습니다." << endl;

}

}

else if (temp->Data < data)

{

if (temp->Backward != 0)

{

Serch(temp->Backward, data);

}

else

{

cout << "데이터 검색 완료 " << endl;

cout << "데이터값 " << data << "의 존재가 확인되지 않습니다." << endl;

}

}

else if (temp->Data == data)

{

cout << "데이터 검색 완료 " << endl;

cout << "데이터값 " << data << "의 존재가 확인되었습니다." << endl;

}

}


void All_Delete()

{

cout << "모든 데이터 삭제 중입니다. 잠시만 기다려 주십시오" << endl;

All_Delete(Head);

cout << "작업 완료." << endl;

Head = 0;

}


void All_Delete(Tree* temp)

{

if (temp != 0)

{

All_Delete(temp->Forward);

All_Delete(temp->Backward);

cout << temp->Data << "삭제완료\n";

delete temp;

}

}

'[C], [ C++]' 카테고리의 다른 글

퀵 정렬 - Quick Sort  (0) 2015.05.28
삽입 정렬 - Insert Sort  (0) 2015.05.28
버블 정렬(Bubble Sort)  (0) 2015.05.28
이진 검색 트리 알고리즘2  (0) 2015.05.28
이진 검색 트리 알고리즘(배열, 재귀함수)  (0) 2015.05.28

+ Recent posts