디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

형들 C언어 한번만 도와주세요 ㅠ

;;(121.128) 2009.12.14 17:27:35
조회 76 추천 0 댓글 0




이번에 데이터 스트럭쳐 과목 때문에, 레드 블랙 트리를 만들어 내야 하는데

씨언어도 제대로 배운적도 없는데 어떻게 어떻게 해서 여기까지 진행했는데, 로테이션을 어떻게 해야할지 막막해..

정확히는.. 로테이션을 하면 PRINT가 안되는 상황이 발생하고 있어..

포인터 때문에 그런거 같은데.. 형들 제발 도와주세요 ㅠ

void insertElement(Node ** root, int newElement)
{
 Node *nparent;
 Node *uncle;
 Node *gparent;
 Node *temp;
 Node *temp2;
 Node *temp3;
 Node *temp4;
 if (root == NULL)
 {
  printf("Error no root pointer\\n");
  return;
 }
 nparent = NULL;
 uncle = NULL;
 gparent = NULL;
 temp = NULL;
 temp2 = NULL;
 temp3 = NULL;
 temp4 = NULL;
 while (*root != NULL)
 {
  // Insert to the left of this node
  if (newElement <  (*root)->element)
  {
   nparent = (*root);
   root = &((*root)->left);
  }
 
  // Insert to the right of this node
  else if (newElement > (*root)->element)
  {
   nparent = (*root);
   root = &((*root)->right);
  }
 }
 // This pointer is NULL, so create a new node and add it here
 *root = newNodeElt(newElement);// BINARY 트리
 printf("%d\\n",&(*root));
 printf("%d\\n",&((*root)->left));
 printf("%d\\n",&((*root)->left->left));
 printf("%d\\n",&((*root)->right));
 printf("%d\\n",&((*root)->right->right));

 if(nparent != NULL) (*root)->parent = nparent;//parent 노드의 성립
 if(nparent == NULL)
 {
  (*root)->color = BLACK;//루트 노드의 색 정의
  (*root)->parent = NULL;
 }
 else
 {
  
  if((*root)->parent->parent != NULL)
  {
   gparent = (*root)->parent->parent;
   if(gparent->left == (*root)->parent && gparent->right != NULL) uncle = gparent->right;
   if(gparent->right == (*root)->parent && gparent->left != NULL) uncle = gparent->left;
  }//uncle과 gparent의 선언
  if((*root)->parent->color == BLACK)
  {
   printf("**루트의 값 %d",**root);
   printf("&(*root): %d\\n",*(&(*root)));
   printf("(*root): %d\\n",(*root));
   printf("root: %d\\n",root);
   printf("*root: %d\\n",*root);
   printTreeInOrder(root);
   //이미 balance tree
  }
  else if((*root)->parent->color == RED)
  {
   if(uncle != NULL && uncle->color == RED)
   {
    (*root)->parent->color = BLACK;
    uncle->color = BLACK;
    gparent->color = RED;
    if(gparent->parent == NULL) gparent->color = BLACK;
   }//case3

  else if(gparent != NULL)//parent red, uncle은 없거나 BLACK case3~5 //parent == red
  {
    if(((*root) == (*root)->parent->right) && ((*root)->parent == gparent->left))
    {
     // left rotation 필요
    }
    else if(((*root) == (*root)->parent->left) && ((*root)->parent == gparent->right))
    {
     // right rotation 필요
    }
    (*root)->parent->color = BLACK;
    gparent->color = RED;
    if((*root) == (*root)->parent->left && (*root)->parent == gparent->left)
    {
     //right lotation 필요
    }
    else
    {
     //left rotation 필요
    }
  }

  }//parent == red end
 }
}

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
등록순정렬 기준선택
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 가족과 완벽하게 손절해야 할 것 같은 스타는? 운영자 24/06/24 - -
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2