이 진 트 리 프로 그래 밍 실천

3665 단어 데이터 구조
#define _CRT_SECURE_NO_WARNINGS
#include 
#include
#include 

int num = 0;
/*
typedef struct BiTNode
{
	int data;
	struct BiTNode *lchild,*rchild;
};*/

//        :      
struct BiTNode
{
	int data;
	struct BiTNode *lchild,*rchild;
};
typedef struct BiTNode BiTNode;
typedef struct BiTNode* BITree;

//    
void preOrder(BiTNode*root)
{
	if (root==NULL)
	{
		return;
	}
	printf("%d ",root->data);
	preOrder(root->lchild);
	preOrder(root->rchild);
}
//    
void inOrder(BiTNode*root)
{
	if (root==NULL)
	{
		return;
	}
	inOrder(root->lchild);
	printf("%d ",root->data);
	inOrder(root->rchild);
}
//    
void postOrder(BiTNode*root)
{
	if (root==NULL)
	{
		return;
	}
	postOrder(root->lchild);
	postOrder(root->rchild);
	printf("%d ",root->data);
}
//        
//          ?            
int count(BiTNode*root)
{
	if (root)
	{
		if ((root->lchild==NULL)&&(root->rchild==NULL))
		{
			num++;
			printf("data:%d
",root->data); } count(root->lchild); count(root->rchild); } return num; } // void count2(BiTNode*root,int*num) { if (root) { if ((root->lchild==NULL)&&(root->rchild==NULL)) { (*num)++; printf("data:%d
",root->data); } if (root->lchild) { count2(root->lchild,num); } if (root->rchild) { count2(root->rchild,num); } } } // void count3(BiTNode*root,int*num) { if (root) { if (root->lchild) { count3(root->lchild,num); } if ((root->lchild==NULL)&&(root->rchild==NULL)) { (*num)++; printf("data:%d
",root->data); } if (root->rchild) { count3(root->rchild,num); } } } // int Depth(BiTNode*root) { int depthleft = 0; int depthright = 0; int depthtree = 0; if (root == NULL) { depthtree = 0; return depthtree; } depthleft = Depth(root->lchild); depthright = Depth(root->rchild); depthtree = 1 + (depthleft>depthright?depthleft:depthright); return depthtree; } // Copy BiTNode* CopyTree(BiTNode*root) { BiTNode*newtree = NULL; BiTNode*left = NULL; BiTNode*right = NULL; if (root == NULL) { //tmp = NULL; return newtree; } if (root->lchild) { left = CopyTree(root->lchild); } else { left = NULL; } if (root->rchild) { right = CopyTree(root->rchild); } else { right = NULL; } newtree = (BiTNode*)malloc(sizeof(BiTNode)); if (newtree==NULL) { return NULL; } newtree->lchild = left; newtree->rchild = right; newtree->data = root->data; } void display01() { BiTNode t1,t2,t3,t4,t5,t6; int num = 0; int dep = 0; BiTNode*newtree; memset(&t1,0,sizeof(BiTNode)); memset(&t2,0,sizeof(BiTNode)); memset(&t3,0,sizeof(BiTNode)); memset(&t4,0,sizeof(BiTNode)); memset(&t5,0,sizeof(BiTNode)); memset(&t6,0,sizeof(BiTNode)); t1.data = 1; t2.data = 2; t3.data = 3; t4.data = 4; t5.data = 5; t6.data = 6; // t1.lchild = &t2; t1.rchild = &t3; t2.lchild = &t4; t2.rchild = &t5; t3.lchild = &t6; /* */ printf("

"); preOrder(&t1); printf("

"); inOrder(&t1); printf("

"); postOrder(&t1); count3(&t1,&num); printf("
:%d
",num); dep = Depth(&t1); printf("
:%d
",dep); newtree = CopyTree(&t1); printf("

"); preOrder(newtree); } int main() { display01(); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기