이원탐색트리 삽입,삭제,검색 설명좀
슬찬
질문 제목 : 파일처리 소스가 이해가안됩니다. 질문 요약 :중간중간에 주석은 되어있는데 나머지 주석처리좀부탁드려요 ㅜㅜ질문 내용 :
#include stdio.h
#include stdlib.h
typedef struct node
{
struct node* left;
struct node* right;
int key;
} node;
/p
node* createnode(int data);
node* searchnode(node* tree, int finddata);
node* deletnode(node* tree, int data);
void insertnode(node* tree, node* newnode);
void printtree(node* tree);
int main()
{
node* tree = null;
node* newnode;
node* findnode;
int input, change;
int ch;
int i = 0;
while(1)
{
printf(binary search tree program(bst) \n);
printf(--------------------------------\n);
printf(1. insert \n);
printf(2. delete \n);
printf(3. search \n);
printf(4. 노드검색\n);
printf(5. 종료\n);
printf(--------------------------------\n);
printf(선택: );
scanf(%d, &ch);
switch(ch)
{
casecase 1 :
system(cls);
if(i==0) //첫번째입력인지아닌지확인한다
{
printf(임의의정수입력);
scanf(%d, &input);
tree = createnode(input);
i++;
}
else
{
printf(지금트리에있는값);
printtree(tree);
printf(\n););
printf(새로운node()생성되었습니다. );
scanf(%d, &input);
newnode = createnode(input);
insertnode(tree, newnode);
}
system(pause);
system(cls);
break;
case 2 :
system(cls);
if(tree == null)
{
printf(트리가생성되지않았습니다. 트리를생성해주세요\n);
system(pause);
system(cls);
break;
}
printf(현재트리에있는값);
printtree(tree);
printf(\n);
printf(이진트리에삭제할값을입력하세요);
scanf(%d, &input);
deletnode(tree, input);
system(cls);
break;
case 3 :
system(cls);
if(tree == null)
{
printf(트리가생성되지않았습니다. 트리를생성해주세요\n);
system(pause);
system(cls);
break;
}
printf(현재트리에있는값);
printtree(tree);
printf(\n);
printf(이진트리에서수정시킬값을입력하세요);
scanf(%d, &input);
printf(이진트리에서수정할값을입력하세요);
scanf(%d, &change);
deletnode(tree, input);
if(tree == null)
{
tree = createnode(change);
}
else
{
newnode = createnode(change);
insertnode(tree, newnode);
}
printf(현재트리에있는값);
printtree(tree);
printf(\n);
system(pause);
system(cls);
break;
case 4 :
system(cls);
if(tree == null)
{
printf(트리가생성되지않았습니다. 트리를생성해주세요\n);
system(pause);
system(cls);
break;
}
printf(이진탐색트리의노드를표시합니다);
printtree(tree);
printf(\n);
system(pause);
break;
case 5 :
exit(1);
}
}
}
node* createnode(int data) //노드생성
{
node* newnode = (node*)malloc(sizeof(node));
newnode-left = null;
newnode-right = null;
newnode-key = data;
return newnode;
}
node* searchnode(node* tree, int finddata) //노드찾기
{
if(tree == null) return null;
if(tree-key == finddata)
{
return tree;
}
else if(tree-left, finddata)
{
searchnode(tree-right, finddata);
}
else
{
searchnode(tree-left, finddata);
}
}
node* deletnode(node* tree, int data) //노드삭제
{
node* treelist = tree;
node* minnode = null;
node* previous = null;
int wich = 0;
while(1)
{
if(treelist-key == data){
break;
}
else if(treelist-key data)
{
previous = treelist;
treelist = treelist-left;
wich = 0;
}
else if(treelist-key data)
{
previous = treelist;
treelist = treelist-right;
wich = 1;
}
}
//지울노드의자식이둘다있을경우
if((treelist-left != null) && (treelist-right != null))
{
while(1){
node* _treelist = treelist;
node* _previous = null;
while(_treelist-left != null)
{
_previous = _treelist;
_treelist = _treelist-left;
}
minnode = _treelist; //최소값
if((minnode-left != null) || (minnode-right != null))
{
_previous-left = minnode-left;
_previous-right = minnode-right;
}
else
{
_previous-left = null;
}
minnode-left = treelist-left;
minnode-right = treelist-right;
if(wich == 0)
previous-left = minnode;
else if(wich == 1)
previous-right = minnode;
return treelist;
}
}
else if((treelist-left == null) && (treelist-right == null))
{
if(wich == 0)
previous-left = null;
else if(wich == 1)
previous-right = null;
return treelist;
}
//노드의자식이하나만있을경우
else if((treelist-left != null) || (treelist-right != null))
{
if(wich == 0)
{
if(treelist-left != null)
previous-left = treelist-left;
else if(treelist-right != null)
previous-right = treelist-right;
}
else if(wich == 1)
{
if(treelist-left != null)
previous-left = treelist-left;
else if(treelist-right != null);
previous-right = treelist-right;
}
return treelist;
}
return null;
}
void insertnode(node* tree, node* newnode) //노드삽입
{
if(newnode-key tree-key)
{
if(tree-right != null) insertnode(tree-right, newnode);
else tree-right = newnode;
}
else if(newnode-key tree-key)
{
if(tree-left != null) insertnode(tree-left, newnode);
else tree-left = newnode;
}
}
void printtree(node* tree) //이진탐색트리출력(중위)
{
if(tree == null) return;
printtree(tree-left);
printf(%4d, tree-key);
printtree(tree-right);
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |