您的位置:首页 > 其它

二叉树根据先序,中序创建二叉树,后序,中序创建二叉树,层次序列,中序遍历创建二叉树。

2017-04-02 14:35 393 查看
在二叉树的创建过程中,先序,后序,层次序列这三种可以确定根的位置,只有中序序列可以确定左右子树的位置。所以我们可以通过(先序,中序),(后序,中序),(层次序列,中序)这三种情况创建一颗确定的二叉树。相关代码如下。

#include <iostream>
#include <queue>
#include <deque>
using namespace std;
typedef char ElementType;
struct Node
{
ElementType data;
Node* lchild;
Node* rchild;
};
//创建一个新节点
Node* newNode(ElementType v)
{
Node* node = new Node;
node->data = v;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
//搜索值为x,并将其修改为newdata
void search_tree(Node* root, int x, int newdata)
{
if (root = NULL)
{
return;
}
if (root->data = x)
{
root->data = newdata;
}
search_tree(root->lchild, x, newdata);
search_tree(root->rchild, x, newdata);
}
//插入节点
void insert(Node* &root, int x)
{
if (root == NULL)
{
root = new Node;
root->data = x;
root->lchild = NULL;
root->rchild = NULL;
return;
}
if(x<root->data)
insert(root->lchild, x);
else
insert(root->rchild, x);

}
//创建
Node* create(int data[], int n)
{
Node* root=NULL;
for (int i = 0;i < n;i++)
{
insert(root, data[i]);
}
return root;
}

//根据先序和中序创建一个二叉树
void buildBinaryTreeInPreAndIn(Node*&root,int preL, int preR,char preOrder[],int inL, int inR,char inOrder[])
{
if (preL> preR)
return;
int rootidx;
for (int i = inL;i <= inR;i++)
if (inOrder[i] == preOrder[preL])
{
rootidx = i;
break;
}
root = newNode(preOrder[preL]);
int numL = rootidx - inL;
buildBinaryTreeInPreAndIn(root->lchild, preL + 1, preL + numL, preOrder, inL, rootidx - 1, inOrder);
buildBinaryTreeInPreAndIn(root->rchild, preL + numL + 1, preR, preOrder, rootidx + 1, inR, inOrder);
}
//根据后续和中序创建一个二叉树
void BuildBinaryTreeInPostAndIn(Node*&root, int postL, int postR, char postOrder[], int inL, int inR, char inOrder[])
{
if (postL > postR)
return;
int rootidx;
for (int i = inL;i <= inR;i++)
{
if (inOrder[i] == postOrder[postR])
{
rootidx = i;
break;
}
}
root = newNode(postOrder[postR]);
int numL = rootidx - inL;
BuildBinaryTreeInPostAndIn(root->lchild, postL, postL + numL - 1, postOrder, inL,  rootidx - 1, inOrder);
BuildBinaryTreeInPostAndIn(root->rchild, postL + numL, postR - 1, postOrder, rootidx + 1, inR, inOrder);
}
//根据层次遍历和中序遍历生成一棵二叉树
void BuildBinaryTreeInLevelAndIn(Node*&root, deque<ElementType>queLevel, int inL, int inR, char inOrder[])
{
if (queLevel.empty()) return;
int rootidx;
for (int i = inL;i <= inR;i++)
{
if (inOrder[i] == queLevel.front())
{
rootidx = i;
break;
}
}
root = newNode(queLevel.front());
queLevel.pop_front();//删掉头结点
//这里我们要将层次序列拆成两个序列,根据中序序列的左右子树的位置来确定。
deque<ElementType> level1;
deque<ElementType> level2;
while (!queLevel.empty())
{
char data = queLevel.front();
bool flag = true;
for (int i = inL;i < rootidx;i++)
{
if (data == inOrder[i])
{
level1.push_back(queLevel.front());
queLevel.pop_front();
flag = false;
break;
}
}
if (flag)
{
level2.push_back(queLevel.front());
queLevel.pop_front();
}
}
BuildBinaryTreeInLevelAndIn(root->lchild, level1, inL, rootidx - 1, inOrder);
BuildBinaryTreeInLevelAndIn(root->rchild, level2, rootidx + 1, inR, inOrder);
}

//先序遍历
void preOrder(Node*root)
{
if (root == NULL)
return;
cout << root->data << endl;
preOrder(root->lchild);
preOrder(root->rchild);
}
//中序遍历
void inOrder(Node*root)
{
if (root == NULL)
return;
inOrder(root->lchild);
cout << root->data << endl;
inOrder(root->rchild);
}
//后续遍历
void postOrder(Node*root)
{
if (root == NULL)
return;
postOrder(root->lchild);
postOrder(root->rchild);
cout << root->data << endl;
}
//层次遍历
void levelOrder(Node*root)
{
queue<Node*>que;
que.push(root);
while (!que.empty())
{
Node* top = que.front();
que.pop();
cout << top->data << endl;
if (top->lchild)
que.push(top->lchild);
if (top->rchild)
que.push(top->rchild);
}
}

void main()
{
char preOrders[] = {'x','A','B','D','E',
b18e
'C','F'};
char inOrders[] = {'x', 'D','B','E','A','C','F' };
char postOrders[] = { 'x', 'D','E','B','F','C','A' };
char levelOrders[] = { 'x','A','B','C','D','E','F' };
deque<ElementType> quelevel;
for (int i = 1;i <= 6;i++)
quelevel.push_back(levelOrders[i]);

Node* root = NULL;
//buildBinaryTreeInPreAndIn(root, 1, 6, preOrders, 1, 6, inOrders);
//  BuildBinaryTreeInPostAndIn(root, 1, 6, postOrders, 1, 6, inOrders);
BuildBinaryTreeInLevelAndIn(root, quelevel, 1, 6, inOrders);
cout << "先序遍历" << endl;
//先序遍历
preOrder(root);
//中序遍历
cout << "中序遍历" << endl;
inOrder(root);
//后续遍历
cout << "后续遍历" << endl;
postOrder(root);
//层次遍历
cout << "层次遍历" << endl;
levelOrder(root);

system("pause");
}


程序运行截图如下。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  遍历 二叉树 创建
相关文章推荐