수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

자료구조 BST(이진탐색트리)에서 모든 Branch(가지들)의 깊이를 어떻게 구하나요?

잔디

2023.04.01

질문 제목 : bst(이진탐색트리)에 존재하는 모든 branches(가지들)의 depth(깊이)를 구하는 방법이진탐색트리에서 모든 branch들의 depth를 찾아array로 저장한 다음, 최대값, 최소값,평균값, 표준편차 찾기질문 내용 :

과제중입니다. 자료구조요..

이제 bst에 대해선 거의 숙지를 하고, 본격적으로 교수님이 요구하는 부분을 건드리려 하는데

도무지 감이 안잡히네요.교수님께서 요구하신 사항은, bst의 자료구조속에 자료를 넣고, 그 bst의 최대 깊이, branch중의 최소 깊이, 모든 branch들의 평균 깊이, 그리고 표준편차를 구하라는 겁니다. 물론 프로그래밍으로요,

최대 depth구하는건 구현했습니다.

그런데 아무 쓸모가 없더군요.. 일단 다 알아야 표준편차를 구하든가 하니깐요.ㅠ 자료의 갯수는 64개, 128개 이렇게 늘릴겁니다. 지금 아래에서는 실험적으로 10이로 지정해 놓았습니다.

소스 코드는 다음과 같습니다.

밑에 xml:namespace prefix...뭐 이런건 왜 뜨는지 모르겠네요 ㅠ 지우고 돌려보세요#include stdio.h
#include stdlib.h?xml:namespace prefix = o /?xml:namespace prefix = o /
#include math.h
#include time.h
#include stdbool.h
#include ctype.h
#include windows.htypedef struct node
{
int* dataptr;
struct node* left;
struct node* right;
}node;

typedef struct
{
int count;
int (*compare)(void* argu1, void* argu2);
node* root;
}bst_tree;

typedef struct
{
char h;
int numvalue;

} value;
bst_tree* bst_destroy(bst_tree* tree);
void bst_insert(bst_tree* tree, int* dataptr);
void bst_delete(bst_tree* tree, int* dltkey);
void* bst_retrieve(bst_tree* tree, int* keyptr);
void bst_traverse(bst_tree* tree, void (*process)(int* dataptr));
bool bst_empty(bst_tree* tree);
bool bst_full(bst_tree* tree);
int bst_count(bst_tree* tree);

static node* _insert(bst_tree* tree, node* root, node* newptr);
static node* _delete(bst_tree* tree, node* root, int* dataptr, bool* success);
static void* _retrieve(bst_tree* tree, int* dataptr, node* root);
static void _traverse(node* root, void (*process)(int* dataptr));
static void _destroy(node* root);bst_tree* bst_create(int (*compare)(void* argu1, void* argu2));
int createrand(void);
int comparevalue(void* num1, void* num2);
void addvalue(bst_tree* list, int val);
void printlist (bst_tree* list);
void processvalue(void* dataptr);
int max_depth(node* data);

int main(void)
{
int run, secondrun, valtocompare, standard;//variables for creating and comparing random values
int h, l, m, d;//h=highst depth of the tree, l=lowest depth of the tree, m=mean of the depth of branches
// d: standard deviation of depth of the tree
int lastnum=10; //n
int numarray[10];
bool isnumberalreadyin=false;

bst_tree* numtree;
srand((unsigned int)gettickcount());
numtree = bst_create(comparevalue);

for (run=0; runlastnum; run++)
{
valtocompare=rand()%1000;
numarray[run]=valtocompare;
while(!isnumberalreadyin)
{
standard=0;
for(secondrun=0; secondrun=run; secondrun++)
{
if (numarray[secondrun]==valtocompare)
{
valtocompare=rand()%1000;
standard++;
}
}

if (standard==0)
isnumberalreadyin=true;
}

addvalue(numtree, valtocompare);
}

printlist(numtree);
h=max_depth(numtree-root); //depth의 최댓값 구하기!

numtree = bst_destroy(numtree);

getch();
return 0;
}int comparevalue(void* num1, void* num2)
{
value firstnumber;
value secondnumber;

firstnumber = *(value*)num1;
secondnumber= *(value*)num2;

if(firstnumber.numvalue secondnumber.numvalue)
return -1;

if(firstnumber.numvalue secondnumber.numvalue)
return 1;
}

void addvalue(bst_tree *list, int randomvalue)
{

value* valptr;

valptr=(value*)malloc(sizeof(value));

if(!valptr)
printf(memory overflow in add\n);

valptr-numvalue=randomvalue;
bst_insert(list, valptr);

}
void printlist (bst_tree* list)
{
printf(\ntree list:\n);
bst_traverse (list, processvalue);
printf(end of the list\n);
return;
}

void processvalue(void* dataptr)
{
value avalue;
avalue = *(value*)dataptr;
printf(%d\n, avalue.numvalue);
return;
}

int max_depth(node* data)
{
int max = 0, depth;

if( data == null)
return 0;
else
{
depth = max_depth(data-left) + 1;
if( depth max)
max = depth;
depth = max_depth(data-right) + 1;
if( depth max)
max = depth;
return max;
}}//이건 depth의 최대값 구하는 함수

bst_tree* bst_create(int (*compare)(void* argu1, void* argu2))
{
bst_tree* tree;

tree=(bst_tree*)malloc(sizeof(bst_tree));
if(tree)
{
tree-root = null;
&nbsbsp; tree-count = 0;
tree-compare = compare;
}

return tree;
} //bst_create

bst_tree* bst_destroy(bst_tree* tree)
{
if(tree)
_destroy(tree-root);

free(tree);
return null;
} //bst_destroy

