二叉树根据先序,中序创建二叉树,后序,中序创建二叉树,层次序列,中序遍历创建二叉树。
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"); }
程序运行截图如下。
相关文章推荐
- 根据前序和中序创建二叉树及二叉树的前序、中序、后序、层次打印
- 根据二叉树的前序遍历序列和中序遍历序列求二叉树的后序遍历序列
- 根据前序遍历序列和中序遍历序列创建二叉树
- 转载-根据先序序列和中序序列创建二叉树
- 根据二叉树的中序遍历和后序遍历创建 一棵二叉树,并且返回所创建的树的树高,具体方法请参见下面源码(经过调试,无任何bug存在)
- 二叉树创建、前序遍历、中序遍历、后序遍历 的 递归与非递归实现 以及 层次遍历
- 根据先序序列和中序序列创建二叉树
- 根据中序和层次遍历序列,构造二叉树
- 二叉树序列---根据后序和中序系列或前序和中序序列构造二叉树
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 根据后序中序序列构建二叉树,按层次打印
- 二叉树的前中后层次遍历(递归+非递归)、创建树(数组、前序+中序、中序加后序)
- 采用二叉链表结构实现二叉树,并以递归遍历思想实现二叉树的创建、二叉树的遍历(先序、中序、后序和层次遍历)
- 二叉树:根据二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 根据中序遍历和先序遍历,后序遍历创建二叉树。
- 二叉树的创建、二叉排序树、前序、中序、后序、层次遍历
- 算法实验-二叉树的创建和前序-中序-后序-层次 遍历
- 算法实验-二叉树的创建和前序-中序-后序-层次 遍历
- 二叉树的创建、前序遍历、中序遍历、后序遍历
- 二叉树的创建、前序遍历、中序遍历、后序遍历