模板类BinaryTree的插入与输出

代码:(有待改进)

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

int pow(int x,int y)
{
    static int res = 1;
    if(y == 0)
        return res;
    else
    {
        res*=x;
        pow(x,y-1);
    }
}

class binaryTreeNode
{
public:
    int data;
    binaryTreeNode *leftChild,*rightChild;
};

class binaryTree : public binaryTreeNode
{
private:
    binaryTreeNode *root;
public:
    binaryTree()
    {
        root = new binaryTreeNode;
        root->leftChild = NULL;
        root->rightChild = NULL;
    }
    ~binaryTree()
    {
        destory(root);
    }
    void destory(binaryTreeNode *&subtree)
    {
        if(subtree == NULL)
            return ;
        destory(subtree->leftChild);
        destory(subtree->rightChild);
        delete subtree;
    }

    void inorderCreatTree()
    {
        inorderCreatTree(root);
    }
    void inorderCreatTree(binaryTreeNode *&subtree)
    {
        int dx;
        cin>>dx;
        if(dx == 0)
        {
            subtree = NULL;
            return;
        }
        subtree = new binaryTreeNode;
        inorderCreatTree(subtree->leftChild);
        subtree->data = dx;
        inorderCreatTree(subtree->rightChild);
    }
     void preorderCreatTree()
    {
        preorderCreatTree(root);
    }
    void preorderCreatTree(binaryTreeNode *&subtree)
    {
        int dx;
        cin>>dx;
        if(dx == -1)
        {
            subtree = NULL;
            return;
        }
        subtree = new binaryTreeNode;
        subtree->data = dx;
        preorderCreatTree(subtree->leftChild);
        preorderCreatTree(subtree->rightChild);
    }
    void  postorderCreatTree()
    {
        postorderCreatTree(root);
    }
    void postorderCreatTree(binaryTreeNode *&subtree)
    {
        int dx;
        cin>>dx;
        if(dx == -1)
        {
            subtree = NULL;
            return;
        }
        subtree = new binaryTreeNode;
        postorderCreatTree(subtree->leftChild);
        postorderCreatTree(subtree->rightChild);
        subtree->data = dx;
    }
    void inoder()
    {
        inorder(root);
    }
    const void inorder(binaryTreeNode *subtree)
    {
        if(subtree == NULL)
            return ;
        inorder(subtree->leftChild);
        cout<<subtree->data<<endl;
        inorder(subtree->rightChild);
    }
    void preorder()
    {
        preorder(root);
    }
    const void preorder(binaryTreeNode *subtree)
    {
        if(subtree == NULL)
            return ;
        cout<<subtree->data<<' ';
        preorder(subtree->leftChild);
        preorder(subtree->rightChild);
    }
    void postorder()
    {
        postorder(root);
    }
    const void postorder(binaryTreeNode *subtree)
    {
        if(subtree == NULL)
            return ;
        postorder(subtree->leftChild);
        postorder(subtree->rightChild);
        cout<<subtree->data<<' ';
    }

};

int main()
{   
    binaryTree Test1;
    Test1.inorderCreatTree();
    Test1.inoder();
    system("pause");

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值