ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[PTA][C语言][数据结构]树—二叉树遍历

2021-11-13 21:02:00  阅读:186  来源: 互联网

标签:Node 遍历 temp BinaryTree PTA C语言 BT 二叉树 root


1.二叉树的创建和递归遍历

从键盘接受输入扩展先序序列,以二叉链表作为存储结构,建立二叉树。并输出这棵二叉树的先序、中序和后序遍历序列。 二叉树结点的data是字符类型数据, 其中#表示空格字符。
输入格式:

输入扩展先序序列。二叉树结点的data是字符类型数据, 其中#表示空格字符。

输出格式:

第一行输出先序遍历序列;第二行输出中序遍历序列;第三行输出后序遍历序列。

输入样例:

ABC##DE#G##F### 

输出样例:

ABCDEGF
CBEGDFA
CGEFDBA

代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct BinaryTree_Node {
	char value;
	struct BinaryTree_Node* lChild;
	struct BinaryTree_Node* rChild;
} * BT_Node;

// 创建(先序)
BT_Node Create_BinaryTree() {
	BT_Node root = (BT_Node)malloc(sizeof( struct BinaryTree_Node));
	char temp;
	scanf("%c", &temp);
	if (temp != '#') {
		root->value = temp;
		root->lChild = Create_BinaryTree();
		root->rChild = Create_BinaryTree();
	} else {
		root = NULL;
	}
	return root;
}

// 先序遍历 
void Preorder_traverse(BT_Node root) {
	if (root == NULL) {
		return;
	}
	printf("%c", root->value);
	Preorder_traverse(root->lChild);
	Preorder_traverse(root->rChild);
}

// 中序遍历 
void Inorder_traverse(BT_Node root) {
	if (root == NULL) {
		return;
	}
	Inorder_traverse(root->lChild);
	printf("%c", root->value);
	Inorder_traverse(root->rChild);
}

// 后序遍历 
void Postorder_traverse(BT_Node root) {
	if (root == NULL) {
		return;
	}
	Postorder_traverse(root->lChild);
	Postorder_traverse(root->rChild);
	printf("%c", root->value);
}

int main() {
	BT_Node root;
	root = Create_BinaryTree();
	Preorder_traverse(root);
	printf("\n");
	Inorder_traverse(root);
	printf("\n");
	Postorder_traverse(root);
}

2.非递归先序和中序遍历

从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。采取非递归方法输出这棵二叉树的先序、中序遍历序列。
输入格式:

输入扩展先序序列。二叉树结点的data是字符类型数据, 其中#表示空格字符。

输出格式:

第一行输出先序遍历序列,第二行输出中序遍历序列。

输入样例:

ABC##DE#G##F### 

输出样例:

ABCDEGF
CBEGDFA

代码:

#include <stdio.h>
#include <stdlib.h>

#define M 100

typedef struct BinaryTree_Node {
	char value;
	struct BinaryTree_Node* lChild;
	struct BinaryTree_Node* rChild;
} *BT_Node;

typedef struct stack {
	BT_Node elements[M];
	int top;
} seqstack;
//定义一个储存树类型地址的栈,方便遍历的时候追踪到树的地址。

BT_Node root;//定义一个树根
seqstack s;//定义栈

//初始化栈
void setnull() {
	s.top = 0;
}
//入栈操作
void push(BT_Node temp) {
	s.elements[s.top++] = temp;
}
//取栈顶并出栈顶
BT_Node pop() {
	return s.elements[--s.top];
}
//判空
int empty() {
	return s.top == 0;
}
//创建
BT_Node Create_BinaryTree() {
	BT_Node root = (BT_Node)malloc(sizeof(struct BinaryTree_Node));
	char temp;
	scanf("%c", &temp);
	if (temp != '#') {
		root->value = temp;
		root->lChild = Create_BinaryTree();
		root->rChild = Create_BinaryTree();
	} else {
		root = NULL;
	}
	return root;
}
//前序遍历的非递归算法
void Preorder_traverse(BT_Node root) {
	BT_Node temp = root;
	while (temp != NULL || s.top != 0) {
		//先遍历左孩子,并输出。
		while (temp != NULL) {
			printf("%c", temp->value);
			push(temp);
			temp = temp->lChild;
		}
		//当左孩子遍历完后,取栈顶,找右孩子。此时循环还没有结束,再遍历它的左孩子,直至孩子全部遍历结束。
		if (s.top != 0) {
			temp = pop();
			temp = temp->rChild;
		}
	}
	printf("\n");
}
//中序遍历的非递归算法
void Inorder_traverse(BT_Node root) {
	BT_Node temp = root;
	while (temp != NULL || s.top != 0) {
		//先把左孩子入栈,所有左孩子入栈结束
		while (temp != NULL) {
			push(temp);
			temp = temp->lChild;
		}
		//左孩子入栈结束,取栈顶,输出栈顶元素,遍历右孩子
		if (s.top != 0) {
			temp = pop();
			printf("%c", temp->value);
			temp = temp->rChild;
		}
	}
	printf("\n");
}

