数据结构(二叉树)

来源:互联网 发布:淘宝店铺改名字怎么改 编辑:程序博客网 时间:2024/06/05 20:06
#ifndef TREE_H#define TREE_H#include <iostream>using namespace std;template<class T>struct BinaryTreeNode{    BinaryTreeNode *parent;    T data;    BinaryTreeNode* leftChild;    BinaryTreeNode* rightChild;    BinaryTreeNode()    {        parent = leftChild = rightChild = nullptr;        data = (T)0;    }    BinaryTreeNode(T data, BinaryTreeNode *parent, BinaryTreeNode *leftChild, BinaryTreeNode *rightChild)    {        this->data = data;        this->parent = parent;        this->leftChild = leftChild;        this->rightChild = rightChild;    }};template<class T>class BinaryTree{public:    BinaryTree()    {        root = new BinaryTreeNode<T>();        depth = 0;        count = 0;    }    ~BinaryTree()    {        ClearBitTree();        delete root;    }    //清空二叉树,使之成为空二叉树    void ClearBitTree()    {    }    //是否是空二叉树    bool IsEmptyBiTree()    {        return root->leftChild == nullptr;    }    //获取树的深度    int GetDepth()    {        if (IsEmptyBiTree())        {            return 0;        }        return GetDepth(root->leftChild);    }    int GetDepth(BinaryTreeNode<T> *node)    {        int leftDepth = node->leftChild == nullptr ? 0 : GetDepth(node->leftChild);        int ritghDepth = node->rightChild == nullptr ? 0 : GetDepth(node->rightChild);        return (leftDepth > ritghDepth ? leftDepth : ritghDepth) + 1;    }    //获取树的大小    int GetCount()    {        return count;    }    //获取二叉树的跟    BinaryTreeNode<T> GetRoot()    {        if (IsEmptyBiTree())        {            return nullptr;        }        return root->leftChild;    }    //获取节点的左兄弟    BinaryTreeNode<T> *GetLeftSibling(BinaryTreeNode<T> *node)    {        BinaryTreeNode<T> *parent = node->parent;        if (parent == nullptr)        {            return nullptr;        }        if (parent->leftChild == node)        {            return nullptr;        }        return parent->leftChild;    }    //获取节点的右兄弟    BinaryTreeNode<T> *GetRightSibling(BinaryTreeNode<T> *node)    {        BinaryTreeNode<T> *parent = node->parent;        if (parent == nullptr)        {            return nullptr;        }        if (parent->rightChild == node)        {            return nullptr;        }        return parent->rightChild;    }    //LR = 0左孩子, 1右孩子    void InsertChild(BinaryTreeNode<T> *node, int LR, T value)    {        if (node == nullptr)        {            return;        }        if (LR == 0)        {            BinaryTreeNode<T> *leftChild = new BinaryTreeNode<T>(value, node, nullptr, nullptr);            node->leftChild = leftChild;        }        else if (LR == 1)        {            BinaryTreeNode<T> *rightChild = new BinaryTreeNode<T>(value, node, nullptr, nullptr);            node->rightChild = rightChild;        }    }    void DeleteChild(BinaryTreeNode<T> *node, int LR)    {        if (node == nullptr)        {            return;        }        if (LR == 0)        {            BinaryTreeNode<T> *leftChild = node->leftChild;            if (leftChild == nullptr)            {                return;            }            node->leftChild = nullptr;            delete leftChild;        }        else if (LR == 1)        {            BinaryTreeNode<T> *rightChild = node->rightChild;            if (rightChild == nullptr)            {                return;            }            node->rightChild = nullptr;            delete rightChild;        }    }protected:    ////二叉树的头数据,左孩子为空则表示无数据,是空二叉树    BinaryTreeNode<T> *root;    int depth;    int count;};class BinaryTreePrint : public BinaryTree<int>{public:    void InOrderTraverse(BinaryTreeNode<int> *node)    {        if (node == nullptr)        {            return;        }        InOrderTraverse(node->leftChild);        InOrderTraverse(node->rightChild);        cout << node->data << endl;    }    //中序输出树    void InOrderTraversePrint()    {        if (IsEmptyBiTree())        {            cout << "Tree is Empty" << endl;        }        cout << "Tree Order-----------------------------" << endl;        InOrderTraverse(root->leftChild);        cout << "Depth = " << GetDepth() << endl;        cout << "Count = " << GetCount() << endl;    }    //先序创建树    void CreateBiTreeNode(BinaryTreeNode<int> **node, BinaryTreeNode<int> *parent)    {        count++;        int num;        cin >> num;        if (num == 0)        {            node = nullptr;            count--;        }        else        {                       *node = new BinaryTreeNode<int>(num, parent, nullptr, nullptr);            CreateBiTreeNode(&(*node)->leftChild, *node);            CreateBiTreeNode(&(*node)->rightChild, *node);        }    }    void CreateBiTree()    {        CreateBiTreeNode(&root->leftChild, root);    }};#endif
0 0
原创粉丝点击