Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[1625] 흐미.. 정말 모르겠어요.. 좀 도와 주세요..ㅠㅠ
maxgirl [] 1395 읽음    2002-11-24 22:11
아래 소스를 AVL tree 생성 및 검색 프로그램 작성 하는 건데요.. 정말 모르겠어요.. 도와주세요..ㅠㅠ
 
#include "stdafx.h"
#define MAX_SLOTS 100
class Index {
  public:
 Index() {
  i_key = i_offset = i_left = i_right = 0;
  i_left_cnt = i_right_cnt = 0;
 }
 int i_key;
 int i_offset;
 int i_parent;
 int i_left;
 int i_right;
 int i_left_cnt;
 int i_right_cnt;
};
int N = MAX_SLOTS;
int Idx = 0;
int Key = 0;
Index ia[MAX_SLOTS];
int _findEmpty()
{
 for (int i = 1; i < N; i++) {
  if (ia[i].i_key == 0) {
   Idx = i;
   return i;
  }
 }
 printf("No empty slot remained\n");
 return 0;
}
int _insert(int k, int p)
{
 int child;
 if (ia[k].i_key == 0) {
  ia[k].i_key = Key;
  ia[k].i_parent = p;
  printf("Insert %d, on %d th\n", Key, k);
  return k;
 }
 if (ia[k].i_key == Key) {
  printf("Duplicated key = %d on %d th \n", Key, k);
  return k;
 }
 else if (ia[k].i_key > Key) {
  if (ia[k].i_left == 0)
   ia[k].i_left = _findEmpty();
  child = _insert(ia[k].i_left, k);
  ia[k].i_left_cnt++;
 }
 else {
  if (ia[k].i_right == 0)
   ia[k].i_right = _findEmpty();
  child = _insert(ia[k].i_right, k);
  ia[k].i_right_cnt++;
 }
 return child;
}

int _search(int k)
{
 if (k == 0) {
  printf("Not found %d\n", Key);
  return 0;
 }
 printf("searching %d\n", k);
 if (ia[k].i_key == Key) {
  printf("found %d on %d \n", Key, k);
  return k;
 }
 else if (ia[k].i_key > Key)
  _search(ia[k].i_left);
 else
  _search(ia[k].i_right);
 return 0;
}
void Dump()
{
 printf("Id key offset parent left right leftcnt rightcnt\n");
 for (int i = 1; i <= Idx; i++) {
  printf("%d %d %d %d %d %d %d %d\n",
  i,
  ia[i].i_key,
  ia[i].i_offset,
  ia[i].i_parent,
  ia[i].i_left,
  ia[i].i_right,
  ia[i].i_left_cnt,
  ia[i].i_right_cnt);
 }
}
   
void Insert(int key)
{
 Key = key;
 int id = _insert(1, 1);
};
void Search(int key)
{
 Key = key;
 int id = _search(1);
}
int Data[MAX_SLOTS] = {5, 3, 8, 2, 4, 1, 6, 7, 10, 11, 12 };  
int main(int argc, char* argv[])
{
 for (int i = 0; i < 11; i++)
  Insert(Data[i]);
 Search(7);
 Dump();
 return 0;
}
 

 


+ -

관련 글 리스트
1625 흐미.. 정말 모르겠어요.. 좀 도와 주세요..ㅠㅠ maxgirl 1395 2002/11/24
1626     C로 된 AVL Tree 소스! 김백일 3151 2002/11/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.