一、 已知二叉树的先序和中序数列,创建二叉树
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);
}