Creation and traversal of binary tree (recursive traversal)

This is a simple construction of a binary tree, the input is an integer .

#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); //Create a stack (for recording position)
BinTree CreateBinTree(); //Create a binary tree
bool AddQ(Queue Q, BinTree BT);
BinTree DeleteQ(Queue Q); //Record the position of the previous node
bool Isempty(Queue Q);
int GetHeight(BinTree BT); //Calculate the height
void PreorderPrintLeaves(BinTree BT); //leaf node
void PreorderTraversal(BinTree BT); //Preorder traversal
void InorderTraversal(BinTree BT); //Inorder Traversal
void PostorderTraversal(BinTree BT); //Subsequent traversal
void display();


intmain()
{
	int i, k, c=0, n=-1;
	BinTree T;
	while(1)
	{
		if(n==5)
		break;
		display();
		scanf("%d", &n);
		switch(n)
		{
			case 1:
				{
				printf("Enter the table of the tree, use '0' to represent the left or right space under the node: ");
				T = CreateBinTree();
				if(T)
				{
				printf("Created successfully!\n");
				c=1; //Create success condition
				}
				else
				printf("Failed to create!\n");
				}break;
			case 2:
				{
					if(c==1)
					{
					printf("The output of preorder traversal is: ");
					PreorderTraversal(T);
					printf("\nInorder traversal output is: ");
					InorderTraversal(T);
					printf("\nThe output of post-order traversal is: ");
					PostorderTraversal(T);
					}
					else
					printf("No new tree has been created\n");	
				}break;
				
			case 3:
				{
					if(c==1)
					printf("The height of the tree is: %d\n", GetHeight(T));
					else
					printf("No new tree has been created\n");
				}break;
			case 4:
				{
					if(c==1)
					{
					printf("All leaf nodes of the tree are: ");
					PreorderPrintLeaves(T);
					}
					else
					printf("No new tree has been created\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()//Binary tree creation
{
	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)//Calculate the height of the tree
{
	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)//Display leaf nodes
{
	if(BT)
	{
		if( !BT->left && !BT->right )
		printf("%d ", BT->date);
		PreorderPrintLeaves(BT->left);
		PreorderPrintLeaves(BT->right);
	}
}


void PreorderTraversal(BinTree BT)//Preorder traversal
{
	if(BT)
	{
		printf("%d ", BT->date);
		PreorderTraversal(BT->left);
		PreorderTraversal(BT->right);
	}
}


void InorderTraversal(BinTree BT)//Inorder Traversal
{
	if(BT)
	{
		InorderTraversal(BT->left);
		printf("%d ", BT->date);
		InorderTraversal(BT->right);
	}
}


void PostorderTraversal(BinTree BT)//Subsequent traversal
{
	if(BT)
	{
		PostorderTraversal(BT->left);
		PostorderTraversal(BT->right);
		printf("%d ", BT->date);
	}
}


void display() //Display menu
{
	int n, i=0;
	printf("\n\n");
	printf(" |..........Binary tree creation................|\n");
	printf(" | 1. Create a binary tree|\n");
	printf(" | 2. Output binary tree|\n");
	printf(" | 3. Display the height of the tree|\n");
	printf(" | 4. Display all leaf nodes|\n");
	printf(" | 5. end|\n");
	printf("                    |.....................................|\n");
	printf("\n\n");
	printf("Select menu: ");

}

Compiler: DEV C++




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324523763&siteId=291194637