높이측정프로그램만 구현하면되는데
물보라
질문 제목 :
처음에 공백 이원 탐색 트리에서 시작하여 n번의 무작위 삽입을 수행하는 프로그램을 작성하여라. 삽입될 값으 생성하기 위해 난수 생성기를 이용하라 결과 이원탐색 트리의 높이를 조사하고 높이를 log 2 n으로 나눠라.질문 내용 :
어디가 틀렸나요?#includestdlib.h
#includeconio.h
#includestring.h
#includestdio.h
#includetime.h
#includemath.htypedef struct bintree *treepointer;struct bintree {
treepointer leftchild;
int key;
treepointer rightchild;
};treepointer bin_node = null;treepointer insert(treepointer bin_node, int key);treepointer search(treepointer bin_node, int key, treepointer *parent);
void printbin(treepointer bin_node);treepointer insert(treepointer bin_node, int x)
{
treepointer ptr, parent;
treepointer new;
ptr = search(bin_node, x, &parent);
if ( ptr != null ) {
printf(\n중복된 키 값이 있음.\n\n);
return bin_node;
}
new = (treepointer)malloc(sizeof(struct bintree));
new-key = x;
new-rightchild = null;
new-leftchild = null;
if ( bin_node == null ) bin_node = new;
else if ( x parent-key ) parent-leftchild = new;
else parent-rightchild = new;
return bin_node;
}
treepointer search(treepointer bin_node, int key, treepointer *parent)
{
treepointer ptr;
ptr = bin_node;
*parent = null;
if ( ptr == null ) return ptr;
while ( ptr != null ) {
if ( key == ptr-key ) return ptr;
*parent = ptr;// 찾을 키값을 가진 노드의 부모노드
if ( key ptr-key ) ptr = ptr-leftchild;
else ptr = ptr-rightchild;
}
return ptr;
}void printbin(treepointer ptr) {
if (ptr != null)
{
printf(();
printbin(ptr-leftchild);
printf (%d, ptr-key);
printbin(ptr-rightchild);
printf());
}
}int height(treepointer bin_node)
{
int right_height=0;
int left_height=0;
if(bin_node == null)
return 0;
else{
left_height=height(bin_node-leftchild);
right_height=height(bin_node-rightchild);
}
return left_height right_height ? left_height+1 : right_height+1;
}int main()
{
int n,i,height=0;
int menu = 0;
int key;
treepointer ptr;
treepointer parent;
srand((unsigned)time(null));
while (menu != 2) {
printf(\n이진 탐색 트리 연산\n\n);
printf(1. 삽입\n);
printf(2. 종료\n);
printf(목록선택 : );
scanf(%d, &menu);
switch(menu) {
case 1 :
printf(\n삽입 개수 입력 : );
scanbsp;scanf(%d, &n);
for(i=0;in;i++){
key = rand()%n;
bin_node = insert(bin_node, key);
printf(\n);
printbin(bin_node);
printf(\n);
}
printf(\n%d, height(bin_node));//에러가 뜨는부분
printf(\n높이 비율 height/log(n)/log2 : %d,((height)/log(n)/log(2)));
break;
case 2 :
printf(\n프로그램 종료됩니다.\n);
free(bin_node);
break;
default :
printf(\n잘못 선택하셨습니다.\n);
}
}
getch();
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2704529 | 메모리 동적 할당 관련해서 질문 있습니다~ | 궁수자리 | 2025-08-11 |
2704497 | int double char 같은것좀 좀만 더 가르쳐 주세요 (6) | 로지 | 2025-08-10 |
2704473 | 구조체 배열 초기화 질문 | 꽃은별 | 2025-08-10 |
2704445 | 배열과 조건문에 관한질문 (3) | 찬솔큰 | 2025-08-10 |
2704417 | 이진 트리 깊이 값 구하는것 질문입니다. | 푸른잎 | 2025-08-10 |
2704391 | 자료 유형에 관련된 문제입니다. (2) | 늘솜 | 2025-08-09 |
2704365 | c언어 문제안되서물어봅니다 (3) | 맑은가람 | 2025-08-09 |
2704338 | 다항식의 뺄셈에 대해 질문드립니다... (1) | 여자 | 2025-08-09 |
2704311 | 센서를 이용한 라인 주행인데 명령어좀 알려주세요 ㅠ_ㅠ (3) | 초록이 | 2025-08-09 |
2704284 | 배열for문;; (3) | 은솜 | 2025-08-08 |
2704255 | readline(int fd, char *buf, int nbytes)함수를 구현하는법좀..ㅠㅠ | 파라나 | 2025-08-08 |
2704196 | 간단한 c언어좀 만들어주세요 (2) | 슬S2아 | 2025-08-08 |
2704118 | 성적에서 등수 구하기 (4) | 딥레드 | 2025-08-07 |
2704062 | 알고리즘 알려주세요 | 나리 | 2025-08-06 |
2704035 | 답은 나오는데 과정에서 `` 약간 이상합니다.ㅎㅎ 답만나와버려요 | 핫블랙 | 2025-08-06 |
2703979 | 문자열 EEPROM 작성 방법 문의드립니다. | 그녀는귀여웠다 | 2025-08-06 |
2703954 | 키보드를 입력하면 캐릭터를 움직이기 질문 (7) | 좋아해 | 2025-08-05 |
2703924 | 계속 에러가... (4) | 눈꽃 | 2025-08-05 |
2703897 | 배열의 최댓값과 최솟값 차이 구하기 (1) | 하늬 | 2025-08-05 |
2703869 | C언어 질문입니다 급해용!!! ㅠㅠ (2) | 덕이 | 2025-08-05 |