二叉树的创建与遍历(递归)
来源:互联网 发布:弈乐麻将 游戏网络 编辑:程序博客网 时间:2024/06/14 11:08
我总结了二叉树的创建方法,有以下几个:由前序遍历创建二叉树、直接创建二叉树、用前序和中序创建二叉树、用中序和后序创建二叉树
当然 还有很多。
#include "BinaryTree.h"
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef char ELEM_TYPE;
typedef struct BtNode
{
BtNode *LeftChild;
BtNode *RightChild;
ELEM_TYPE data;
}BtNode, *BtTree;
void Init_BtTree(BtNode *ptr);//初始化二叉树
BtNode * BuyNode();//创建一个节点
BtNode * Creat_BtTree1();//由前序遍历创建二叉树(递归)
BtNode * Creat_BtTree2(char *&str);//直接创建二叉树
BtNode * Creat_BtTree3(char ** const str);//直接创建二叉树
//由于前序和后序无法确定一个唯一的二叉树,所以不使用前序和后序来创建二叉树
BtNode * Creat_BtTreeIL(char *is, char *ls, int n);//用中序和后序创建二叉树
BtNode * CreatIL(char *is, char *ls, int n);//用中序和后序创建二叉树
BtNode * Creat_BtTreePI(char *ps, char *is, int n);//用前序和中序创建二叉树
BtNode * CreatPI(char *ps, char *is, int n);//用前序和中序创建二叉树
int FindIs(char *is, int n, char ch);
void PerOrder(BtNode *ptr);//前序遍历(递归)
void InOrder(BtNode *ptr);//中序遍历(递归)
void LastOrder(BtNode *ptr); //后序遍历(递归)
void Init_BtTree(BtNode *ptr)//初始化二叉树
{
ptr = NULL;
}
BtNode * BuyNode()//创建一个节点
{
BtNode *s = NULL;
s = (BtNode *)malloc(sizeof(BtNode));
if (NULL == s)
exit(1);
memset(s, 0, sizeof(BtNode));
return s;
}
BtNode * Creat_BtTree1()//由前序遍历创建二叉树
{
BtNode *p = NULL;
char ch;
scanf("%c", &ch);
if (ch != '#')
{
p = BuyNode();
p->data = ch;
p->LeftChild = Creat_BtTree1();
p->RightChild = Creat_BtTree1();
}
return p;
}
BtNode * Creat_BtTree2(char *&str)//直接创建二叉树
{
BtNode *ptr = NULL;
if (str != NULL && *str != '#')
{
ptr = BuyNode();
ptr->data = *str;
ptr->LeftChild = Creat_BtTree2(++str);
ptr->RightChild = Creat_BtTree2(++str);
}
return ptr;
}
BtNode * Creat_BtTree3(char ** const str)//直接创建二叉树
{
BtNode *ptr = NULL;
if (*str != NULL && **str != '#' && str != NULL)
{
ptr = BuyNode();
ptr->data = **str;
ptr->LeftChild = Creat_BtTree3(&++*str);
ptr->RightChild = Creat_BtTree3(&++*str);
}
return ptr;
}
BtNode * Creat_BtTreeIL(char *is, char *ls, int n)//用中序和后序创建二叉树
{
if (NULL == is || NULL == ls || n < 1)
{
return NULL;
}
else
{
return CreatIL(is, ls, n);
}
}
BtNode * CreatIL(char *is, char *ls, int n)//用中序和后序创建二叉树
{
BtNode *s = NULL;
if (n > 0)
{
int pos = FindIs(is, n, ls[n - 1]);
if (pos == -1) exit(1);
s = BuyNode();
s->data = ls[n - 1];
s->LeftChild = CreatIL(is, ls, pos);
s->RightChild = CreatIL(is+pos+1, ls+pos, n-pos-1);
}
return s;
}
int FindIs(char *is, int n, char ch)
{
for (int i = 0; i < n; ++i)
{
if (is[i] == ch)
{
return i;
}
}
return -1;
}
BtNode * Creat_BtTreePI(char *ps, char *is, int n)//用前序和中序创建二叉树
{
if (NULL == ps || NULL == is || n < 1)
{
return NULL;
}
else
{
return CreatPI(ps, is, n);
}
}
BtNode * CreatPI(char *ps, char *is, int n)//用前序和中序创建二叉树
{
BtNode *s = NULL;
if (n > 0)
{
int pos = FindIs(is, n, ps[0]);
if (pos == -1) exit(1);
s = BuyNode();
s->data = ps[0];
s->LeftChild = CreatPI(ps + 1, is, pos);
s->RightChild = CreatPI(ps + pos + 1, is + pos + 1, n - pos - 1);
}
return s;
}
void PerOrder(BtNode *ptr)//前序遍历(递归)
{
{
if (ptr != NULL)
{
printf("%c-", ptr->data);
PerOrder(ptr->LeftChild);
PerOrder(ptr->RightChild);
}
}
}
void InOrder(BtNode *ptr)//中序遍历(递归)
{
if (ptr != NULL)
{
InOrder(ptr->LeftChild);
printf("%c-", ptr->data);
InOrder(ptr->RightChild);
}
}
void LastOrder(BtNode *ptr)//后序遍历(递归)
{
{
if (ptr != NULL)
{
LastOrder(ptr->LeftChild);
LastOrder(ptr->RightChild);
printf("%c-", ptr->data);
}
}
}
void main()
{
BtNode *root = NULL;
Init_BtTree(root);
//root = Creat_BtTree1();
/*char *str = "ABC##DE##F##G#H##";
root = Creat_BtTree2(str);
root = Creat_BtTree3(&str);*/
char *ps = "ABCDEFGH";
char *is = "CBEDFAGH";
char *ls = "CEFDBHGA";
int n = strlen(is);
//root = Creat_BtTreeIL(is, ls, n);
root = Creat_BtTreePI(is, ls, n);
PerOrder(root);
printf("\n");
InOrder(root);
printf("\n");
LastOrder(root);
printf("\n");
}
程序中二叉树的结构:
- 二叉树的创建与遍历(递归)
- 二叉树的创建与递归遍历
- 二叉树的创建与遍历(递归版本)
- 二叉树的创建与遍历(递归)
- 二叉树的创建与遍历(递归)
- 二叉树的创建遍历-递归与非递归
- 二叉树的创建与前中后序遍历递归非递归
- 二叉树的创建和递归与非递归遍历
- 二叉树的创建与遍历(递归创建与遍历)
- 二叉树的常用操作(创建、先中后序递归与非递归遍历、层序遍历)
- 二叉树的创建--(3)遍历二叉树(递归与非递归、Level)
- 二叉树的创建与前序中序后序遍历的递归实现
- 数据结构与算法学习记录--二叉树的创建,递归遍历,非递归遍历的实现
- 二叉树的创建,递归遍历,非递归遍历
- 二叉树的创建,递归遍历以及非递归遍历
- 【数据结构与算法】二叉树的遍历(递归遍历、非递归遍历、层序遍历)
- 二叉树的遍历(递归与非递归)
- 二叉树的遍历算法(递归与非递归)
- QGraphicsItem的缩放
- 9.20...
- 知识点复习
- Spring AOP 之 通知、连接点、切点、切面
- HDU 4273(计算几何+凸包重心)
- 二叉树的创建与遍历(递归)
- 第四周训练总结(一)
- “挖坑填坑”理解快速排序
- Oracle数据库入门 基础知识day05 火推阳光笔记
- 2017-09-21 LeetCode_031 Next Permutation
- Android Crash日志收集
- LWC 50:677. Map Sum Pairs
- 47. 数据结构笔记之四十七的有向无环图的应用排序
- 携程2018 秋招编程题