이진탐색트리 디스크 기반??
앤드류
질문 제목 : 질문 내용 :이진탐색트리를 디스크 기반으로 짜오라는데 먼말인지 잘 모르겟네여 ㅜㅜ
#include stdio.h
#include string.h
#define yes 1
#define no 0
#define list struct list list {
char *name;
char *tel;
list *left;
list *right;
}; void main() {
list *root, *insert(), *srch(), *modi(), *delet(), *temp;
char name[15], tel[15];
char cname[15];
int key;
void prnt(), inp();
file *fp; root=null; do {
printf(\ninput(file)-1, input(keyboard)-2, display-3, search-4, update-5, delete-6, end-7);
printf(select a key ---);
scanf(%d, &key);
if(key==1) {
printf(\n---input---\n);
fp=fopen(binary.dat, r);
if(fp != null)
while(fscanf(fp, %s %s, name, tel) != eof) {
printf(name=%s tel=%s\n, name, tel);
root=insert(root, name, tel);
&n}
else
printf(fopen error\n);
}
if(key==2) { //키보드에서 삽입할 데이터값을 입력받음
inp(name, tel);
root=insert(root, name, tel);
}
if(key==3) {
printf(\n---display---\n);
printf(\nlevel name tel \n);
prnt(root, 1);
}
if(key==4) {
printf(\n---search---\n);
printf( name--- );
scanf(%s, cname);
temp=srch(root, cname);
}
if(key==5) {
printf(\n---update---\n);
printf( name--- );
scanf(%s, cname);
modi(root, cname);
}
if(key==6) {
printf(\n---delete---\n);
printf( name--- );
scanf(%s, cname);
root=delet(root, cname);
}
if(key==7) {
printf(\n---end---\n);
}
} while(key7);
} //재귀적으로 트리를 순회하면서 입력위치를 결정
list *insert(list *rp, char *name, char *tel) {
list *create_node();
char *strsv();
int cmp; if(rp==null) { //트리가 비어있는 상태
rp=create_node(); //하나의 노드를 할당받아
rp-name=strsv(name); //데이터 필드와 연결필드를 설정
rp-tel=strsv(tel);
rp-left=rp-right=null;
}
else if((cmp=strcmp(name, rp-name))==0) { //이미 데이터가 존재할 때
printf(\n------------------------\n);
printf(name=%-15s tel=%-13s \n, rp-name, rp-tel);
printf(\n------------------------\n);
}
else if(cmp 0) //왼쪽 부트리로 이동
rp-left=insert(rp-left, name, tel);
else
rp-right=insert(rp-right, name, tel);
return(rp);
} void inp(char *name, char *tel) {
printf(name--- );
scanf(%s, name);
printf(tel--- );
scanf(%s, tel);
} list *create_node() {
char *malloc();
return((list *)malloc(sizeof(list)));
} char *strsv(char *sl) { //입력받은 데이터크기+1만큼의 공간을 확보하여 데이터를 복사
char *p, *malloc();
if((p=malloc(strlen(sl)+1)) != null) //eol을 위한 공간 확보
strcpy(p, sl);
return(p);
} void prnt(list *pp, int lev) {
if(pp != null) { //중위 순회 알고리즘을 활용한 출력
prnt(pp-left, lev+1);
printf(%2d %-14s %15s \n, lev, pp-name, pp-tel);
prnt(pp-right, lev+1);
}
} list *srch(list *sp, char *snam) {
int cmp; if(sp != null) {
if((cmp=strcmp(snam, sp-name))==0) {
printf(\n name tel \n);
printf(-----------------------\n);
printf(%-14s %-15s \n, sp-name, sp-tel);
}
else if(cmp 0)
sp-left=srch(sp-left, snam);
else
sp-right=srch(sp-right, snam);
}
else
printf(search fail: %s is not exist in the tree \n, snam);
return(sp);
} list *modi(list *mp, char *mnam) {
char mtel[15];
int cmp; if(mp != null) {
if((cmp=strcmp(mnam, mp-name))==0) {
printf(tel-- );
scanf(%s, mtel);
strcpy(mp-name, mnam);
strcpy(mp-tel, mtel);
}
else if(cmp 0)
mp-left=modi(mp-left, mnam);
else
mp-right=modi(mp-right, mnam);
}
else
printf(search fail: %s is not exist in the tree \n, mnam);
return(mp);
} list *delet(list *dp, char *dnam) {
list *findmin(), *temp;
void free();
int cmp; if(dp==null)
printf(search fail no search name exist \n);
else if((cmp=strcmp(dnam, dp-name)) 0)
dp-left=delet(dp-left, dnam);
else if(cmp 0)
dp-right=delet(dp-right, dnam);
else if(dp-left && dp-right) { //두개의 자식이 있는 노드
temp=findmin(dp-right); //삭제될 노드를 검색
strcpy(dp-name, temp-&g temp-name); //데이터의 복사
strcpy(dp-tel, temp-tel);
dp-right=delet(dp-right, dp-name); //최소노드를 결정한다.
}
else { //하나의 자식이 있는 경우
temp=dp;
if(dp-left==null)
dp=dp-right;
else if(dp-right==null)
dp=dp-left;
free(temp);
}
return(dp);
} list *findmin(list *node) {
list *temp, *save; save=node;
temp=save-left; //오른쪽 부트리의 최소걋?최소값을 찾는다. while(temp != null) {
save=temp;
temp=temp-left;
}
return(save);
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695738 | fopen과fclose질문~~ (5) | 희선 | 2025-05-23 |
2695707 | 3의 배수 나타내기. (2) | 수리 | 2025-05-23 |
2695626 | 피보나치수열 과제 때문에 질문 드립니다. (6) | 옆집언니 | 2025-05-22 |
2695595 | 포인트공부중입니다 int형에서 4=1 인가요? (3) | 족장 | 2025-05-22 |
2695567 | 드라이브 고유번호를 가져오는 함수 (2) | 초코맛사탕 | 2025-05-21 |
2695533 | 음수의 산술변환! 질문이요 ㅠㅠ... (4) | 꽃여름 | 2025-05-21 |
2695506 | 구조체 배열 이용 도서목록 출력 프로그램 (1) | 가을귀 | 2025-05-21 |
2695450 | c언어 함수 질문이요.... | 이슬비 | 2025-05-20 |
2695403 | VirtualAlloc함수 및 메모리 질문 | 크리에이터 | 2025-05-20 |
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |
2695010 | C++의 STL은 왜 굳이 템플릿화 시켜서 라이브러리를 만드나요? (초보수준의 질문..) (2) | 엘보어 | 2025-05-16 |