您的位置:首页 > 其它

二叉树的创建与遍历(递归)

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");

}

程序中二叉树的结构:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: