비트리인데 좀 도와주세요................
얀별
책들 뒤지면서 함수부분은 다 찾아서 짰습니다. 근데 이게 포인터 함수라서 좀 어렵네요...
그래서 메인에서 함수 호출을 못하겠어요... 메인함수좀 짜주시면 감사하겠습니다.
그리고 소스도 맞는지 틀리는지도 아직 잘 모르겠습니다. 틀리면 좀 도와주세요
이 소스는 삼입 검색 삭제 를 하기위한 것입니다.
물론 비트리구요.......
부탁 드립니다.
#include stdlib.h
#include malloc.h
#include string.h
#define M 1
#define MM (M*2+1)
typedef struct _Bnode
{
int n;
struct _Bnode *ptr[MM+1];
int key[MM];
}Bnode;
Bnode *head;
void init_Btree(int *num)
{
head = (Bnode*)malloc(sizeof(Bnode));
head -ptr[0] =NULL;
head - n=0;
*num =0;
}
int in_Bnode(int key, Bnode *t)
{
int i;
for (i=0;i t-n; i++)
if (key == t-key[i]) return i;
return -1;
}
Bnode *Btree_search(int key, Bnode *base, int *num)
{
Bnode *t;
int i;
t=base -ptr[0];
while (t !=NULL && in_Bnode(key, t) ==-1)
{
for (i=0; it-n&&key =t-key[i]; i++);
t= t-ptr[i];
}
return t;
}
void insert_key(Bnode *t, int key, Bnode *left, Bnode *right)
{
int i;
i=t-n;
while (t-key[i-1] key && i 0)
{
t-key[i] = t-key[i-1];
t-ptr[i+1] = t- ptr[i];
i--;
}
t-n++;
t-key[i] =key;
t-ptr[i]=left;
t-ptr[i+1]=right;
}
Bnode *split(int key, Bnode *pivot, Bnode *base)
{
Bnode *left, *right;
Bnode *child;
int i;
if ((right = (Bnode*)malloc(sizeof(Bnode)))==NULL) return NULL;
if (pivot == base)
{
child = pivot-ptr[0];
if ((left=(Bnode*)malloc(sizeof(Bnode)))==NULL)return NULL;
for (i=0;iM;i++)
{
left -key[i] = child -key[i];
left-ptr[i]=child-ptr[i];
}
left-ptr[i]=child-ptr[i];
left-n=M;
for (i=M+1; iMM; i++)
{
right-key[i-M-1]=child-key[i];
right-ptr[i-M-1]=child-ptr[i];
}
right-ptr[i-M-1]=child-ptr[i];
right-n=M;
child-n=0;
insert_key(child, child-key[M], left, right);
}
else
{
for(i=0;ipivot-n && key =pivot-key[i]; i++);
left = pivot-ptr[i];
left-n=M;
for (i=M+1;iMM;i++)
{
right-key[i-M-1]=left-key[i];
right-ptr[i-M-1]=left-ptr[i];
}
right-ptr[i-M-1]=left-ptr[i];
right-n=M;
insert_key(pivot, left-key[M], left, right);
child=pivot;
}
return child;
}
Bnode *Btree_insert(int key, Bnode *base, int *num)
{
Bnode *t, *parent;
int i;
parent =base;
t=base -ptr[0];
if (t==NULL)
{
if ((t=(Bnode*)malloc(sizeof(Bnode)))==NULL)return NULL;
t-n=0;
t-ptr[0]=NULL;
base-ptr[0]=t;
}
while(t !=NULL)
{
if (in_Bnode(key, t) =0)
return NULL;
if (t-n==MM)
{
t=split(key, parent, base);
if (t==NULL) return NULL;
}
parent =t;
for (i=0; i t-n && key = t-key[i]; i++);
t= t-ptr[i];
}
insert_key (parent, key, NULL, NULL);
(*num)++;
return parent;
}
void delete_key(Bnode *t, int index)
{
int i;
for (i=index+1; it-n; i++)
{
t-key[i-1]=t-key[i];
t-ptr[i-1]=t-ptr[i];
}
t-ptr[i-1]=t-ptr[i];
t-n--;
}
int borrow_key(Bnode *p, int index)
{
int from, to;
Bnode *p1, *p2;
if (index == p-n)
{
from =index -1;
to =index;
}
else
{
from =index+1;
to =index;
}
p1=p-ptr[from];
p2=p-ptr[to];
if (p1-n =M)return 0;
if (from to)
{
insert_key(p2, p-key[to], p2-ptr[p2-n],p1-ptr[0]);
p-key[to]=p1-key[0];
delete_key(p1,0);
}
else
{
insert_key(p2, p-key[from], p1-ptr[p1-n],p2-ptr[0]);
p-key[from]=p1-key[p1-n -1];
delete_key(p1,p1-n -1);
}
return 1;
}
Bnode *bind_node(Bnode *p, int index, Bnode *base)
{
Bnode *left, *right;
int i;
if (index==p-n)index --;
left =p-ptr[index];
right=p-ptr[index+1];
left-key[left-n++]=p-key[index];
for (i=0;iright-n;i++)
{
left-key[left-n]=right-key[i];
left-ptr[left-n++]=right-ptr[i];
}
delete_key(p, index);
p-ptr[index]=left;
free(right);
if (p-n==0)
{
free(p);
base-ptr[0]=left;
}
return left;
}void make_swap(Bnode *p, Bnode *t, int *key, int index)
{
Bnode *center, *pcenter;
pcenter =t;
center =pcenter-ptr[index+1];
while(c;while(center-ptr[0] !=NULL)
{
pcenter =center;
center = center-ptr[0];
}
t-key[index]=center-key[0];
*key=center-key[0];
}
Bnode *Btree_delete(int key, Bnode *base, int *num)
{
Bnode *t, *parent;
int pi=0;
int ti;
parent =base;
t=base-ptr[0];
while (t !=NULL)
{
if (t-n =M&&parent !=base)
if (!borrow_key(parent, pi)) t=bind_node(parent, pi, base);
if ((ti=in_Bnode(key,t)) =0)
{
if (t-ptr[0] ==NULL)break;
else make_swap(parent, t, &key, ti);
}
parent=t;
for (pi=0; pit-n&&key = t-key[pi]; pi++);
t=t-ptr[pi];
}
if (t==NULL) return NULL;
if (t-n=M&&parent !=base)
if (!borrow_key(parent, pi)) t=bind_node(parent, pi, base);
delete_key(t, in_Bnode(key,t));
(*num)--;
return t;
}
-
민아
코드 올릴때 들여쓰기라도 좀 하시고... 메소드마다 간단하게 \여기서는 무슨무슨 일을 한다\ 정도는 붙여주셔야 읽을때 도움이 될텐데... 이렇게 올리시면 켄트벡이라도 두손 들걸요.. -_-;;;
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2696504 | 플래시 위에 div 올리기 (5) | 큰꽃늘 | 2025-05-30 |
2696458 | 제가 만든 소스 한번 봐주시고 수정 할 꺼 있으면 말해주세요. (실행은 되지만 깜빡거리네요) | 이플 | 2025-05-29 |
2696434 | 퍼센트 레이아웃 질문인데요.. | 나츠 | 2025-05-29 |
2696372 | %=open_main%, %=open_sub% 가 뭘까요? (9) | 행복녀 | 2025-05-29 |
2696347 | 콘솔 프로그램 질문 | 상큼한캔디 | 2025-05-28 |
2696320 | c언어 scanf 함수를 이요해 문자열 입력 받을 시 질문 있습니다. | 슬아라 | 2025-05-28 |
2696292 | 익스플로러9이상에서만 이상한 보더가 보이는데 삭제할수 있나요? | 망고 | 2025-05-28 |
2696263 | 프로그래밍 공부시작 질문 (6) | 진이 | 2025-05-28 |
2696206 | SK2의 플래시를 밴치마킹하려고하는데요.. (1) | 비내리던날 | 2025-05-27 |
2696179 | ie7에서 사라지지가 않네요. (2) | 빛길 | 2025-05-27 |
2696150 | div에 스크롤 생기게 하려면... (2) | 에드가 | 2025-05-27 |
2696123 | 자료구조론 공부중인데 | 김자영 | 2025-05-26 |
2696094 | exe 파일 | 제철 | 2025-05-26 |
2696043 | 제이쿼리 .scroll() 관련 질문드립니다 | 이거이름임 | 2025-05-26 |
2695984 | 마크업상으로 하단에 있으나 우선적으로 이미지파일을 다운로드받는 방법 (1) | 들꿈 | 2025-05-25 |
2695934 | tr 속성값 (9) | 새 | 2025-05-25 |
2695905 | ASP로 개발됐을 때 css가 달라져요 ㅠㅠ (4) | 슬아라 | 2025-05-24 |
2695878 | form을 이용한 다른 페이지로 넘기는 방법을 알려주세요 (1) | 핫파랑 | 2025-05-24 |
2695844 | 저기 암호화 및 복호화 프로그램.. 만들어볼려는대 (2) | 한빛 | 2025-05-24 |
2695814 | [질문] PDA에서 애플릿이 가능한가요? (1) | 봄시내 | 2025-05-24 |