树(详细代码)

树形结构是一类重要的非线性结构。树形结构是节点之间有分支,并具有层次关系的结构。

一、树的定义 

树是包含n个结点的有穷集。树中每个元素用结点表示。

树是由根节点和若干颗子树构成。 

二、树形结构基本术语 

1. 节点的度:一个节点含有的子树的个数

2. 树的度:一颗树中,最大的节点的度

3. 叶节点:度为0的节点

4. 分支节点:度不为0的节点

5. 树的高度:树中节点的最大层次

三、代码示例

1. 树形结构

template
class Tree
{
    //节点数据结构
    struct Node
    {
        int data;
        Node *pP;//父节点
        Node *pB;//兄弟节点
        Node *pC;//子节点
    };
    Node *pRoot;//根节点
}

2. 基本函数

public:
    Tree();//构造函数
    ~Tree();//析构函数
    void clear();//清空树
    bool find(T const & findData);//查找函数
    void Insert(T const data,T const findData,bool isChild);//插入函数

private:
    void _clear(Node *root);//内部清除函数
    Node * _find(Node *root,T const & findData);//内部查找函数

3. 内部查找函数

Node * _find(Node *root,T const & findData)//内部查找函数
    {
        //1.该节点是否为空
        if (root)
        {
            //2.该节点为查找节点
            if (root->data == findData)
            {
                return root;
            }
            else
            {
                //3.递归该节点的兄弟节点,并返回给辅助节点
                Node * tempNode = _find(root->pB,findData);
                //4.该节点是否为查找节点,并返回
                if (tempNode)
                {
                    return tempNode;
                }
                //5.递归节点的子节点,并返回
                return _find(root->pC,findData);
            }
        }
        else
        {
            return nullptr;
        }
    }

4. 内部清除函数

template
void Tree::_clear(Node *root)//内部清除函数
{
    if (root)
    {
        _clear(root->pB);
        _clear(root->pC);

        root->data = 0;
        root = nullptr;
        delete root;
    }
}

5. 插入函数

template
void Tree::Insert(T const insertData,T const findData,bool isChild)
{
    //1.构建插入节点
    Node * insertNode = new Node;
    insertNode->data = insertData;
    insertNode->pP = nullptr;
    insertNode->pB = nullptr;
    insertNode->pC = nullptr;
    //2.根节点是否为空
    if (pRoot)
    {
        Node * findNode = _find(pRoot,findData);
        //3.寻找节点是否为空
        if (findNode)
        {
            //4.插入位置是否为子节点
            if (isChild)
            {
                //5.插入位置子节点为空
                if (findNode->pC)
                {
                    Node  * tempNode = findNode->pC;
                    while (tempNode->pB)
                    {
                        tempNode = tempNode->pB;
                    }
                    tempNode->pB = insertNode;
                    insertNode->pP = tempNode->pP;
                }
                else
                {
                    findNode->pC = insertNode;
                    insertNode->pP = findNode;
                }
            }
            //在该节点的兄弟节点插入(有序 兄弟节点最后插入)
            else
            {
                while (findNode->pB)
                {
                    findNode = findNode->pB;
                }
                findNode->pB = insertNode;
                insertNode->pP = findNode->pP;
            }
        }
        //寻找节点为空
        else
        {
            //在根节点最下面的子节点插入
            Node * tempNode = pRoot;//辅助节点
            while (tempNode->pC)
            {
                tempNode = tempNode->pC;
            }
            tempNode->pC = insertNode;
            insertNode->pP = tempNode;
        }
    }
    //根节点为空
    else
    {
        pRoot = insertNode;//空树插入
    }
}

6. 查找函数

template
bool Tree::find(T const & findData)
{
    return _find(pRoot,findData) != nullptr;
}

7. 清除函数

template
void Tree::clear()
{
    _clear(pRoot);
}

8. 构造析构函数

template
Tree::Tree()
{
    pRoot = nullptr;
}
template
Tree::~Tree()
{
    clear();
}

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