이 진 트 리 옮 겨 다 니 는 응용
이 진 트 리 노드 갯 수 구하 기
//
int Size(BinaryTreeNode *t){
if(!t) return 0;
return Size(t->LChild) + Size(t->RChild) + 1;
}
이 진 트 리 잎 결산 점 개 수 를 구하 다.
//
int Leaf(BinaryTreeNode *t){
if(t==NULL) return 0;
if((t->LChild == NULL) && (t->RChild == NULL)) return 1;
return Leaf(t->LChild) + Leaf(t->RChild);
}
이 진 트 리 높이 구하 기
//
int Depth(BinaryTreeNode *t){
if(!t) return 0;
// else return (1 + Depth(t->LChild) > Depth(t->RChild) ? Depth(t->LChild) : Depth(t->RChild));
else return 1 + max(Depth(t->LChild) , Depth(t->RChild));
}
이 진 트 리 교환
//
void Exch(BinaryTreeNode *t){
if(t){
BinaryTreeNode *q = t->LChild;
t->LChild = t->RChild;
t->RChild = q;
Exch(t->LChild);
Exch(t->RChild);
}
}
이 진 트 리 도 를 1 결 포인트 로 구하 세 요.
// 1
int Count1(BinaryTreeNode *t){
if(t == NULL) return 0;
if((t->LChild == NULL && t->RChild != NULL) || (t->LChild != NULL && t->RChild == NULL))
return 1;
return Count1(t->LChild) + Count1(t->RChild);
}
이 진 트 리 도 를 2 결 포인트 로 구하 세 요.
// 2
int Count2(BinaryTreeNode *t){
if(t == NULL) return 0;
else if((t->LChild == NULL) && (t->RChild == NULL)) return 0;
else if((t->LChild == NULL) && (t->RChild != NULL)) Count2(t->RChild);
else if((t->LChild != NULL) && (t->RChild == NULL)) Count2(t->LChild);
else return Count2(t->LChild) + Count2(t->RChild) + 1;
}
//
int NumberOfTwoDegree(BinaryTreeNode *t){
if(t == NULL) return 0;
else if(t->LChild != NULL && t->RChild != NULL)
return NumberOfTwoDegree(t->LChild) + NumberOfTwoDegree(t->RChild) + 1;
else return NumberOfTwoDegree(t->LChild) + NumberOfTwoDegree(t->RChild);
}
이 진 트 리 복사
//
BinaryTreeNode *Copy(BinaryTreeNode *p){
if(!p) return NULL;
BinaryTreeNode *q = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
q->Data = p->Data;
q->LChild = Copy(p->LChild);
q->RChild = Copy(p->RChild);
return q;
}
이 진 트 리 삭제
//
void Clear(BinaryTreeNode *t){
if(t){
Clear(t->LChild);
Clear(t->RChild);
free(t);
}
}
2 차원 트 리 옮 겨 다 니 기
//
//
void BreadthFirstSearch(BinaryTreeNode *t){
Queue Q;
BinaryTreeNode * node;
EnQueue(&Q, t->data);
while(!IsEmpty(&Q)){
Front(&Q, node);
DeQueue(&Q);
printf("%d", node->LChild);
if(node->LChild) EnQueue(&Q, node->LChild);
if(node->RChild) EnQueue(&Q, node->RChild);
}
}
//
//
void Levelorder(BinaryTreeNode *t){
BinaryTreeNode *p;
Queue *Q;
Create(&Q, 100);
if(t != NULL){
EnQueue(&Q, t);
while(!IsEmpty(&Q)){
Front(&Q, p);
DeQueue(Q);
printf("%c",p->data);
if(p->LChild) EnQueue(&Q, p->LChild);
if(p->RChild) EnQueue(&Q, p->RChild);
}
}
}
두 그루 의 이 진 트 리 가 같 는 지 아 닌 지 를 판단 하 다.
//
bool Compare(BinaryTreeNode *t, BinaryTreeNode *p){
if(t && p){
if(t->Data != p->Data)
return FALSE;
return Compare(t->LChild, p->LChild) && Compare(t->RChild, p->RChild);
}
else if(t || p)
return FALSE;
return TRUE;
}
저작권 성명: 본 고 는 블 로 거들 의 오리지널 글 입 니 다. 만약 에 잘못 이 있 으 면 댓 글 에서 지적 해 주 십시오. 감사합니다. 출처 를 밝 히 려 면 ~
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Rails Turbolinks를 페이지 단위로 비활성화하는 방법원래 Turobolinks란? Turbolinks는 링크를 생성하는 요소인 a 요소의 클릭을 후크로 하고, 이동한 페이지를 Ajax에서 가져옵니다. 그 후, 취득 페이지의 데이터가 천이 전의 페이지와 동일한 것이 있...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.