9493 두 갈래 나무의 결점 개수 계산하기 (c 언어)

29672 단어 두 갈래 나무
9493은 두 갈래 나무의 결점 개수를 계산했다
제목 설명:
두 갈래 정렬 트리를 구축하고 약간의 데이터를 삽입한 후 트리의 결점 개수를 제시합니다.
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK  1
#define ERROR  0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int  Status;

typedef int  ElemType;
typedef struct BiTNode{
  ElemType data;
  struct BiTNode *lchild,*rchild;//      
} BiTNode,*BiTree;

Status InsertBiTree(BiTree &T,int e) //     
{
	if(T==NULL){
		T=(BiTNode *)malloc(sizeof(BiTNode));
		T->data=e;T->lchild=NULL;
		T->rchild=NULL;
		return 1;
	}
	if(T->data<e)
		InsertBiTree(T->rchild,e);
	else
		InsertBiTree(T->lchild,e);
	return 0;
}



Status PrintElement( ElemType e ) {  //     e  
	printf("%d ", e );
	return OK;
}// PrintElement


Status PreOrderTraverse( BiTree T, Status(*Visit)(ElemType) )//       
 {
	if(T==NULL)  return 0;
	Visit( T->data );
	PreOrderTraverse(T->lchild,Visit);
	PreOrderTraverse(T->rchild,Visit);
	return OK;
} // PreOrderTraverse

Status InOrderTraverse( BiTree T, Status(*Visit)(ElemType) ) //       
{
	if(T==NULL)  return 0;
	InOrderTraverse(T->lchild,Visit);
	Visit(T->data);
	InOrderTraverse(T->rchild,Visit);
	return 1;
} // InOrderTraverse

Status PostOrderTraverse( BiTree T, Status(*Visit)(ElemType) )//       
 {
    if(T==NULL)  return 0;
	PostOrderTraverse(T->lchild,Visit);
	PostOrderTraverse(T->rchild,Visit);
	Visit(T->data);
	return 1;

} // PostOrderTraverse


int  TreeCount( BiTree T)//     
{
	//    
}

int main()   //   
{
	BiTree  T=NULL;
	int i,n,e;
	//       T
	
    // 、 、       
	PreOrderTraverse(T,PrintElement);
	printf("
"
); InOrderTraverse(T,PrintElement); printf("
"
); PostOrderTraverse(T,PrintElement); printf("
"
); // return 0;// }//main-1 (-1 ) ~ 、 、 50 20 10 80 70 -1 50 20 10 80 70 10 20 50 70 80 10 20 70 80 50 5

소스:
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK  1
#define ERROR  0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int  Status;

typedef int  ElemType;
typedef struct BiTNode{
  ElemType data;
  struct BiTNode *lchild,*rchild;//      
} BiTNode,*BiTree;

Status InsertBiTree(BiTree &T,int e) //     
{
	if(T==NULL){
		T=(BiTNode *)malloc(sizeof(BiTNode));
		T->data=e;T->lchild=NULL;
		T->rchild=NULL;
		return 1;
	}
	if(T->data<e)
		InsertBiTree(T->rchild,e);
	else
		InsertBiTree(T->lchild,e);
	return 0;
}



Status PrintElement( ElemType e ) {  //     e  
	printf("%d ", e );
	return OK;
}// PrintElement


Status PreOrderTraverse( BiTree T, Status(*Visit)(ElemType) )//       
 {
	if(T==NULL)  return 0;
	Visit( T->data );
	PreOrderTraverse(T->lchild,Visit);
	PreOrderTraverse(T->rchild,Visit);
	return OK;
} // PreOrderTraverse

Status InOrderTraverse( BiTree T, Status(*Visit)(ElemType) ) //       
{
	if(T==NULL)  return 0;
	InOrderTraverse(T->lchild,Visit);
	Visit(T->data);
	InOrderTraverse(T->rchild,Visit);
	return 1;
} // InOrderTraverse

Status PostOrderTraverse( BiTree T, Status(*Visit)(ElemType) )//       
 {
    if(T==NULL)  return 0;
	PostOrderTraverse(T->lchild,Visit);
	PostOrderTraverse(T->rchild,Visit);
	Visit(T->data);
	return 1;

} // PostOrderTraverse
int sum=0;   //       sum
int  TreeCount( BiTree T)//     
{
	if(T)      //    ,   ,  sum++
    {
        TreeCount(T->lchild);
        TreeCount(T->rchild);
        sum++;
    }
    return 0;
}
int main()   //   
{
	BiTree  T=NULL;
	int i,n,e;
	while(scanf("%d",&n)&&n!=-1)
    {
        InsertBiTree(T,n);
    }
	PreOrderTraverse(T,PrintElement);
	printf("
"
); InOrderTraverse(T,PrintElement); printf("
"
); PostOrderTraverse(T,PrintElement); printf("
"
); TreeCount(T); printf("%d
"
,sum); return 0;// }//main

좋은 웹페이지 즐겨찾기