二叉树的创建和遍历(递归遍历 · 数据结构)

这是二叉树的简单建立,输入的是整型。这是我的个人建议,二叉树的基础就是创建和遍历的理解,好好打出这份代码挺重要的。堆栈和链表都用上了,但我的代码里,堆栈是不用于存储,而是用于位置的记录,链表才是用于数据的存储,这个要分清。

#include<stdio.h>
#include<malloc.h>
#define NoInfo 0
#define MAXSIZE 100


typedef struct TNode *Position;
typedef Position BinTree;
typedef Position Queue;
struct TNode
{
	int date;
	Position DL[100];
	BinTree left, right;
	int rear, front;
	int MaxSize;
};


Queue CreateQueue(int MaxSize); //创建堆栈(用于记录位置) 
BinTree CreateBinTree(); //创建二叉树 
bool AddQ(Queue Q, BinTree BT);
BinTree DeleteQ(Queue Q); //记录上一个结点位置 
bool Isempty(Queue Q);
int GetHeight(BinTree BT); //计算高度 
void PreorderPrintLeaves(BinTree BT); //叶节点
void PreorderTraversal(BinTree BT); //先序遍历 
void InorderTraversal(BinTree BT); //中序遍历 
void PostorderTraversal(BinTree BT); //后续遍历
void display();


int main()
{
	int i, k, c=0, n=-1;
	BinTree T;
	while(1)
	{
		if(n==5)
		break;
		display();
		scanf("%d", &n);
		switch(n)
		{
			case 1:
				{
				printf("输入树的表,结点下的左或右有空位就用'0'表示:");
				T = CreateBinTree();
				if(T)
				{
				printf("创建成功!\n");
				c=1;	//创建成功条件
				} 
				else
				printf("创建失败!\n");
				}break;
			case 2:
				{
					if(c==1)
					{
					printf("先序遍历输出为:");
					PreorderTraversal(T);
					printf("\n中序遍历输出为:");
					InorderTraversal(T);
					printf("\n后序遍历输出为:"); 
					PostorderTraversal(T);
					}
					else
					printf("还未创建新的树\n");	
				}break;
				
			case 3:
				{
					if(c==1)
					printf("树的高度为:%d\n", GetHeight(T));
					else
					printf("还未创建新的树\n");
				}break;
			case 4:
				{
					if(c==1)
					{
					printf("树的所有叶节点是:");
					PreorderPrintLeaves(T);
					}
					else
					printf("还未创建新的树\n");
				}	
		}
	}
	 
    return 0;
}


Queue CreateQueue(int MaxSize)
{
	Queue Q = (Queue)malloc(sizeof(struct TNode));
	Q->rear = Q->front = 0;
	Q->MaxSize = MaxSize;
	
	return Q;
}


BinTree CreateBinTree()//二叉树创建
{
	int DATE;
	BinTree BT, T;
	Queue Q = CreateQueue(MAXSIZE);
	scanf("%d", &DATE);
	if(DATE!=NoInfo)
	{
		BT = (BinTree)malloc(sizeof(struct TNode));
		BT->date = DATE;
		BT->left = BT->right = NULL;
		AddQ(Q, BT);
	}
	else return NULL;
	while(!Isempty(Q))
	{
		T = DeleteQ(Q);
		scanf("%d", &DATE);
		if(DATE==NoInfo) T->left = NULL;
		else
		{
		T->left = (BinTree)malloc(sizeof(struct TNode));
		T->left->date = DATE;
		T->left->left = T->left->right = NULL;
		AddQ(Q, T->left);
		}
		scanf("%d", &DATE);
		if(DATE==NoInfo) T->right = NULL;
		else
		{
		T->right = (BinTree)malloc(sizeof(struct TNode));
		T->right->date = DATE;
		T->right->left = T->right->right = NULL;
		AddQ(Q, T->right);
		}
	}
	return BT;
}


bool AddQ(Queue Q, BinTree BT)
{
    Q->rear=(Q->rear+1) % Q->MaxSize;
    Q->DL[Q->rear] = BT;
}


BinTree DeleteQ(Queue Q)
{
	Q->front = (Q->front+1) % Q->MaxSize;
	return Q->DL[Q->front];
}


bool Isempty(Queue Q)
{
	return (Q->rear==Q->front);
}


int GetHeight(BinTree BT)//计算树的高度 
{
	int HL, HR, MaxH;
	if(BT)
	{
		HL = GetHeight(BT->left);
		HR = GetHeight(BT->right);
		MaxH = HL>HR ? HL:HR;
		
		return ( MaxH + 1 );
	}
	else
	return 0;
}


void PreorderPrintLeaves(BinTree BT)//显示叶节点 
{
	if(BT)
	{
		if( !BT->left && !BT->right ) 
		printf("%d ", BT->date);
		PreorderPrintLeaves(BT->left);
		PreorderPrintLeaves(BT->right);
	}
}


void PreorderTraversal(BinTree BT)//先序遍历 
{
	if(BT)
	{
		printf("%d ", BT->date);
		PreorderTraversal(BT->left);
		PreorderTraversal(BT->right);
	}
}


void InorderTraversal(BinTree BT)//中序遍历 
{
	if(BT)
	{
		InorderTraversal(BT->left);
		printf("%d ", BT->date);
		InorderTraversal(BT->right);
	}
}


void PostorderTraversal(BinTree BT)//后续遍历 
{
	if(BT)
	{
		PostorderTraversal(BT->left);
		PostorderTraversal(BT->right);
		printf("%d ", BT->date);
	}
}


void display() //显示菜单 
{
	int n, i=0;
	printf("\n\n");
	printf("                    |...........二叉树创建................|\n");
	printf("                    |           1.创建 二叉树             |\n");
	printf("                    |           2.输出 二叉树             |\n");
	printf("                    |           3.显示树的高度            |\n");
	printf("                    |           4.显示所有叶结            |\n");
	printf("                    |           5.结       束             |\n");
	printf("                    |.....................................|\n"); 
	printf("\n\n");
	printf("选择菜单:");

}

编译器:DEV C++




阅读更多

没有更多推荐了,返回首页