首页 > 其他 > 详细

二叉树的构建

时间:2019-11-13 21:25:34      阅读:73      评论:0      收藏:0      [点我收藏+]

一、

#include <iostream>
using namespace std;
class Node
{
public:
    int index;//节点索引
    int data;//节点数据
    Node *pLChild;//左孩子
    Node *pRChild;//右孩子
    Node *pParent;//双亲节点
public:
    Node()
    {
        index = 0;
        data = 0;
        pLChild = NULL;
        pRChild = NULL;
        pParent = NULL;
    }
    Node* SearchNode(int nodeIndex)//从该节点往下开始搜索nodeIndex
    {
        if(this->index == nodeIndex)
        {
            return this;
        }
        if(this->pLChild!=NULL)
        {
            if(this->pLChild->index == nodeIndex)
                return this->pLChild;
            Node *temp = pLChild->SearchNode(nodeIndex);
            if(NULL!=temp)
                return temp;
        }
        if(this->pRChild!=NULL)
        {
            if(this->pRChild->index == nodeIndex)
                return this->pRChild;
            Node *temp = pRChild->SearchNode(nodeIndex);
            if(NULL != temp)
                return temp;
        }
        return NULL;
    }
    void DeleteNode()//删除该节点以及该节点下面的节点
    {
        if(this->pLChild != NULL)
        {
            this->pLChild->DeleteNode();
        }
        if(this->pRChild != NULL)
        {
            this->pRChild->DeleteNode();
        }
        if(this->pParent != NULL)
        {
            if(this->pParent->pLChild == this)
            {
                this->pParent->pLChild = NULL;
            }
            if(this->pParent->pRChild == this)
            {
                this->pParent->pRChild = NULL;
            }
        }
        delete this;
    }
};
class Tree
{
private:
    Node *m_pRoot;
public:
    Tree()//创建树
    {
        m_pRoot = new Node();
    }
    Node* SearchNode(int nodeIndex)//搜索节点
    {
        return m_pRoot->SearchNode(nodeIndex);
    }
    bool AddNode(int nodeIndex,int direction,Node *pNode)//添加节点
    {
       Node *temp = SearchNode(nodeIndex);
       if(temp == NULL)
       {
           return false;
       }
       Node *node = new Node();
       if(node == NULL)
       {
           return false;
       }
       node->index = pNode->index;
       node->data = pNode->data;
       node->pParent = temp;
       if(direction == 0)
       {
           temp->pLChild = node;
       }
       if(direction == 1)
       {
           temp->pRChild = node;
       }
       return true;
    }
    bool DeleteNode(int nodeIndex,Node *pNode)//删除节点
    {
        Node *temp = SearchNode(nodeIndex);
        if(temp == NULL)
        {
            return false;
        }
        if(pNode != NULL)
        {
            pNode->index = temp->index;
            pNode->data = temp->data;
        }
        temp->DeleteNode();
        return true;
    }
    ~Tree()//销毁树
    {
        //DeleteNode(0,NULL);
        m_pRoot->DeleteNode();
    }
};
int main()
{
    /*         (0)
        5(1)         8(2)
     2(3)   6(4) 9(5)     7(6)*/
    Node *node1 = new Node();
    node1->index = 1;
    node1->data = 5;

    Node *node2 = new Node();
    node2->index = 2;
    node2->data = 8;

    Node *node3 = new Node();
    node3->index = 3;
    node3->data = 2;

    Node *node4 = new Node();
    node4->index = 4;
    node4->data = 6;

    Node *node5 = new Node();
    node5->index = 5;
    node5->data = 9;

    Node *node6 = new Node();
    node6->index = 6;
    node6->data = 7;

    Tree *tree = new Tree();
    tree->AddNode(0,0,node1);
    tree->AddNode(0,1,node2);
    tree->AddNode(1,0,node3);
    tree->AddNode(1,1,node4);
    tree->AddNode(2,0,node5);
    tree->AddNode(2,1,node6);
    //tree->DeleteNode(2,NULL);
    //tree->DeleteNode(6,NULL);
    //tree->DeleteNode(5,NULL);
    cout << "Hello world!" << endl;
    return 0;
}

 

二叉树的构建

原文:https://www.cnblogs.com/good-hair/p/11853119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!