선순, 중순, 후순, 잎결점수, 깊이, 복사, 몇 가지 두 갈래 나무의 흔한 귀속 사용 방법

8991 단어 두 갈래 나무
//vs2015 번역 통과.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"


typedef struct Node
{
    int data;
    struct Node *lchild, *rchild;
}BiNode , *BiTree;



// 
void  inOrder(BiNode *root)
{
    if (root!=NULL)
    {
        inOrder(root->lchild);
        printf("data:%d 
"
, root->data); inOrder(root->rchild); } } // void postOrder(BiNode *root) { if (root != NULL) { postOrder(root->lchild); postOrder(root->rchild); printf("data:%d
"
, root->data); } } // void preOrder(BiNode *root) { if (root != NULL) { printf("data:%d
"
, root->data); preOrder(root->lchild); preOrder(root->rchild); } } // // void getLeafNum(BiNode *root,int *num) { if (root != NULL) { printf("data:%d
"
, root->data); if (root->lchild ==NULL && root->rchild==NULL) { (*num)++; } getLeafNum(root->lchild,num); getLeafNum(root->rchild,num); } } int GetDepth(BiNode *root) { if (root!=NULL) { int left = 1; int right = 1; left += GetDepth(root->lchild); right += GetDepth(root->rchild); return left > right ? left : right; } return 0; } // BiNode *copyTree(BiNode *T) { BiNode *newLptr = NULL; BiNode *newRptr = NULL; BiNode *newNode = NULL; if (T->lchild!=NULL) { newLptr = copyTree(T->lchild); } if (T->rchild !=NULL) { newRptr = copyTree(T->lchild); } newNode = (BiNode *)malloc(sizeof(BiNode)); if (newNode==NULL) { return NULL; } newNode->lchild = newLptr; newNode->rchild = newRptr; newNode->data = T->data; return newNode; } void main() { BiNode t1, t2, t3, t4, t5; memset(&t1, 0, sizeof(BiNode)); memset(&t2, 0, sizeof(BiNode)); memset(&t3, 0, sizeof(BiNode)); memset(&t4, 0, sizeof(BiNode)); memset(&t5, 0, sizeof(BiNode)); t1.data = 1; t2.data = 2; t3.data = 3; t4.data = 4; t5.data = 5; t1.lchild = &t2; t1.rchild = &t3; t2.lchild = &t4; t3.lchild = &t5; printf("
"
); inOrder(&t1); printf("
"
); preOrder(&t1); printf("
"
); postOrder(&t1); int num = 0; getLeafNum(&t1, &num); printf("leaf num%d
"
, num); printf("depth :%d
"
,GetDepth(&t1)); { // BiNode *newtree = NULL; newtree =copyTree(&t1); } system("pause"); }

좋은 웹페이지 즐겨찾기