아래 소스를 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;
}