int main() {
	BT_Node root;
	root = Create_BinaryTree();
	Preorder_traverse(root);
	Inorder_traverse(root);
	//Postorder_traverse(root);
}

3.非递归后序遍历

从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。采取非递归方法输出这棵二叉树的后序遍历序列。
输入格式:

输入扩展先序序列。二叉树结点的data是字符类型数据, 其中#表示空格字符。

输出格式:

输出后序遍历序列。

输入样例:

ABC##DE#G##F### 

输出样例:

CGEFDBA

代码:

#include <stdio.h>
#include <stdlib.h>

#define M 100

typedef struct BinaryTree_Node {
	char value;
	int count;
	struct BinaryTree_Node* lChild;
	struct BinaryTree_Node* rChild;
} *BT_Node;

typedef struct stack {
	BT_Node elements[M];
	int top;
} seqstack;

BT_Node root;
seqstack s;

//初始化
void init() {
	s.top = 0;
}
//入栈操作
void push(BT_Node temp) {
	s.elements[s.top++] = temp;
}
//取栈顶并出栈顶
BT_Node pop() {
	return s.elements[--s.top];
}
//判空
int empty() {
	return s.top == 0;
}
//创建
BT_Node Create_BinaryTree() {
	BT_Node root = (BT_Node)malloc(sizeof(struct BinaryTree_Node));
	char temp;
	scanf("%c", &temp);
	if (temp != '#') {
		root->value = temp;
		root->lChild = Create_BinaryTree();
		root->rChild = Create_BinaryTree();
	} else {
		root = NULL;
	}
	return root;
}
//后序遍历的非递归算法
void Postorder_traverse(BT_Node root) {
	BT_Node temp = root;
	while (temp != NULL || s.top != 0) {
		while (temp != NULL) {
			temp->count = 1;// 当前节点首次被访问
			push(temp);
			temp = temp->lChild;
		}
		if (s.top != 0) {
			temp = pop();
			// 第一次出现在栈顶,继续向右找
			if (temp->count == 1) {
				temp->count++;
				push(temp);
				temp = temp->rChild;
			} else if (temp->count == 2) {//第二次输出并制空,防止陷入死循环
				printf("%c", temp->value);
				temp = NULL;
			}
		}
	}
	printf("\n");
}

int main() {
	BT_Node root;
	init();
	root = Create_BinaryTree();
	Postorder_traverse(root);
}

3.层次遍历

从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。输出这棵二叉树的层次遍历序列。
输入格式:

输入扩展先序序列。二叉树结点的data是字符类型数据, 其中#表示空格字符。

输出格式:

输出层次遍历序列。

输入样例:

ABC##DE#G##F### 

输出样例:

ABCDEFG

代码:

#include <stdio.h>
#include <stdlib.h>

#define Max 100

typedef struct BinaryTree_Node {
	char value;
	int count;
	struct BinaryTree_Node* lChild;
	struct BinaryTree_Node* rChild;
} *BT_Node;

typedef struct queue {
	BT_Node elements[Max];
	int front;
	int rear;
} Queue;

BT_Node root;
Queue q;

