이진트리 삽입및 길이측정
가온길
질문 제목 : 이진트리 삽입및 길이 측정질문 내용 :
각 노드의 갯수를 100, 100 ,200 ....1000으로 하여
완전이진트리(rand()를 이용해 키값배정)를 구성해서
그 이진트리의 길이를 측정한다.
제가 구하고 싶은것은 100,200짜리 노드를 배정한 이진트리의 길이를 측정하고 싶습니다.
코드를 수정해서 올려주시면 감사합니다.#include stdio.h
#include stdlib.h
#include math.h
#include time.h
typedef int itype;
typedef struct data{
int key;
int item;
}kdata;
typedef struct tree *treepointer;
typedef struct tree{
treepointer leftchild;
kdata data;
treepointer rightchild;
} node;
node tree;
treepointer modifiedsearch(treepointer tree, int key)
{
while(tree)
{
if(key == tree-data.key)
return null;
else if(key tree-data.key)
{
if(tree-leftchild == null)
return tree;
tree = tree-leftchild;
}
else
{
if(tree-rightchild == null)
return tree;
tree = tree-rightchild;
}
}
return 0;
}
void insert(treepointer *node, int k, itype theitem){
//data=(k,theitem);
treepointer ptr, temp=modifiedsearch(*node,k);
if(temp||!(*node)){
ptr=(treepointer)malloc(sizeof(*ptr));
ptr-data.key=k;
ptr-data.item=theitem;
ptr-leftchild=ptr-rightchild=null;
if(*node)
if(k temp-data.key) temp-leftchild=ptr;
else temp-rightchild=ptr;
else *node=ptr;
}
}
int height(treepointer tree)
{
int right_height=0;
int left_height=0;
if(tree==null)
return 0;
else{
left_height=height(tree-leftchild);
right_height=height(tree-rightchild);
}
return left_height right_height? left_height+1: right_height+1;
}
int main(int argc, char *argv[])
{
int i=0;
for(i=0; i100; i++){
int rand_k=rand()%100;
itype i100;
insert(, rand_k, i100);
system(pause);
return 0;
}
-
보슬
책보고 만든 함수인데요
어떻게 써먹죠? 함수의 인자 사용법을 잘몰라서요. -
그루터기
정확한 용어인지는 모르겟지만, 각 노드의 , 이진트리에서의 루트 노트와의 거리? 를 의미하는것이라면
100, 200, 300이라는 값을 순서대로 넣었을 경우,
log_2_값 을 통해서 알아낼수 있구요
아니면 매번 탐색을 통해 구할 수도 잇어요 -
채꽃
정확히 , 이진트리의 길이라는것이 무엇인가요?
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692451 | 이 문제좀 풀어주세요 ^^ | 게자리 | 2025-04-23 |
2692424 | 2차원배열 자료입력질문이요! (1) | 똘끼 | 2025-04-22 |
2692401 | 유닉스안에서 C언어를 이용한 명함 만들기 입니다; 이해안가는 부분이있네요 | 2gether | 2025-04-22 |
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
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 |