创建二叉树,先序遍历、中序遍历、后序遍历二叉树,求二叉树的深度,求叶子结点个数,求结点个数(C++)

#include 
#include 
#include 
using namespace std;

//a b d # g # # # c e # # f h # # #

//树结点
struct TreeNode
{
    TreeNode* lChild;
    char data;
    TreeNode* rChild;
};

//二叉树类
class BinaryTree
{
public:
    BinaryTree()
    {
        this->root = NULL;
    }
    
    void createBinaryTree(TreeNode* &t) //先序序列创建二叉树
    {
        char value;
        cin >> value;

        if (value != '#')
        {
            t = new TreeNode;
            t->data = value;
            createBinaryTree(t->lChild);
            createBinaryTree(t->rChild);
        }
        else
        {
            t = NULL;
            return;
        }
    }
    
    void preOrder01(TreeNode* t) //先序遍历(运用递归算法)
    {
        if (t != NULL)
        {
            cout << t->data << " ";
            preOrder01(t->lChild);
            preOrder01(t->rChild);
        }
    }
    
    void preOrder02() //非递归先序遍历 (应用栈)
    {
        stack<TreeNode*>stk;

        TreeNode* t = this->root;
        while (t != NULL || !stk.empty())
        {
            if (t != NULL)
            {
                cout << t->data << " ";
                stk.push(t);
                t = t->lChild;
            }
            else
            {
                t = stk.top();
                stk.pop();
                t = t->rChild;
            }
        }
    }
    
    void inOrder01(TreeNode* t) //中序遍历(运用递归算法)
    {
        if (t != NULL)
        {
            inOrder01(t->lChild);
            cout << t->data << " ";
            inOrder01(t->rChild);
        }
    }
    
    void inOrder02() //非递归中序遍历(应用栈)
    {
        stack<TreeNode*>stk;

        TreeNode* t = this->root;
        while (t != NULL || !stk.empty())
        {
            if (t != NULL)
            {
                stk.push(t);
                t = t->lChild;
            }
            else
            {
                t = stk.top();
                cout << t->data << " ";
                stk.pop();
                t = t->rChild;
            }
        }
    }
    
    void postOrder(TreeNode* t) //后序遍历(运用递归算法)
    {
        if (t != NULL)
        {
            postOrder(t->lChild);
            postOrder(t->rChild);
            cout << t->data << " ";
        }
    }
    
    void levelOrder() //层次遍历(应用队列)
    {
        queue<TreeNode*>q;

        TreeNode* t = this->root;
        q.push(t);
        while (!q.empty())
        {
            t = q.front();
            cout << t->data << " ";
            q.pop();
            if (t->lChild != NULL) { q.push(t->lChild); }
            if (t->rChild != NULL) { q.push(t->rChild); }
        }
    }
    
    void copyTree(TreeNode* &oldT, TreeNode* &newT) //复制二叉树
    {
        if (oldT != NULL)
        {
            newT = new TreeNode;
            newT->data = oldT->data;
            copyTree(oldT->lChild, newT->lChild);
            copyTree(oldT->rChild, newT->rChild);
        }
        else
        {
            newT = NULL;
            return;
        }
    }
    
    int getDepth(TreeNode* t) //获得二叉树深度
    {
        if (t != NULL) { return max(getDepth(t->lChild), getDepth(t->rChild)) + 1; }
        else { return 0; }
    }
    
    int nodeCount(TreeNode* t) //获得结点个数
    {
        if (t != NULL) { return nodeCount(t->lChild) + nodeCount(t->rChild) + 1; }
        else { return 0; }
    }
    
    int LeafCount(TreeNode* &t) //获得叶子结点个数
    {
        if (t == NULL) { return 0; }
        if (t->lChild == NULL && t->rChild == NULL)
        {
            return 1;
        }
        else if(t->lChild != NULL || t->rChild != NULL)
        {
            return LeafCount(t->lChild) + LeafCount(t->rChild);
        }
    }
    
public:
    TreeNode* root; //指向根结点的指针
};

int main() {
    BinaryTree bt;
    bt.createBinaryTree(bt.root);
    cout << "---------------遍历--------------" << endl;
    cout << "递归先序遍历:";
    bt.preOrder01(bt.root); 
    cout << endl;
    cout << "非递归先序遍历:";
    bt.preOrder02(); 
    cout << endl;
    cout << "递归中序遍历:";
    bt.inOrder01(bt.root);
    cout << endl;
    cout << "非递归中序遍历:";
    bt.inOrder02();
    cout << endl;
    cout << "递归后序遍历:";
    bt.postOrder(bt.root);
    cout << endl;
    cout << "层次遍历:";
    bt.levelOrder();
    cout << endl;
    cout << "------------复制二叉树-----------" << endl;
    BinaryTree bt02;
    bt02.copyTree(bt.root, bt02.root);
    cout << "先序遍历新树:";
    bt02.preOrder01(bt02.root);
    cout << endl;
    cout << "----------获得二叉树深度----------" << endl;
    cout << "树的深度:" << bt.getDepth(bt.root) << endl;
    cout << "-----------获得结点个数-----------" << endl;
    cout << "结点个数:" << bt.nodeCount(bt.root) << endl;
    cout << "---------获得叶子结点个数---------" << endl;
    cout << "叶子结点个数:" << bt.LeafCount(bt.root) << endl;

    system("pause");
    return 0;
}

结果如下:
创建二叉树,先序遍历、中序遍历、后序遍历二叉树,求二叉树的深度,求叶子结点个数,求结点个数(C++)_第1张图片

​​

你可能感兴趣的:(c++,数据结构,算法)