void bst_insert(bst_tree* tree, void* dataptr)
{
node* newptr;

newptr = (node*)malloc(sizeof(node));
if(!newptr)
return false;

newptr-right = null;
newptr-left = null;
newptr-dataptr = dataptr;o:aptr;
if(tree-count == 0)
tree-root = newptr;
else
_insert(tree, tree-root, newptr);
(tree-count)++;

}//bst_insert

void bst_delete (bst_tree* tree, void* dltkey)
{
bool success;
node* newroot;

newroot=_delete(tree, tree-root, dltkey, &success);
if(success)
{
tree-root=newroot;
(tree-count)--;
if (tree-count ==0)
//tree is now empty
tree-root=null;
}

}

void* bst_retrieve (bst_tree* tree, void * keyptr)
{
if(tree-root)
return _retrieve(tree, keyptr, tree-root);
else
return null;
}//bst_retrieve

void bst_traverse (bst_tree* tree, void (*process)(void* dataptr))
{
_traverse(tree-root, process);
return;
}// bst_traversebool bst_empty (bst_tree* tree)
{
return (tree-count == 0);
} //bst_empty

bool bst_full (bst_tree* tree)
{
node* newptr;

newptr = (wptr = (node*)malloc(sizeof(*(tree-root)));
if(newptr)
{
free (newptr);
return false;
}
else
return true;
}//bst_full

int bst_count (bst_tree* tree)
{
return (tree-count);
}//bst_countstatic node* _insert(bst_tree* tree, node* root, node* newptr)
{
if(!root)
return newptr;

if(tree-compare(newptr-dataptr, root-dataptr)0)
{
root-left=_insert(tree, root-left, newptr);
return root;
}

else
{
root-right = _insert(tree, root-right, newptr);
return root;
}
return root;
}//_insert

static node* _delete(bst_tree* tree, node* root, void* dataptr, bool* success)
{
node* dltptr;
node* exchptr;
node* newroot;
void* holdptr;

if(!root)
{
*success = false;
return null;
}

if(tree-compare(dataptr, root-dataptr)0)
root-left = _delete(tree, root-left, dataptr, success);
else if (tree-compare(dataptr, root-dataptr) 0)
root-right = _delete(tree, root-right, dataptr, success);
else
{
dltptr=root;
if(!root-left)
{
free (root-dataptr);
newroot = root-right;
free(dltptr);
*success = true;
return newroot;
}
else
if(!root-right)
{
newroot = root-left;
free(dltptr);
*success = true;
return newroot;
}
else
{
exchptr = root-left;
while(exchptr-right)
exchptr = exchptr-right;

holdptr = root-dataptr;
root-dataptr = exchptr-dataptr;
exchptr-dataptr= holdptr;
&; root-left = _delete(tree, root-left, exchptr-dataptr, success);
}
}
return root;
}//_delete

static void* _retrieve(bst_tree* tree, void* dataptr, node* root)
{
if(root)
{
if (tree-compare(dataptr, root-dataptr)0)
return _retrieve(tree, dataptr, root-left);
else if (tree-compare(dataptr, root-dataptr)0)
return _retrieve(tree, dataptr, root-right);
else
return root-dataptr;
}

else
return null;
}//_retrieve

static void _traverse(node* root, void (*process)(void* dataptr))
{
if (root)
{
_traverse (root-left, process);
process(root-dataptr);
_traverse (root-right, process);
}
return;
}//_traverse

static void _destroy(node* root)
{
if (root)
{
_destroy(root-left);
free (root-dataptr);
_destroy(root-right);
free(root);
}
return;
}

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 유리

    그리고, 이왕이면 직접적으로 가르쳐 주셨으면 좋겠습니다.ㅠ 시간이 얼마 없거든요...(이번 토요일 오전 마감)

번호 제 목 글쓴이 날짜
2698782 기초적인 함수 질문이요ㅠㅠㅠㅠ 내담 2025-06-20
2698749 프로그램 짜던 도중 패닉입니다...ㅜ 파랑 2025-06-19
2698719 조건부컴파일 질문입니다.~ (2) 큐트 2025-06-19
2698693 재귀 함수 에러 바닐라 2025-06-19
2698673 고민이있는데 들어좀주세요!! (1) 초코맛캔디 2025-06-19
2698644 1부터 n까지의 합을 구하는데 엄청긴숫자의 합을 구할때는 어떻게 해야하나요? (4) 슬우 2025-06-18
2698616 다른 함수로 안넘어갑니다..;;; 도1도캣 2025-06-18
2698587 배열하다 막혀서... (3) WhiteCat 2025-06-18
2698559 문자열을 비우는방법 (2) 하늘 2025-06-18
2698528 착하고 친절한 선생씌구해염~ㅋㅋ (4) 옆집언니야 2025-06-17
2698502 자료구조 큐 캔서 2025-06-17
2698477 실행화면 배경문의요 선아 2025-06-17
2698430 변수의 값이 저장이 않되네요;; (4) 피네 2025-06-16
2698404 C#을 배울려고 하는데 C나 C++을 알아야 하나요 ?? (1) 신당 2025-06-16
2698342 프로그램 질문점녀 (4) 데빌의눈물 2025-06-16
2698318 파일 입출력 질문입니다~ (2) 꽃 2025-06-15
2698291 문자 출력 함수 : putchar, fputc에 관하여. 으뜸 2025-06-15
2698261 씨언어 (1) 마리 2025-06-15
2698212 구조체, 포인터가 같이 들어간 프로그램 소스코드 있으신분? (4) 그림자 2025-06-14
2698184 간단한 C언어 인데 .. 붕붕 2025-06-14
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com