二叉树(前序遍历+中序遍历+后序遍历)递归

本文介绍了一种使用C++实现二叉树的操作方法,包括二叉树的创建、遍历(前序、中序、后序及层序)、计算叶子节点数量及树的高度等功能。
// BinaryTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include <algorithm>
#include<queue>
using namespace std;
template <class T>
struct BiNode   //二叉树的结点结构
{
    T data;
    BiNode<T> *lchild, *rchild;

};
template <class T>
class BiTree
{
public:
    BiTree();             //构造函数,初始化一棵二叉树,其前序序列由键盘输入
    ~BiTree(void);         //析构函数,释放二叉链表中各结点的存储空间
    BiNode<T>* Getroot();  //获得指向根结点的指针
    void PreOrder(BiNode<T> *root);     //前序遍历二叉树
    void InOrder(BiNode<T> *root);      //中序遍历二叉树
    void PostOrder(BiNode<T> *root);    //后序遍历二叉树
    void LeverOrder(BiNode<T> *root);   //层序遍历二叉树
    int Leaf (BiNode<T> *root);    //计算二叉树的叶子数
    int Deep(BiNode<T> *root);     //计算二叉树的树高
    bool isComplete(BiNode<T> *root);//判断是否为完全二叉树
private:
    BiNode<T> *root;         //指向根结点的头指针
    BiNode<T> *Creat();     //有参构造函数调用
    void Release(BiNode<T> *root);   //析构函数调用 
};
template<class T>
BiTree<T>::BiTree()
{
    this->root = Creat();
}
template <class T>
BiNode<T>* BiTree<T>::Creat()
{
    BiNode<T>* root;
    T ch;

    cout << "请输入创建一棵二叉树的结点数据" << endl;
    cin >> ch;
    if (ch == '#') root = NULL;
    else
    {
        root = new BiNode<T>;       //生成一个结点
        root->data = ch;
        root->lchild = Creat();    //递归建立左子树
        root->rchild = Creat();    //递归建立右子树
    }
    return root;
}
template<class T>
BiTree<T>::~BiTree(void)
{
    Release(root);
}
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
    if (root != NULL) {
        Release(root->lchild);   //释放左子树
        Release(root->rchild);   //释放右子树
        delete root;
    }
}
template<class T>
BiNode<T>* BiTree<T>::Getroot()
{
    return root;
}
template<class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
    if (root == NULL)  return;
    else {
        cout << root->data << " ";
        PreOrder(root->lchild);
        PreOrder(root->rchild);
    }
}
template <class T>
void BiTree<T>::InOrder(BiNode<T> *root)
{
    if (root == NULL)  return;      //递归调用的结束条件           
    else {
        InOrder(root->lchild);    //中序递归遍历root的左子树
        cout << root->data << " ";    //访问根结点的数据域
        InOrder(root->rchild);    //中序递归遍历root的右子树
    }
}
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
    if (root == NULL)   return;       //递归调用的结束条件
    else {
        PostOrder(root->lchild);    //后序递归遍历root的左子树
        PostOrder(root->rchild);    //后序递归遍历root的右子树
        cout << root->data << " ";      //访问根结点的数据域
    }
}

template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
    const int MaxSize = 100;
    int front = 0;
    int rear = 0;  //采用顺序队列,并假定不会发生上溢
    BiNode<T>* Q[MaxSize];
    BiNode<T>* q;
    if (root == NULL) return;
    else {
        Q[rear++] = root;
        while (front != rear)
        {
            q = Q[front++];
            cout << q->data << " ";
            if (q->lchild != NULL)    Q[rear++] = q->lchild;
            if (q->rchild != NULL)    Q[rear++] = q->rchild;
        }
    }
}
template<class T>
int BiTree<T>::Leaf(BiNode<T>* root)
{
    if (root == NULL)
        return 0;
    else if ((root->lchild == NULL) && (root->rchild == NULL))
        return 1;
    else
        return(Leaf(root->lchild) + Leaf(root->rchild));
}
template<class T>
int BiTree<T>::Deep(BiNode<T>* root)
{
    if (root == NULL)
        return 0;
    else
        return max(Deep(root->lchild), Deep(root->rchild)) + 1;
}
void main()
{
    BiTree<char> bt; //创建一棵树
    BiNode<char>* root = bt.Getroot();  //获取指向根结点的指针 
    cout << "------前序遍历------ " << endl;
    bt.PreOrder(root);
    cout << endl;
    cout << "------中序遍历------ " << endl;
    bt.InOrder(root);
    cout << endl;
    cout << "------后序遍历------ " << endl;
    bt.PostOrder(root);
    cout << endl;
    cout << "------层序遍历------ " << endl;
    bt.LeverOrder(root);
    cout << endl;
    cout << "------叶子结点------ " << endl;
    cout << bt.Leaf(root) << endl;
    cout << "------数的高度------ " << endl;
    cout << bt.Deep(root) << endl;
    system("pause");
    cout << endl;
}

通过二叉树前序遍历和中序遍历结果求后序遍历结果,可利用前序遍历和中序遍历的特点,结合递归的方法来实现。 前序遍历的顺序是先访问根节点,然后递归遍历左子树,最后递归遍历右子树;中序遍历的顺序是先递归遍历左子树,然后访问根节点,最后递归遍历右子树。因此,前序遍历的第一个元素就是二叉树的根节点,在中序遍历中找到该根节点的位置,其左边的元素就是左子树的中序遍历结果,右边的元素就是右子树的中序遍历结果。根据左子树和右子树的节点数量,又可以从前序遍历中划分出左子树和右子树的前序遍历结果。然后对左子树和右子树分别递归地进行上述操作,直到子树为空。 以下是实现该功能的 Java 代码示例: ```java class Solution { public void postOrder(int[] pre, int[] mid, int preStart, int preEnd, int midStart, int midEnd) { if (preStart > preEnd || midStart > midEnd) { return; } // 前序遍历的第一个元素是根节点 int rootVal = pre[preStart]; // 在中序遍历中找到根节点的位置 int rootIndexInMid = midStart; while (mid[rootIndexInMid] != rootVal) { rootIndexInMid++; } // 计算左子树的节点数量 int leftSubtreeSize = rootIndexInMid - midStart; // 递归遍历左子树 postOrder(pre, mid, preStart + 1, preStart + leftSubtreeSize, midStart, rootIndexInMid - 1); // 递归遍历右子树 postOrder(pre, mid, preStart + leftSubtreeSize + 1, preEnd, rootIndexInMid + 1, midEnd); // 输出根节点 System.out.print(rootVal + " "); } public static void main(String[] args) { Solution solution = new Solution(); int[] pre = {1, 2, 4, 5, 3, 6, 7}; int[] mid = {4, 2, 5, 1, 6, 3, 7}; solution.postOrder(pre, mid, 0, pre.length - 1, 0, mid.length - 1); } } ``` 上述代码中,`postOrder` 方法接收前序遍历数组 `pre`、中序遍历数组 `mid`,以及前序遍历和中序遍历的起始和结束索引。在方法内部,首先找到根节点,然后在中序遍历中确定根节点的位置,计算左子树的节点数量,接着递归地对左子树和右子树进行处理,最后输出根节点的值,从而实现后序遍历的结果输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值