2015. 2. 10. 18:16 Programing/C++

Tree 구현하기

#include <iostream>

using namespace std;


char input[200];

char heap[200];

int cnt_1=0;

int cusor = 1;

int depth_cnt = 1;

int depth_check = 0;

int depth_temp;

int depth = 0;

int k_1 = 1;

int k = 1;//초기값 1

void print_1(int _n);

void print_2(int _n);


int jegop(int _n)

{

 int k = 1;

 int n = _n;

 for(int i = 0 ; i < n; i++)

  k *= 2;

 return k;

}

void ancestor_sort(int _i, int _j)

{

 int i = _i; //i보내는거

 int n = _j; //cusor 보내는거

 int cnt=0;

 char temp;

 if(cusor != 0)

 {

  if((i%2) == 1) //홀수고 왼쪽 노드

  {

   cusor = ((i-1)/2);

   if(heap[i] < heap[cusor])

   {

    temp = heap[cusor];

    heap[cusor] = heap[i];

    heap[i] = temp;

    ancestor_sort(cusor, cusor);

   }

   }

  else if((i%2) == 0) //짝수고 오른쪽 노드

  {

   cusor = ((i-2)/2);

    if(heap[i] < heap[cusor])

   {

    temp = heap[cusor];

    heap[cusor] = heap[i];

    heap[i] = temp;

    ancestor_sort(cusor, cusor);

   }

  }

 }

 cusor = 1;

}

void sort(char *_c,int _arr_size) //'A'랑 'D'는 없는 상태

{

 int arr_size = _arr_size;

 int n = (arr_size/3)+1;

 for(int i = n-1 ; i >= 0; i--)

 {

  ancestor_sort(i,1);

 }

}

void insert(char *_c, int _arr_size) //들어온게 input

{

 char *c = _c;

 int arr_size = _arr_size;

 int count = 0;

 for(int i = 0; i < arr_size; i++)

 {

  if(c[i] == 'A')

  {

   if(c[i+1] != ' '){

   heap[count] = c[i+1];

   count ++;

   }

  }

  else if (c[i] == 'D')

  {

   if(c[i+1] != ' ')

   for(int j=0; j <arr_size; j++)

    if(c[i+1] == heap[j])

     heap[j] = NULL;

  }

 }

 sort(heap, arr_size);

}

void main()

{

 int n;

 cin >> n;

// char input[500]; //입력받은 문자열들이 저장되는 배열

 int i_int; //getchar를 받기 위해 있는 변수

 int arr_size = (n * 3)-1;

 int cnt=0;

 fflush(stdin);

 while((i_int=getchar()) != '\n')

 {

  input[cnt] = i_int;

  cnt++;

 }

 insert(input, arr_size); //cnt 안받기로 했음

 

 cout<<"Original Form"<<endl;

 print_1(n);

}


void print_1(int _n)

{

 int n = _n;

 for(int i = 0; i < n; i++)

 {

  if(jegop(i) <= n)

   depth = i;

 }

 cout << depth <<endl;

 int depth_save = depth;

 int arr_count=0;

 int count = 0;

 

 char **arr = new char *[jegop((depth+1))-1];

 for(int i = 0; i < (jegop(depth+1))-1; i++)

  arr[i] = new char[depth];


 for(int i = 0 ; i <= depth; i++)

  for(int j =0; j<= jegop(depth+1)-1; j++)

   arr[i][j] = ' ';



 int cnt=0;

 int depth_check = 0;

 int d1, d2, d3;

 d1 = jegop(depth) -1;

 

 for(int i = 0; i < n ; i++)

 {

  if(depth_check == 0)

  {

   arr[count][d1] = heap[i];

   cnt++;

   depth_check ++;

   }

  else 

  {

   arr[count][d1+d2] = heap[i];

   d2 = d2 + d3;

   cnt++;

  }


  if(cnt == k)

  {

   d2 = d1+1;

   d3 = d2;

      depth--;

   d1 = jegop(depth)-1;

   depth_check = 0;

   cnt = 0;

   count++;

   k = jegop(k); 

  }

  

  

 }

   /*  cout << endl;

  char **arr_1 = new char *[depth];

     for(int i = 0; i < depth; i++)

  arr_1[i] = new char[jegop((depth+1))-1];

  */



 for(int i = 0 ; i <= depth_save; i++)

 {

  for(int j =0; j<= jegop(depth_save+1)-1; j++)

  {

   cout << arr[i][j];

  } 

  cout << endl;

 }

 cout << endl;

 cout << "Lotation Form" << endl;

  for(int i = 0 ; i <= jegop(depth_save+1)-1; i++)

  {

   for(int j =0; j<= depth_save; j++)

  {

   cout<< arr[j][i];

  } 

  cout << endl;

     }


 

 



}

'Programing > C++' 카테고리의 다른 글

구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
이차원 동적 배열 입력 받아 생성하기  (0) 2015.02.10
C++ 문자형을 int형으로  (0) 2015.02.10
Posted by kimmayer

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.3
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함