数据结构与算法之二叉树的建立

一、 已知二叉树的先序和中序数列,创建二叉树

1)算法思想

在这里插入图片描述

2)代码实现

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

typedef char Elemtype;
typedef struct BtNode
{
	struct BtNode* leftchild;
	struct BtNode* rightchild;
	Elemtype data;
}BtNode,* BinaryTree;


//中序遍历
void InOder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		InOder(ptr->leftchild);
		printf("%c ", ptr->data);
		InOder(ptr->rightchild);
	}
}

//先序遍历
void PreOder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		printf("%c ", ptr->data);
		PreOder(ptr->leftchild);
		PreOder(ptr->rightchild);
	}
}

//后序遍历
void PastOder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		PastOder(ptr->leftchild);
		PastOder(ptr->rightchild);
		printf("%c ", ptr->data);
	}
}

//初始化节点
BtNode* Buynode()
{
	BtNode* s = (BtNode*)malloc(sizeof(BtNode));
	if (nullptr == s) exit(1);
	memset(s, 0, sizeof(BtNode));
	return s;
}

int FindPos(const char* istr, int n, char ch)
{
	int pos = -1;
	for (int i = 0; i < n; i++)
	{
		if (istr[i] == ch)
		{
			pos = i;
			break;
		}
	}
	return pos;
}

BtNode* CreatBTreePI(const char* pstr, const char* istr,int n)
{
	BtNode* s = nullptr;
	if (n > 0)
	{
		s = Buynode();
		s->data = pstr[0];
		int pos = FindPos(istr, n, pstr[0]);
		if (pos == -1)exit(1);
		s->leftchild = CreatBTreePI(pstr+1,istr,pos);
		s->rightchild = CreatBTreePI(pstr+pos+1,istr+pos+1,n-pos-1);
	}
	return s;
}

BtNode* CreatBinaryTreePI(const char* pstr, const char* istr)
{
	int n = strlen(pstr);
	int m = strlen(istr);
	if (nullptr == pstr || nullptr == istr || n < 1 || m < 1 || n != m)return nullptr;
	else return CreatBTreePI(pstr, istr, n);  //这里n代表的规模


}

int main()
{
	const char* pstr = "ABCDEFGH"; //先序数列
	const char* istr = "CBEDFAGH"; //中序数列
	BinaryTree root = CreatBinaryTreePI(pstr, istr); //通过先序数列和中序数列创建二叉树
	PreOder(root);
	printf("\n");
	InOder(root);
	printf("\n");
	PastOder(root);
	printf("\n");
}

二、已知二叉树的先序和后序数列,创建二叉树

1)算法思想

在这里插入图片描述

2)代码实现

核心代码

int FindPos(const char* istr, int n, char ch)
{
	int pos = -1;
	for (int i = 0; i < n; i++)
	{
		if (istr[i] == ch)
		{
			pos = i;
			break;
		}
	}
	return pos;
}

BtNode* CreatBTreeIL(const char* istr, const char* lstr, int n)
{
	BtNode* s = nullptr;
	if (n > 0)
	{
		s = Buynode();
		s->data = lstr[n-1];
		int pos = FindPos(istr, n, lstr[n-1]);
		if (pos == -1)exit(1);
		s->leftchild = CreatBTreeIL(istr,lstr,pos);
		s->rightchild = CreatBTreeIL(istr+pos+1,lstr+pos,n-pos-1);
	}
	return s;
}

BtNode* CreatBinaryTreeIL(const char* istr, const char* lstr)
{
	int n = strlen(istr);
	int m = strlen(lstr);
	if (nullptr == istr || nullptr == lstr || n < 1 || m < 1 || n != m)return nullptr;
	else return CreatBTreeIL(istr, lstr, n);  //这里n代表的规模

}

三、二叉树的顺序存放(打印先、中、后序遍历)

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

void InOrder_Ar(const int* arr, int i,int n)
{ 
	if (i<n&&arr[i]!=-1)
	{
		InOrder_Ar(arr, i*2+1, n);
		printf("%d ", arr[i]);
		InOrder_Ar(arr, i * 2 + 2, n);
	}
}

void PreOrder_Ar(const int* arr, int i, int n)
{
	if (i < n && arr[i] != -1)
	{
		printf("%d ", arr[i]);
		PreOrder_Ar(arr, i * 2 + 1, n);
		PreOrder_Ar(arr, i * 2 + 2, n);
	}
}

void PastOrder_Ar(const int* arr, int i, int n)
{
	if (i < n && arr[i] != -1)
	{
		PastOrder_Ar(arr, i * 2 + 1, n);
		PastOrder_Ar(arr, i * 2 + 2, n);
		printf("%d ", arr[i]);
	}
}

int main()
{
	int arr[] = { 31,23,12,66,-1,5,17,70,62,-1,-1,-1,88,-1,55 };
	int n = sizeof(arr) / sizeof(arr[0]);
	InOrder_Ar(arr,0, n);
	printf("\n");
	PastOrder_Ar(arr, 0, n);
	printf("\n");
	PreOrder_Ar(arr, 0, n);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

淡蓝色的经典

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

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

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

打赏作者

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

抵扣说明:

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

余额充值