二叉树的建立与四种遍历

二叉树的建立与七种遍历方法

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

typedef struct node
{
	char data;     // 节点数据
	struct node *left, *right;   // 左,右节点
}*Node;

node *input()    // 使用前序遍历的方法建立一棵二叉树
{
	node *p;
	char c;
	scanf_s("%c", &c);
	if (c == '#')   // 如果输入是#号,表示是一棵空树
	{
		p = NULL;
	}
	else
	{
		p = (struct node *)malloc(sizeof(node));
		p->data = c;
		p->left = input();
		p->right = input();

	}
	return p;
}

typedef struct
{
	Node data[MAXSIZE];    // 节点数组
	int rear, front;        // 队尾和队头
}SeQueue;

SeQueue *init_Queue()   // 初始化队列
{
	SeQueue *sq;
	sq = (SeQueue*)malloc(sizeof(SeQueue));
	sq->front = sq->rear = -1;    // 初始化为-1
	return sq;
}

int Empty_Queue(SeQueue *sq)    // 空队列
{
	if (sq->front == sq->rear)
		return 1;
	else
		return 0;
}

void In_Queue(SeQueue  *sq, Node x)    // 入队操作
{
	if (sq->rear == MAXSIZE - 1)
	{
		printf("\n顺序队列是满的!");
		exit(1);
	}
	sq->rear++;
	sq->data[sq->rear] = x;
}

void Out_Queue(SeQueue  *sq, Node x)    // 出队操作
{
	if (Empty_Queue(sq))
	{
		printf("\n顺序队列是空的!不能做删除操作!");
		exit(1);
	}
	sq->front++;
	x = sq->data[sq->front];
}

void LevelOrder(Node T)      // 层次遍历
{
	Node p = T;
	SeQueue *sq = init_Queue();    //初始化队列
	In_Queue(sq, p);     //根节点入队
	while (!Empty_Queue(sq))    //队列不空循环
	{
		p = sq->data[sq->front + 1];   // 队头元素
		printf("%c ", p->data);
		Out_Queue(sq, p);      //对头元素出队
		if (p->left != NULL)       //左子树不空则入队
		{
			In_Queue(sq, p->left);
		}
		if (p->right != NULL)      //右子树不空则入队
		{
			In_Queue(sq, p->right);
		}
	}
}

void PreOrder(Node t)     // 非递归前序遍历
{
	Node stack[MAXSIZE];         // 栈数组
	int top = -1;               // 标识符
	while (t || top != -1)
	{
		while (t)
		{
			printf("%c ", t->data);
			top++;
			stack[top] = t;  // 入栈
			t = t->left;     // 判断左节点是否为空
		}
		if (top > -1)
		{
			t = stack[top];    // 把栈顶的值赋给t
			top--;             // 因为栈顶已经输出过,出栈
			t = t->right;
		}
	}
}

void preorder(Node t)    // 递归前序遍历
{
	if (t)
	{
		printf("%c ", t->data);
		preorder(t->left);
		preorder(t->right);
	}
}

void InOrder(Node t)    // 非递归中序遍历
{
	Node stack[MAXSIZE];
	int top = -1;
	while (t || top != -1)
	{
		while (t)
		{
			top++;
			stack[top] = t;
			t = t->left;      // 一直入栈,直到左节点为空
		}
		if (top > -1)
		{
			t = stack[top];     // 把栈顶赋给t
			top--;              // 出栈  
			printf("%c ", t->data);      // 输出栈顶元素
			t = t->right;
		}
	}
}

void inorder(Node t)     // 递归中序遍历
{
	if (t)
	{
		inorder(t->left);
		printf("%c ", t->data);
		inorder(t->right);
	}
}

void PostOrder(Node t)    // 非递归后序遍历                   
{                         // 先遍历左子树,再遍历右字树
	Node stack[MAXSIZE];    // 入栈的元素
	int stack1[MAXSIZE];     // 记录该输出的元素
	int top = -1;
	while (t || top != -1)
	{
		while (t)
		{
			top++;
			stack[top] = t;       // 入栈 
			stack1[top] = 0;
			t = t->left;
		}
		while (top > -1 && stack1[top] == 1)    // 输出结点元素
		{
			t = stack[top];
			printf("%c ", t->data);
			top--;
		}
		if (top > -1)   // 判断右节点
		{
			t = stack[top];
			stack1[top] = 1;
			t = t->right;
		}
		else
		{
			t = NULL;
		}
	}
}

void postorder(Node t)    // 递归后序遍历
{
	if (t)
	{
		postorder(t->left);
		postorder(t->right);
		printf("%c ", t->data);
	}
}

int main()
{
	Node p;
	p = (struct node *)malloc(sizeof(node));
	p = input();
	printf("层次遍历:");
	LevelOrder(p);
	printf("\n");
	printf("非递归前序遍历:");
	PreOrder(p);
	printf("\n");
	printf("递归前序遍历:");
	preorder(p);
	printf("\n");
	printf("非递归中序遍历:");
	InOrder(p);
	printf("\n");
	printf("递归中序遍历:");
	inorder(p);
	printf("\n");
	printf("非递归后序遍历:");
	PostOrder(p);
	printf("\n");
	printf("递归后序遍历:");
	postorder(p);
	printf("\n");
	system("pause");
	return 0;
}

在这里插入图片描述

发布了60 篇原创文章 · 获赞 6 · 访问量 7761

猜你喜欢

转载自blog.csdn.net/qq_44205272/article/details/103438607