1.数据类型定义

在代码中为了清楚的表示一些错误和函数运行状态,我们预先定义一些变量来表示这些状态。在head.h头文件中有如下定义:

//定义数据结构中要用到的一些变量和类型
#ifndef HEAD_H
#define HEAD_H

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.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;   //用户定义的数据类型

#endif
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.



2.数据结构实现

typedef struct BiNode{
	ElemType data;
	struct BiNode *left,*right;
}BiNode,*pBiNode;
  • 1.
  • 2.
  • 3.
  • 4.



3.二叉树代码实现

BiTree.h代码如下:

#ifndef BITREE_H
#define BITREE_H

#include "head.h"


typedef struct BiNode{
	ElemType data;
	struct BiNode *left,*right;
}BiNode,*pBiNode;


Status InsertRight(pBiNode &root,ElemType e);
Status InsertLeft(pBiNode &root,ElemType e);


Status InitBiTree(pBiNode &tree){
	tree=(pBiNode)malloc(sizeof(BiNode));
	if(!tree) return OVERFLOW;
	tree->data=-999999;
	tree->left=NULL;
	tree->right=NULL;
	return OK;
}
Status BiTreeEmpty(pBiNode root){
	if(root==NULL) return ERROR;
	return root->left==root->right && root->data==-999999;
}

Status HasNoNode(pBiNode root){
	if(root==NULL) return ERROR;
	return root->left==root->right ;
}

Status CreatTreeNode(pBiNode &node,ElemType e){
	node=(pBiNode)malloc(sizeof(BiNode));
	if(!node) return OVERFLOW;
	node->data=e;
	node->left=NULL;
	node->right=NULL;
	return OK;
}
Status InsertRight(pBiNode &root,ElemType e){
	if(root->right==NULL){
		if(e>root->data){
			pBiNode p;
			CreatTreeNode(p,e);
			root->right=p;
			return OK;
		}else{
			pBiNode p;
			CreatTreeNode(p,e);
			root->left=p;
			return OK;
		}

	}else{
		e>root->data? InsertRight(root->right,e):InsertLeft(root,e);
	}

}
Status InsertLeft(pBiNode &root,ElemType e){
	if(root->left==NULL){
		if(e>root->data){
			pBiNode p;
			CreatTreeNode(p,e);
			root->right=p;
			return OK;
		}else{
			pBiNode p;
			CreatTreeNode(p,e);
			root->left=p;
			return OK;
		}

	}else{
		e<=root->data?InsertLeft(root->left,e):InsertRight(root,e);
	}

}


Status InsertTree(pBiNode &root,ElemType e){
	if(BiTreeEmpty(root)){
		root->data=e;
		return true;
	}
	if(e>root->data){
		InsertRight(root,e);
	}else{
		InsertLeft(root,e);
	}
}


Status CreateBiTree(pBiNode &root,ElemType *a,int n){
	for (int i=0;i<n;i++)
	{
		InsertTree(root,a[i]);
	}
	return true;
}
Status print(ElemType e ){
	printf("%d ",e);
	return true;
}

Status PreOrderTraverse(pBiNode root,Status(*p)(int)){
	if(root){
		(*p)(root->data);
		PreOrderTraverse(root->left,p);
		PreOrderTraverse(root->right,p);
	}
	return OK;
}

Status MiddleOrderTraverse(pBiNode root,Status(*p)(int)){
	if(root){
		MiddleOrderTraverse(root->left,p);
		(*p)(root->data);
		MiddleOrderTraverse(root->right,p);
	}
	return OK;
}

Status AfterOrderTraverse(pBiNode root,Status(*p)(int)){
	if(root){
		AfterOrderTraverse(root->left,p);
		AfterOrderTraverse(root->right,p);
		(*p)(root->data);
	}
	return OK;
}

Status ClearBiTree(pBiNode &root){
	if(root){
		ClearBiTree(root->left);
		ClearBiTree(root->right);
		free(root);
		root==NULL;
	}
	return OK;
}


#endif
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.



4.测试代码

#include "BiTree.h"

void main(){
	ElemType a[14]={100,50,200,40,30,45,60,55,61,200,150,300,250,400};
	pBiNode root;
	InitBiTree(root);
	CreateBiTree(root,a,14);

	printf("前序:");
	PreOrderTraverse(root,print);

	printf("\n中序:");
	MiddleOrderTraverse(root,print);

	printf("\n后序:");
	AfterOrderTraverse(root,print);

	ClearBiTree(root);

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.


5.插入数据如下:

(C语言)二叉树实现(数据结构十三)_#define


6.测试结果

前序:100 50 40 30 45 60 55 61 200 150 300 250 400
中序:30 40 45 50 55 60 61 100 150 200 250 300 400
后序:30 45 40 55 61 60 50 150 250 400 300 200 100
  • 1.
  • 2.
  • 3.