标签:min 二叉树 epo 构造 word admin typedef nts scanf
【简介】
树形结构是一类重要的非线性数据结构,其中以树和二叉树最为常用。
二叉树是每个结点最多有两个子树的有序树。通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用作二叉查找树和二叉堆或是二叉排序树。二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。二叉树的第i层至多有2的 i -1次方个结点;深度为k的二叉树至多有2^(k) -1个结点;对任何一棵二叉树T,如果其终端结点数(即叶子结点数)为n0,度为2的结点数为n2,则n0 = n2 + 1。
二叉树的链式存储结构是一类重要的数据结构,其形式定义如下:
- typedef struct BiTNode{
-
- char data;
-
- struct BiTNode *lchild,*rchild;
- }BiTNode,*BiTree;
或者
- struct TreeNode{
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int x):val(x),left(nullptr),right(nullptr){}
- };
【二叉树的创建】
通过读入一个字符串,建立二叉树的算法如下:
- int CreateBiTree(BiTree &T){
- char data;
-
- scanf("%c",&data);
- if(data == ‘#‘){
- T = NULL;
- }
- else{
- T = (BiTree)malloc(sizeof(BiTNode));
-
- T->data = data;
-
- CreateBiTree(T->lchild);
-
- CreateBiTree(T->rchild);
- }
- return 0;
- }
或者
- void CreateTree(TreeNode* &root){
- int val;
-
- cin>>val;
-
- if(val == -1){
- root = nullptr;
- return;
- }
- root = new TreeNode(val);
-
- CreateTree(root->left);
-
- CreateTree(root->right);
- }
层次建立二叉树:
- struct TreeNode {
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- };
- TreeNode* CreateTreeByLevel(vector<char> num){
- int len = num.size();
- if(len == 0){
- return NULL;
- }
- queue<TreeNode*> queue;
- int index = 0;
-
- TreeNode *root = new TreeNode(num[index++]);
-
- queue.push(root);
- TreeNode *p = NULL;
- while(!queue.empty() && index < len){
-
- p = queue.front();
- queue.pop();
-
- if(index < len && num[index] != -1){
-
- TreeNode *leftNode = new TreeNode(num[index]);
- p->left = leftNode;
- queue.push(leftNode);
- }
- index++;
-
- if(index < len && num[index] != -1){
-
- TreeNode *rightNode = new TreeNode(num[index]);
- p->right = rightNode;
- queue.push(rightNode);
- }
- index++;
- }
- return root;
- }
-1代表NULL
创建如上二叉树输入:
15 11 20 8 14 -1 -1 -1 -1 13 -1
【二叉树的遍历】
遍历是对树的一种最基本的运算,所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。由于二叉树是非线性结构,因此,树的遍历实质上是将二叉树的各个结点转换成为一个线性序列来表示。
【递归算法】
- void Visit(BiTree T){
- if(T->data != ‘#‘){
- printf("%c ",T->data);
- }
- }
- void PreOrder(BiTree T){
- if(T != NULL){
-
- Visit(T);
-
- PreOrder(T->lchild);
-
- PreOrder(T->rchild);
- }
- }
- void InOrder(BiTree T){
- if(T != NULL){
-
- InOrder(T->lchild);
-
- Visit(T);
-
- InOrder(T->rchild);
- }
- }
- void PostOrder(BiTree T){
- if(T != NULL){
-
- PostOrder(T->lchild);
-
- PostOrder(T->rchild);
-
- Visit(T);
- }
- }
【非递归算法】
【先序遍历】
【思路】:访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
- void PreOrder2(BiTree T){
- stack<BiTree> stack;
-
- BiTree p = T;
-
- while(p || !stack.empty()){
- if(p != NULL){
-
- stack.push(p);
-
- printf("%c ",p->data);
-
- p = p->lchild;
- }
- else{
-
- p = stack.top();
- stack.pop();
-
- p = p->rchild;
- }
- }
- }
- void PreOrder(TreeNode* root){
- if(root == NULL){
- return;
- }
- stack<TreeNode*> stack;
- stack.push(root);
- TreeNode *p = NULL;
- while(!stack.empty()){
- p = stack.top();
- stack.pop();
- cout<<p->val<<endl;
-
- if(p->right){
- stack.push(p->right);
- }
-
- if(p->left){
- stack.push(p->left);
- }
- }
- }
【中序遍历】
【思路】:T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
- void InOrder2(BiTree T){
- stack<BiTree> stack;
-
- BiTree p = T;
-
- while(p || !stack.empty()){
- if(p != NULL){
-
- stack.push(p);
-
- p = p->lchild;
- }
- else{
-
- p = stack.top();
- printf("%c ",p->data);
- stack.pop();
-
- p = p->rchild;
- }
- }
- }
【后序遍历】
【思路】:T是要遍历树的根指针,后序遍历要求在遍历完左右子树后,再访问根。需要判断根结点的左右子树是否均遍历过。
- typedef struct BiTNodePost{
- BiTree biTree;
- char tag;
- }BiTNodePost,*BiTreePost;
-
- void PostOrder2(BiTree T){
- stack<BiTreePost> stack;
-
- BiTree p = T;
- BiTreePost BT;
-
- while(p != NULL || !stack.empty()){
-
- while(p != NULL){
- BT = (BiTreePost)malloc(sizeof(BiTNodePost));
- BT->biTree = p;
-
- BT->tag = ‘L‘;
- stack.push(BT);
- p = p->lchild;
- }
-
- while(!stack.empty() && (stack.top())->tag == ‘R‘){
- BT = stack.top();
-
- stack.pop();
- printf("%c ",BT->biTree->data);
- }
-
- if(!stack.empty()){
- BT = stack.top();
-
- BT->tag = ‘R‘;
- p = BT->biTree;
- p = p->rchild;
- }
- }
- }
或者
- vector<int> postorderTraversal(TreeNode *root) {
- vector<int> result;
- if(root == nullptr){
- return result;
- }
- stack<TreeNode*> s;
- s.push(root);
- TreeNode *node;
- while(!s.empty()){
- node = s.top();
- s.pop();
- result.insert(result.begin(),node->val);
-
- if(node->left){
- s.push(node->left);
- }
-
- if(node->right){
- s.push(node->right);
- }
- }
- return result;
- }
【层次遍历】
【思路】:按从顶向下,从左至右的顺序来逐层访问每个节点,层次遍历的过程中需要用队列。
- void LevelOrder(BiTree T){
- BiTree p = T;
-
- queue<BiTree> queue;
-
- queue.push(p);
-
- while(!queue.empty()){
-
- p = queue.front();
-
- printf("%c ",p->data);
-
- queue.pop();
-
- if(p->lchild != NULL){
- queue.push(p->lchild);
- }
-
- if(p->rchild != NULL){
- queue.push(p->rchild);
- }
- }
- }
【测试】
输入:(先序)
15 11 8 -1 -1 14 13 -1 -1 -1 20 -1 -1
输出:
代码一:
- #include <iostream>
- #include <vector>
- #include <stack>
- #include <queue>
- using namespace std;
-
- struct TreeNode{
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int x):val(x),left(nullptr),right(nullptr){}
- };
- void CreateTree(TreeNode* &root){
- int val;
-
- cin>>val;
-
- if(val == -1){
- root = nullptr;
- return;
- }
- root = new TreeNode(val);
-
- CreateTree(root->left);
-
- CreateTree(root->right);
- }
- void PreOrder(TreeNode* root,vector<int> &result){
- if(root == nullptr){
- return;
- }
- result.push_back(root->val);
-
- PreOrder(root->left,result);
-
- PreOrder(root->right,result);
- }
- void PreOrder2(TreeNode* root,vector<int> &result){
- if(root == nullptr){
- return;
- }
- stack<TreeNode*> s;
- s.push(root);
- TreeNode *node;
- while(!s.empty()){
- node = s.top();
- s.pop();
- result.push_back(node->val);
-
- if(node->right){
- s.push(node->right);
- }
-
- if(node->left){
- s.push(node->left);
- }
- }
- }
- void InOrder(TreeNode* root,vector<int> &result){
- if(root == nullptr){
- return;
- }
-
- InOrder(root->left,result);
- result.push_back(root->val);
-
- InOrder(root->right,result);
- }
- void InOrder2(TreeNode* root,vector<int> &result){
- if(root == nullptr){
- return;
- }
- stack<TreeNode*> s;
- TreeNode *node = root;
- while(node != nullptr || !s.empty()){
-
- if(node != nullptr){
- s.push(node);
- node = node->left;
- }
-
- else{
- node = s.top();
- s.pop();
- result.push_back(node->val);
- node = node->right;
- }
- }
- }
- void PostOrder(TreeNode* root,vector<int> &result){
- if(root == nullptr){
- return;
- }
-
- PostOrder(root->left,result);
-
- PostOrder(root->right,result);
- result.push_back(root->val);
- }
- void PostOrder2(TreeNode *root,vector<int> &result) {
- if(root == nullptr){
- return;
- }
- stack<TreeNode*> s;
- s.push(root);
- TreeNode *node;
- while(!s.empty()){
- node = s.top();
- s.pop();
- result.insert(result.begin(),node->val);
-
- if(node->left){
- s.push(node->left);
- }
-
- if(node->right){
- s.push(node->right);
- }
- }
- }
- void LevelOrder(TreeNode* root,vector<int> &result){
- if(root == nullptr){
- return;
- }
- queue<TreeNode*> queue;
- queue.push(root);
- TreeNode *node;
- while(!queue.empty()){
- node = queue.front();
- queue.pop();
- result.push_back(node->val);
-
- if(node->left){
- queue.push(node->left);
- }
-
- if(node->right){
- queue.push(node->right);
- }
- }
- }
- void Print(vector<int> result){
- int size = result.size();
- for(int i = 0;i < size;++i){
- cout<<result[i]<<" ";
- }
- cout<<endl;
- }
- int main(){
- freopen("C:\\Users\\Administrator\\Desktop\\c++.txt", "r", stdin);
- TreeNode* root = nullptr;
- vector<int> result;
-
- cout<<"1. 创建二叉树"<<endl;
- CreateTree(root);
- cout<<"-----------------------------"<<endl;
-
- cout<<"2.1 递归先序遍历"<<endl;
- PreOrder(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
-
- cout<<"2.2 非递归先序遍历"<<endl;
- PreOrder2(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
-
- cout<<"3.1 递归中序遍历"<<endl;
- InOrder(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
-
- cout<<"3.2 非递归中序遍历"<<endl;
- InOrder2(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
-
- cout<<"4.1 递归后序遍历"<<endl;
- PostOrder(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
-
- cout<<"4.2 非递归后序遍历"<<endl;
- PostOrder2(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
-
- cout<<"5 层次遍历"<<endl;
- LevelOrder(root,result);
- Print(result);
- result.clear();
- cout<<"-----------------------------"<<endl;
- return 0;
- }
测试用例:
输入:
ABC##DE#G##F###
输出:
代码二:
二叉树的先序/中序/后序/层次遍历
标签:min 二叉树 epo 构造 word admin typedef nts scanf
原文地址:http://www.cnblogs.com/KingIceMou/p/7000812.html