이 진 트 리 옮 겨 다 니 는 응용

3955 단어 #데이터 구조
이 진 트 리 가 옮 겨 다 니 는 응용.
이 진 트 리 노드 갯 수 구하 기
//        
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;
}

저작권 성명: 본 고 는 블 로 거들 의 오리지널 글 입 니 다. 만약 에 잘못 이 있 으 면 댓 글 에서 지적 해 주 십시오. 감사합니다. 출처 를 밝 히 려 면 ~

좋은 웹페이지 즐겨찾기