数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
2013-09-30 12:46
483 查看
// 二叉树.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #define maxSize 10 using namespace std; typedef struct BinaryTreeNode { char data; BinaryTreeNode * leftChild; BinaryTreeNode * rightChild; }Node; //构造二叉树 使用先序和中序构造一颗二叉树 void MakeBinaryTree(Node** root, char* preOrder, char* midOrder, int length) { if (length == 0) { (*root) = NULL; return; } (*root) = new Node; (*root)->data = *preOrder; char * rootplace = strchr(midOrder, (*root)->data); if (rootplace == NULL) { cout <<"input wrong order sample!"<<endl; } int leftTreeLength = strlen(midOrder) - strlen(rootplace); int rightTreeLength = length - leftTreeLength - 1; MakeBinaryTree(&(*root)->leftChild, preOrder+1, midOrder, leftTreeLength); MakeBinaryTree(&(*root)->rightChild, preOrder+leftTreeLength+1, rootplace+1, rightTreeLength); } void PostTraverse(Node* root) { if (root == NULL) return; PostTraverse(root->leftChild); PostTraverse(root->rightChild); cout << root->data; } void visit(Node *p) { printf("%c ",p->data); } //先序遍历 void preOrder(Node *p) { if(p==NULL) return; visit(p); preOrder(p->leftChild); preOrder(p->rightChild); } //中序遍历 void inOrder(Node *p) { if(p==NULL) return; inOrder(p->leftChild); visit(p); inOrder(p->rightChild); } //后序遍历 void postOrder(Node *p) { if(p==NULL) return; postOrder(p->leftChild); postOrder(p->rightChild); visit(p); } //层次遍历 typedef struct { Node *data[maxSize]; int front; int rear; }SqQueue; void level(Node *&p) { Node *q; SqQueue qu; qu.front=qu.rear=0; qu.rear=(qu.rear+1)%maxSize; qu.data[qu.rear]=p;//进队 while(qu.front!=qu.rear) { qu.front=(qu.front+1)%maxSize; q=qu.data[qu.front]; //出队 visit(q); if(q->leftChild!=NULL) { qu.rear=(qu.rear+1)%maxSize; qu.data[qu.rear]=q->leftChild;//左孩子进队 } if(q->rightChild!=NULL) { qu.rear=(qu.rear+1)%maxSize; qu.data[qu.rear]=q->rightChild;//右孩子进队 } } } int _tmain(int argc, _TCHAR* argv[]) { char pre[] = "abdeijcfg"; char mid[] = "dbiejafcg"; //"bdeijafcg" "dijebfgca" Node* r; MakeBinaryTree(&r, pre, mid, strlen(pre));//构造了一颗二叉树 printf("先序遍历:"); preOrder(r); printf("\n中序遍历:"); inOrder(r); printf("\n后序遍历:"); postOrder(r); printf("\n层次遍历:"); level(r); return 0; }
相关文章推荐
- [置顶] 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(一)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构 — 实现二叉树的前序,中序,后序遍历(非递归)
- [置顶] 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 采用二叉链表结构实现二叉树,并以递归遍历思想实现二叉树的创建、二叉树的遍历(先序、中序、后序和层次遍历)
- (编程训练)再回首,数据结构——二叉树的前序、中序、后序遍历(递归)
- 数据结构——树(4):二叉树前序,中序,后序遍历互求
- 【数据结构与算法】(六) c 语言实现简单的二叉树静态创建及先序、中序、后序遍历
- (编程训练)再回首,数据结构——二叉树的前序、中序、后序遍历(非递归)
- 数据结构 分别用递归和非递归方法实现二叉树先序,中序,后序遍历
- 数据结构基础 后序遍历和中序遍历还原二叉树