//初始化
void init() {
	q.front= 0;
	q.rear = 0;
}
//入栈操作
void push(BT_Node temp) {
	q.elements[++q.rear] = temp;
}
//取栈顶并出栈顶
BT_Node pop() {
	return q.elements[++q.front];
}
//判空
int empty() {
	return q.rear == q.front;
}
//创建
BT_Node Create_BinaryTree() {
	BT_Node root = (BT_Node)malloc(sizeof(struct BinaryTree_Node));
	char temp;
	scanf("%c", &temp);
	if (temp != '#') {
		root->value = temp;
		root->lChild = Create_BinaryTree();
		root->rChild = Create_BinaryTree();
	} else {
		root = NULL;
	}
	return root;
}
//二叉树的层次遍历
void LevelOrder_Traversal(BT_Node root) {
	BT_Node temp;
	push(root);
	while (!empty()) {
		temp = pop();
		printf("%c", temp->value);
		if (temp->lChild) {
			push(temp->lChild);
		}
		if (temp->rChild) {
			push(temp->rChild);
		}
	}
	printf("\n");
}

int main() {
	init();
	root = Create_BinaryTree();
	LevelOrder_Traversal(root);
}

5.结点个数

从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。分别统计二叉树中叶子结点、度为1的结点、度为2的结点的个数,并输出。
输入格式:

输入扩展先序序列。二叉树结点的data是字符类型数据, 其中#表示空格字符。

输出格式:

第一行依次输出叶子结点个数、度为1的结点个数、度为2的结点个数,以空格隔开。 第二行连续输出叶子结点,中间不间隔。

输入样例:

ABC##DE#G##F###

输出样例:

3 2 2
CGF

代码:

#include <stdio.h>
#include <stdlib.h>

#define M 100

typedef struct BinaryTree_Node {
	char value;
	struct BinaryTree_Node* lChild;
	struct BinaryTree_Node* rChild;
} *BT_Node;

int numberOfLeafNodes = 0;
int numberOf1Nodes = 0;
int numberOf2Nodes = 0;
char leaves[M] = { 0 };

//创建
BT_Node Create_BinaryTree() {
	BT_Node root = (BT_Node)malloc(sizeof(struct BinaryTree_Node));
	char temp;
	scanf("%c", &temp);
	if (temp != '#') {
		root->value = temp;
		root->lChild = Create_BinaryTree();
		root->rChild = Create_BinaryTree();
	}
	else {
		root = NULL;
	}
	return root;
}

//先序遍历
void Preorder_traverse(BT_Node root) {
	if (root == NULL) {
		return;
	}
	if (root->lChild && root->rChild) {
		numberOf2Nodes++;
	} else if (root->lChild || root->rChild) {
		numberOf1Nodes++;
	} else {
		leaves[numberOfLeafNodes++] = root->value;
	}
	Preorder_traverse(root->lChild);
	Preorder_traverse(root->rChild);
}

int main() {
	BT_Node root;
	root = Create_BinaryTree();
	Preorder_traverse(root);
	printf("%d %d %d\n", numberOfLeafNodes, numberOf1Nodes, numberOf2Nodes);
	printf("%s", leaves);
}

6.二叉树的高度

从键盘接收扩展先序序列,以二叉链表作为存储结构,建立二叉树。计算二叉树的高度,并输出。
输入格式:

输入扩展先序序列。二叉树结点的data是字符类型数据, 其中#表示空格字符。

输出格式:

输出一个整数。

输入样例:

ABC##DE#G##F###

输出样例:

5

代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct BinaryTree_Node {
	char value;
	struct BinaryTree_Node* lChild;
	struct BinaryTree_Node* rChild;
} *BT_Node;

int MAX = 0;

//创建
BT_Node Create_BinaryTree() {
	BT_Node root = (BT_Node)malloc(sizeof(struct BinaryTree_Node));
	char temp;
	scanf("%c", &temp);
	if (temp != '#') {
		root->value = temp;
		root->lChild = Create_BinaryTree();
		root->rChild = Create_BinaryTree();
	} else {
		root = NULL;
	}
	return root;
}

// 先序遍历 
void Preorder_traverse(BT_Node root, int depth) {
	if (root == NULL) {
		return;
	}
	if (depth > MAX) {
		MAX = depth;
	}
	Preorder_traverse(root->lChild, depth + 1);
	Preorder_traverse(root->rChild, depth + 1);
}

int main() {
	BT_Node root;
	root = Create_BinaryTree();
	Preorder_traverse(root, 1);
	printf("%d", MAX);
}

标签:Node,遍历,temp,BinaryTree,PTA,C语言,BT,二叉树,root
来源: https://blog.csdn.net/weixin_52192405/article/details/121197160

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有