二叉树递归与非递归的广度优先与深度优先及树的深度C语言实现

先是生成了一个.h文件方便后面使用

typedef struct BinaryTree{
	char data;
	struct BinaryTree* left;
	struct BinaryTree* right;
}BinaryTree;
typedef struct Tree{
	struct BinaryTree* tree;
	int nodenums;
}Tree;
Tree* createBinaryTree(){
	Tree* tree = (Tree*)malloc(sizeof(Tree));
	tree->nodenums = 0;
	tree->tree = NULL;
	return tree;
}
BinaryTree* createBinaryTreeNode(char op){
	BinaryTree* node = (BinaryTree*)malloc(sizeof(BinaryTree));
	node->data = op;
	node->left = NULL;
	node->right = NULL;
}
BinaryTree* lineBinaryTreeNode(BinaryTree* head,BinaryTree* leftnode,BinaryTree* rightnode){
	head->left = leftnode;
	head->right = rightnode;
	return head;
}
Tree* lineTree(Tree* treeptr,BinaryTree* head){
	treeptr->tree = head;
	return treeptr;
}
Tree* getBinaryTree1(){
	Tree* tree = createBinaryTree();
	BinaryTree* node1 = createBinaryTreeNode('A');
	BinaryTree* node2 = createBinaryTreeNode('B');
	BinaryTree* node3 = createBinaryTreeNode('C');
	BinaryTree* node4 = createBinaryTreeNode('D');
	BinaryTree* node5 = createBinaryTreeNode('E');
	BinaryTree* node6 = createBinaryTreeNode('F');
	BinaryTree* node7 = createBinaryTreeNode('G');
	BinaryTree* node8 = createBinaryTreeNode('H');
	BinaryTree* node9 = createBinaryTreeNode('I');
	BinaryTree* node10 = createBinaryTreeNode('J');
	BinaryTree* node11 = createBinaryTreeNode('K');
	BinaryTree* node12 = createBinaryTreeNode('L');
	tree = lineTree(tree,node1);
	node1 = lineBinaryTreeNode(node1,node2,node3);
	node2 = lineBinaryTreeNode(node2,node4,node5);
	node3 = lineBinaryTreeNode(node3,node6,node7);
	node4 = lineBinaryTreeNode(node4,node8,NULL);
	node5 = lineBinaryTreeNode(node5,node9,node10);
	node7 = lineBinaryTreeNode(node7,node11,NULL);
	node11 = lineBinaryTreeNode(node11,NULL,node12);
	return tree;
}

getBinaryTree1函数返回的二叉树图示
在这里插入图片描述

这是递归的深度优先,三个位置分别对应前序中序后序

#include<stdio.h>
#include<stdlib.h>
#include"binarytree.h"
void DFS(BinaryTree* tree);
int main()
{
	Tree* tree = getBinaryTree1();
	DFS(tree->tree);
}
void DFS(BinaryTree* T){
	if(T == NULL) return ;
//  	printf("%c",T->data);
		DFS(T->left);
//		printf("%c",T->data);
		DFS(T->right);
//		printf("%c",T->data);
	
}

这是非递归实现

#include<stdio.h>
#include<stdlib.h>
#include"binarytree.h"
void NDFSHEAD(BinaryTree* node);
void NDFSCENTER(BinaryTree* node);
void NDFSBACK(BinaryTree* node);

int main()
{
	Tree* tree = getBinaryTree1();
//	NDFSHEAD(tree->tree);
//	NDFSCENTER(tree->tree);
//	NDFSBACK(tree->tree); 
}
void NDFSHEAD(BinaryTree* node){
	BinaryTree* stack[15] = {NULL};
	int top = -1;
	stack[++top] = node;
	BinaryTree* p;
	while(top != -1){
		p = stack[top--];
		printf("%c",p->data);
		if(p->right != NULL) stack[++top] = p->right;
		if(p->left != NULL) stack[++top] = p->left;
	}
}
void NDFSCENTER(BinaryTree* node){
	BinaryTree* stack[15] = {NULL};
	int top = -1;
	BinaryTree* p = node;
	while(top!=-1||p!=NULL){
		while(p!=NULL){
			stack[++top] = p;
			p = p->left;
		}
		if(top != -1){
			p = stack[top--];
			printf("%c",p->data);
			p = p->right;
		}
	}
}
void NDFSBACK(BinaryTree* node){
	BinaryTree* stack[15] = {NULL};
	BinaryTree* stack1[15] = {NULL};
	int top = -1;
	int top1 = -1;
	BinaryTree* p;
	stack[++top] = node;
	while(top != -1){
		p = stack[top--];
		stack[++top1] = p;
		if(p->left != NULL) stack[++top] = p->left;
		if(p->right != NULL) stack[++yo] = p->right;
	}
	while(top1 != -1){
		p = stack[top1--];
		printf("%c",p->data);
	}
}

广度优先遍历递归

#include<stdio.h>
#include<stdlib.h>
#include"binarytree.h"
void BFS(BinaryTree* node);
int depth(BinaryTree* node);
void floorBFS(BinaryTree* node,int i);
int main()
{
	Tree* tree1 = getBinaryTree1();
	BFS(tree1->tree);
}
int depth(BinaryTree* node){
	int leftDepth = 0;
	int rightDepth = 0;
	int value = 0;
	if(node == NULL) return value;
	leftDepth = depth(node->left);
	rightDepth = depth(node->right);
	value = 1 + (leftDepth>rightDepth?leftDepth:rightDepth);
	return value;
}
void BFS(BinaryTree* node){
	if(node == NULL) return;
	for(int i = 1;i<=depth(node);i++) floorBFS(node,i);
}
void floorBFS(BinaryTree* node,int i){
	if(node == NULL) return;
	if(i == 1){
		printf("%c",node->data);
		return;
	}
	floorBFS(node->left,i-1);
	floorBFS(node->right,i-1);
}

广度优先非递归

#include<stdio.h>
#include<stdlib.h>
#include"binarytree.h"
void NBFS(BinaryTree* node);
int main()
{
	Tree* tree = getBinaryTree1();
	NBFS(tree->tree);
}
void NBFS(BinaryTree* node){
	BinaryTree* array[15] = {NULL};
	int tail = 0;
	int head = 0;
	array[tail++] = node;
	while(node!=NULL){
		printf("%c",array[head]->data);	
		if(node->left!=NULL) array[tail++] = node->left;
		if(node->right!=NULL) array[tail++] = node->right;
		node = array[++head];
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不是写算法我在创造世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值