c语言 实现森林和二叉树之间的相互转换给定一个森林(由大于等于一棵树组成),将其转化成一颗二叉树;给定一棵二叉树,将其转化成一个森林; 提示:1) 森林中的树可以采用双亲孩子表示,也可采用邻接表表

c语言   实现森林和二叉树之间的相互转换给定一个森林(由大于等于一棵树组成),将其转化成一颗二叉树;给定一棵二叉树,将其转化成一个森林; 提示:1) 森林中的树可以采用双亲孩子表示,也可采用邻接表表示;2) 二叉树采用动态二叉链表表示; 拜托拜托 能自己输入数据 选择森林转换为二叉树还是二叉树转换为森林 写完整代码给我噢     两种转换都要实现  球球啦
别有错误 我无法改正 不要框架 要完整代码

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

// 定义二叉树结点结构
struct TreeNode {
	int data;
	struct TreeNode* left;
	struct TreeNode* right;
};

// 定义森林中的树结点结构
struct ForestNode {
	int data;
	struct ForestNode* nextbrother;
	struct ForestNode* child;
};

// 函数:将森林转换为二叉树
struct TreeNode* forestToTwoTree(struct ForestNode* forest) {
	if (forest == NULL) {
		return NULL;
	}
	
	// 将第一个树转换为二叉树
	struct TreeNode* TwoTree = (struct TreeNode*)malloc(sizeof(struct TreeNode));
	TwoTree->data = forest->data;
	TwoTree->left = NULL;
	TwoTree->right = NULL;
	
	TwoTree->left = forestToTwoTree(forest->child);
	TwoTree->right = forestToTwoTree(forest->nextbrother);
	
	return TwoTree;
}

// 函数:将二叉树转换为森林
struct ForestNode* TwoTreeToForest(struct TreeNode* TwoTree) {
	if (TwoTree == NULL) {
		return NULL;
	}
	
	// 将根结点转换为森林中的一个树结点
	struct ForestNode* tree = (struct ForestNode*)malloc(sizeof(struct ForestNode));
	tree->data = TwoTree->data;
	tree->nextbrother = NULL;
	tree->child = NULL;
	
	// 递归转换左子树
	tree->child = TwoTreeToForest(TwoTree->left);
	
	// 递归转换右子树,并将其作为兄弟结点
	tree->nextbrother = TwoTreeToForest(TwoTree->right);
	
	return tree;
}

// 函数:显示二叉树的中序遍历结果
void displayTwoTree(struct TreeNode* root) {
	if (root != NULL) {
		displayTwoTree(root->left);
		printf("中序遍历结果:%d ", root->data);
		displayTwoTree(root->right);
	}
}

// 函数:释放二叉树内存
void freeTwoTree(struct TreeNode* root) {
	if (root != NULL) {
		freeTwoTree(root->left);
		freeTwoTree(root->right);
		free(root);
	}
}

// 函数:释放森林内存
void freeForest(struct ForestNode* forest) {
	if (forest != NULL) {
		freeForest(forest->nextbrother);
		freeForest(forest->child);
		free(forest);
	}
}

int main() {
	int choice;
	printf("选择操作:\n");
	printf("1. 将森林转换为二叉树\n");
	printf("2. 将二叉树转换为森林\n");
	scanf("%d", &choice);
	
	if (choice == 1) {
		// 输入森林数据
		printf("请输入森林的根结点数据:");
		int data;
		scanf("%d", &data);
		
		struct ForestNode* forest = (struct ForestNode*)malloc(sizeof(struct ForestNode));
		forest->data = data;
		forest->nextbrother = NULL;
		forest->child = NULL;
		
		struct ForestNode* current = forest;
		
		while (1) {
			printf("是否添加兄弟结点(1/0):");
			int addbrother;
			scanf("%d", &addbrother);
			
			if (addbrother == 0) {
				break;
			}
			
			printf("请输入兄弟结点的数据:");
			int siblingData;
			scanf("%d", &siblingData);
			
			struct ForestNode* sibling = (struct ForestNode*)malloc(sizeof(struct ForestNode));
			sibling->data = siblingData;
			sibling->nextbrother = NULL;
			sibling->child = NULL;
			
			current->nextbrother = sibling;
			current = sibling;
		}
		
		// 执行森林转换为二叉树
		struct TreeNode* TwoTree = forestToTwoTree(forest);
		
		// 显示二叉树
		printf("二叉树:");
		displayTwoTree(TwoTree);
		printf("\n");
		
		// 释放内存
		freeTwoTree(TwoTree);
		freeForest(forest);
	} else if (choice == 2) {
		// 输入二叉树数据
		printf("请输入二叉树的根结点数据:");
		int data;
		scanf("%d", &data);
		
		struct TreeNode* TwoTree = (struct TreeNode*)malloc(sizeof(struct TreeNode));
		TwoTree->data = data;
		TwoTree->left = NULL;
		TwoTree->right = NULL;
		
		// 输入左子树
		printf("是否有左子树(1/0):");
		int hasLeftSubtree;
		scanf("%d", &hasLeftSubtree);
		
		if (hasLeftSubtree) {
			printf("请输入左子树的根结点数据:");
			int leftData;
			scanf("%d", &leftData);
			
			TwoTree->left = (struct TreeNode*)malloc(sizeof(struct TreeNode));
			TwoTree->left->data = leftData;
			TwoTree->left->left = NULL;
			TwoTree->left->right = NULL;
		}
		
		// 输入右子树
		printf("是否有右子树(1/0):");
		int hasRightSubtree;
		scanf("%d", &hasRightSubtree);
		
		if (hasRightSubtree) {
			printf("请输入右子树的根结点数据:");
			int rightData;
			scanf("%d", &rightData);
			
			TwoTree->right = (struct TreeNode*)malloc(sizeof(struct TreeNode));
			TwoTree->right->data = rightData;
			TwoTree->right->left = NULL;
			TwoTree->right->right = NULL;
		}
		
		// 执行二叉树转换为森林
		struct ForestNode* forest = TwoTreeToForest(TwoTree);
		
		// 显示森林
		printf("森林:\n");
		struct ForestNode* current = forest;
		while (current != NULL) {
			printf("%d -> ", current->data);
			current = current->nextbrother;
		}
		printf("NULL\n");
		
		// 释放内存
		freeTwoTree(TwoTree);
		freeForest(forest);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_62088638/article/details/134214511