이원탐색트리 삽입,삭제,검색 설명좀
슬찬
질문 제목 : 파일처리 소스가 이해가안됩니다. 질문 요약 :중간중간에 주석은 되어있는데 나머지 주석처리좀부탁드려요 ㅜㅜ질문 내용 :
#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);
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2697549 | 배열 초기화 (4) | 나리 | 2025-06-08 |
2697465 | 수다님...^^ (2) | 가론 | 2025-06-08 |
2697432 | 서버 만드는 함수에서 궁금한게있어요~ | 파랑 | 2025-06-07 |
2697401 | 열혈강의 문제오류 (1) | 꿈 | 2025-06-07 |
2697374 | 기초적인 C언어 프로그래밍 입니다. | 얼 | 2025-06-07 |
2697341 | 좌우대칭 문제인데 Q가 입력되면 종료가 되야하는데 되지않습니다 | 무지개 | 2025-06-07 |
2697314 | fprintf와 fscanf 로 파일 입출력 할떄 | 밝음이 | 2025-06-06 |
2697293 | 툴 소스 뽑아내는법 | 도움 | 2025-06-06 |
2697264 | 소소한거 두어가지 질문할께요~ (8) | 별솔 | 2025-06-06 |
2697235 | scanf로 인풋 받을 때?! | 보담 | 2025-06-06 |
2697207 | 열혈강의 연습문제 질문이요~ (2) | 맥적다 | 2025-06-05 |
2697182 | strcmp, strtok - 어떤 기능을 하는지... | 루다 | 2025-06-05 |
2697129 | [질문] 아래 저축액을 계산하는 프로그램 ㅠ 추가 질문 (7) | 찬늘 | 2025-06-05 |
2697072 | 쌩초보 질문! (1) | 얀 | 2025-06-04 |
2697050 | 도움부탁드립니다.. | 화이트 | 2025-06-04 |
2697023 | gotoxy함수, 어떻게 사용하죠? (3) | 적송 | 2025-06-04 |
2696994 | c언어를 막 시작한 초보인데 질문이 있어용 ㅠㅠ (2) | 귀1여운렩 | 2025-06-03 |
2696944 | 윈도우 콘솔프로그램 질문드립니다 (2) | 꽃님이 | 2025-06-03 |
2696882 | c언어 입력받기 질문 입니다 (2) | 흰추위 | 2025-06-02 |
2696853 | 문자열은 정적메모리에 할당된다고 하는데 정적메모리가 뭡니까?? (6) | 다온 | 2025-06-02 |