二叉树的创建与遍历(递归)
2017-09-21 21:26
351 查看
我总结了二叉树的创建方法,有以下几个:由前序遍历创建二叉树、直接创建二叉树、用前序和中序创建二叉树、用中序和后序创建二叉树
当然 还有很多。
#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");
}
程序中二叉树的结构:
当然 还有很多。
#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");
}
程序中二叉树的结构:
相关文章推荐
- 二叉树的递归创建&遍历
- 二叉树的递归创建,以及二叉查找树查找的建立 和遍历查找的比较
- 二叉树的创建及递归和非递归遍历
- 创建、前序、中序、后序递归遍历二叉树
- 二叉树的创建和递归遍历和非递归遍历
- 二叉树的创建,遍历(前序,中序,后序)-递归 非递归
- 二叉树的创建和先序,中序,后序,递归,非递归遍历
- 不疯魔,不成活!——二叉树的创建、遍历(递归实现)等操作。
- 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度
- 二叉树创建及遍历(递归遍历)
- 链式二叉树的后序创建、递归后序遍历、非递归堆栈后序遍历、后序销毁
- 二叉树(一):二叉树的创建以及三种遍历方法的递归实现
- 二叉树的递归创建以及前序,中序,后序遍历
- 二叉树创建及遍历算法(递归及非递归)
- 数据结构--树和二叉树--二叉树的创建和遍历(递归和非递归方式)
- 二叉树的创建与其递归遍历
- 二叉树的创建、相关操作、递归和非递归式实现三种遍历
- 数据结构-----创建/递归非递归遍历二叉树
- 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度
- 二叉树创建及遍历算法(递归及非递归)