实现二叉树的完整创建、二叉树的先序、中序、后序遍历 代码 C语言

#include <stdio.h>
#include <malloc.h>     

/*
* 先中后序遍历 都由递归来完成,
递归的原理是栈的调用
(可手算推导过程,因为程序写起来很简单,所以更多的还是要理解和熟悉)
*/


/*struct ElemType//用来存放自定义数据的结构体
{
	int value;
};*/

typedef struct BitNode
{
	int data;
	struct BitNode* lchild;
	struct BitNode* rchild;
}BitNode, * BiTree;

//初始化根结点
BiTree Init_BiTree(BiTree& root, int e)
{
	
	root->data =e;
	
	root->lchild = NULL;
	root->rchild = NULL;
	printf("\n以%d为根节点的二叉树初始化根节点成功!", e);

	return root;
}

//给二叉树中的某个结点插入左孩子
BitNode* insert_BitNode_lchild(BitNode*& Tnode)
{
	int e;
	int flag = 1;//该插入的左孩子默认为NULL
	BitNode* child = (BitNode*)malloc(sizeof(BitNode));
	//child = NULL;//该插入的左孩子默认为NULL
	printf("\n若该结点 %d 左孩子为NULL--1,若该结点 %d 左孩子为准确的结点(有意义)--2:",Tnode->data, Tnode->data);
	scanf_s("%d", &flag);

	if (flag == 2)
	{
		printf("\n请输入该结点的值(int):");
		scanf_s("%d", &e);

		//初始化新增结点

		child->data = e;
		child->lchild = NULL;
		child->rchild = NULL;

		printf("\n新增一个(左)结点:%d", child->data);

	}
	else
	{
		child = NULL;
	}

	//将新增的结点child与二叉树中的一个结点Tnode 链接起来
	Tnode->lchild = child;


	return child;

}

//给二叉树中的某个结点插入右孩子
BitNode* insert_BitNode_rchild(BitNode*& Tnode)
{
	int e;
	int flag = 1;//该插入的右孩子默认为NULL
	BitNode* child = (BitNode*)malloc(sizeof(BitNode));
	//child = NULL;//该插入的左孩子默认为NULL
	printf("\n若该结点 %d 右孩子为NULL--1,若该结点 %d 右孩子为准确的结点(有意义)--2:", Tnode->data, Tnode->data);
	scanf_s("%d", &flag);

	if (flag == 2)
	{
		printf("\n请输入该结点的值(int):");
		scanf_s("%d", &e);

		//初始化新增结点

		child->data = e;
		child->lchild = NULL;
		child->rchild = NULL;

		printf("\n新增一个(左)结点:%d", child->data);

	}
	else
	{
		child = NULL;
	}

	//将新增的结点child与二叉树中的一个结点Tnode 链接起来
	Tnode->rchild = child;


	return child;

}





//输出嘛
void visit(BitNode* node)
{
	printf(" %d ",node->data);
}


//先序遍历二叉树(根左右)

void Pre_ER(BiTree T)
{
	if (T!=NULL)
	{
		visit(T);//根
		Pre_ER(T->lchild);//左
		Pre_ER(T->rchild);//右
	}
}

//中序遍历二叉树(左根右)

void In_ER(BiTree T)
{
	if (T!=NULL)
	{
		In_ER(T->lchild);
		visit(T);
		In_ER(T->rchild);
	}
}

//后序遍历二叉树(左右根)

void Post_ER(BiTree T)
{
	if (T != NULL)
	{
		Post_ER(T->lchild);
		Post_ER(T->rchild);
		visit(T);
	}
	
}



//递归法实现二叉树的递归插入(创造出一个初始的完整的二叉树)-- 已手算验证过,嘿嘿嘿 ^>^
BitNode*  insert_node(BitNode* N)
{
	if (N != NULL)//如果该结点不是空
	{
		insert_BitNode_lchild(N);//为该节点添加左孩子
		insert_node(N->lchild);//为该节点的左孩子添加左孩子或右孩子

		insert_BitNode_rchild(N);//为该节点添加右孩子
		insert_node(N->rchild);//为该节点的右孩子添加左孩子或右孩子
	}


	return N;
}





void test()
{
	int e;
	BiTree root=(BitNode* )malloc(sizeof(BitNode));
	printf("\n\n开始创造一个二叉树:\n\n请输入树根结点的值(int):");
	scanf_s("%d", &e);
	Init_BiTree(root, e);//二叉树树根,一颗二叉树的唯一标识
	printf("\n根节点的值:%d",root->data);
	
	root = insert_node(root);//二叉树的枝叶
	printf("\n先序遍历:\n");
	Pre_ER(root);
	printf("\n中序遍历:\n");
	In_ER(root);
	printf("\n后序遍历:\n");
	Post_ER(root);
	printf("\n\n先序中序后序遍历完成!!\n");
}


int main()
{
	test();
	return 0;
}

重点是要理解递归是怎么利用栈来实现递归实现的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言建立二叉树实现先序中序后序遍历代码: ```c #include <stdio.h> #include <stdlib.h> //定义二叉树节点结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; //创建二叉树 TreeNode* createTree() { TreeNode *root = NULL; int val; scanf("%d", &val); if (val != -1) { //输入-1表示该节点为空 root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); } return root; } //先序遍历 void preOrder(TreeNode *root) { if (root != NULL) { printf("%d ", root->val); preOrder(root->left); preOrder(root->right); } } //中序遍历 void inOrder(TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->val); inOrder(root->right); } } //后序遍历 void postOrder(TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->val); } } int main() { printf("请输入二叉树先序遍历,-1表示该节点为空:\n"); TreeNode *root = createTree(); printf("二叉树先序遍历为:"); preOrder(root); printf("\n"); printf("二叉树中序遍历为:"); inOrder(root); printf("\n"); printf("二叉树后序遍历为:"); postOrder(root); printf("\n"); return 0; } ``` 输入格式: 先序遍历的每个节点值,-1表示该节点为空。 输出格式: 二叉树先序中序后序遍历结果。 示例: 输入: ``` 1 2 -1 -1 3 4 -1 -1 5 -1 -1 ``` 输出: ``` 二叉树先序遍历为:1 2 3 4 5 二叉树中序遍历为:2 1 4 3 5 二叉树后序遍历为:2 4 5 3 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值