C++实现二叉树链表

本文介绍如何使用C++实现二叉树链表。通过Node.h定义节点结构,Tree.h实现二叉树链表的相关操作,以及main.cpp进行测试。详细探讨了节点定义、树的构建及遍历等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++实现二叉树链表

Node.h

#ifndef NODE_H
#define NODE_H
#include <iostream>

using namespace std;

class Node
{
public:
    Node();
    ~Node();
    Node *SearchNode(int nodeIndex);
    void DeleteNode();
    void PreordeTraverse();
    void InorderTraverse();
    void PostorderTraverse();
    int index;
    int data;
    Node *pLChild;
    Node *pRChild;
    Node *pParent;

private:

};

Node::Node()
{
    index = 0;
    data = 0;
    pLChild = NULL;
    pRChild = NULL;
    pParent = NULL;
}

Node::~Node()
{
}

Node *Node::SearchNode(int nodeIndex)
{
    Node *temp = NULL;
        if (this->index == nodeIndex)
        {
            return this;
        }
        if (this->pLChild != NULL)
        {
            if (this->pLChild->index == nodeIndex)
            {
                return this->pLChild;
            }
            else
            {
                temp = this->pLChild->SearchNode(nodeIndex);
                if (temp != NULL)
                {
                    return temp;
                }
            }

        }
        if (this->pRChild != NULL)
        {
            if (this->pRChild->index == nodeIndex)
            {
                return this->pRChild;
            }
            else
            {
                this->pRChild->SearchNode(nodeIndex);
                if (temp != NULL)
                {
                    return temp;
                }
            }

        }
    return NULL;
}

void Node::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;
}

void Node::PreordeTraverse()
{
    cout << this->index << " " << this->data << endl;
    if (this->pLChild != NULL)
    {
        this->pLChild->PreordeTraverse();
    }
    if (this->pRChild != NULL)
    {
        this->pRChild->PreordeTraverse();
    }
}

void Node::InorderTraverse()
{
    if (this->pLChild != NULL)
    {
        this->pLChild->InorderTraverse();
    }
    cout << this->index << " " << this->data << endl;

    if (this->pRChild != NULL)
    {
        this->pRChild->InorderTraverse();
    }
}

void Node::PostorderTraverse()
{
    if (this->pLChild != NULL)
    {
        this->pLChild->PostorderTraverse();
    }
    if (this->pRChild != NULL)
    {
        this->pRChild->PostorderTraverse();
    }
    cout << this->index << " " << this->data << endl;
}

#endif // !NODE_H

Tree.h

#ifndef TREE_H
#define TREE_H
#include "Node.h"
#include <iostream>
using namespace std;

class Tree
{
public:
    Tree();                         //创建树
    ~Tree();                                           //销毁树
    Node *SearchNode(int nodeIndex);                        //根据索引寻找节点
    bool AddNode(int nodeIndex, int direction, Node *pNode);            //添加节点
    bool DeleteNode(int nodeIndex, Node *pNode);            //删除节点
    void PreordeTraverse();                             //前序遍历
    void InorderTraverse();                             //中序遍历
    void PostorderTraverse();                           //后序遍历

private:
    Node *m_pRoot;

};

Tree::Tree()
{
    m_pRoot = new Node();
}

Tree::~Tree()
{
    m_pRoot->DeleteNode();
}

Node *Tree::SearchNode(int nodeIndex)
{
    return m_pRoot->SearchNode(nodeIndex);

}

bool Tree::AddNode(int nodeIndex, int direction, Node *pNode)
{
    Node *temp = SearchNode(nodeIndex);

    if (temp != NULL)
    {
        Node *currentNode = new Node;
        if (currentNode == NULL)
        {
            return false;
        }
        currentNode->index = pNode->index;
        currentNode->data = pNode->data;
        currentNode->pParent = temp;
        if (direction == 0)
        {
            temp->pLChild = currentNode;
        }

        if (direction == 1)
        {
            temp->pRChild = currentNode;
        }

        return true;
    }
    return false;
}

bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
    Node *temp = SearchNode(nodeIndex);
    if (temp == NULL)
    {
        return false;
    }
    if (pNode != NULL)
    {
        pNode->data = temp->data;
    }
    temp->DeleteNode();
    return true;
}

void Tree::PreordeTraverse()
{
    m_pRoot->PreordeTraverse();
}

void Tree::InorderTraverse()
{
    m_pRoot->InorderTraverse();
}

void Tree::PostorderTraverse()
{
    m_pRoot->PostorderTraverse();
}



#endif

main.cpp

#include "Tree.h"
#include "Node.h"

int main()
{
    Tree *pTree = new Tree;
    Node *e1 = new Node;
    e1->index = 1;
    e1->data = 1;
    Node *e2 = new Node;
    e2->index = 2;
    e2->data = 2;
    Node *e3 = new Node;
    e3->index = 3;
    e3->data = 3;
    Node *e4 = new Node;
    e4->index = 4;
    e4->data = 4;
    Node *e5 = new Node;
    e5->index = 5;
    e5->data = 5;
    Node *e6 = new Node;
    e6->index = 6;
    e6->data = 6;
    Node *e7 = new Node;
    e7->index = 7;
    e7->data = 7;
    Node *e8 = new Node;
    e8->index = 8;
    e8->data = 8;

    pTree->AddNode(0, 0, e1);
    pTree->AddNode(0, 1, e2);
    pTree->AddNode(1, 0, e3);
    pTree->AddNode(1, 1, e4);
    pTree->AddNode(2, 0, e5);
    pTree->AddNode(2, 1, e6);
    pTree->AddNode(3, 0, e7);
    pTree->AddNode(4, 1, e8);

    //pTree->DeleteNode(2, NULL);
    pTree->PreordeTraverse();
    cout << endl;
    pTree->InorderTraverse();
    cout << endl;
    pTree->PostorderTraverse();

    delete pTree;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值