二叉树(链表表示)

Posted chengmf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树(链表表示)相关的知识,希望对你有一定的参考价值。

Node.h

#pragma once

class Node
{
public:
    Node();
    Node* SearchNode(int indexnode);
    void DeleteNode();
    void PreorderTraversal();
    void InorderTraversal();
    void PostorderTraversal();
    int index;
    int data;
    Node* pLChild;
    Node* pRChild;
    Node* pParent;
};

Node.cpp

#include "Node.h"
#include<iostream>
using namespace std;
Node::Node()
{
    index = 0;
    data = 0;
    pLChild = nullptr;
    pRChild = nullptr;
    pParent = nullptr;
}

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

void Node::DeleteNode()
{
    if (this->pLChild != nullptr)
    {
        this->pLChild->DeleteNode();
    }
    if (this->pRChild != nullptr)
    {
        this->pRChild->DeleteNode();
    }
    if (this->pParent != nullptr)
    {
        if (this->pParent->pLChild == this)
        {
            this->pParent->pLChild = nullptr;
        }
        if (this->pParent->pRChild == this)
        {
            this->pParent->pRChild = nullptr;
        }
    }
    delete this;
}

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

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

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

Tree.h

#pragma once
#include"Node.h"

class Tree
{
public:
    Tree();
    ~Tree();
    Node* SearchNode(int indexnode);
    bool AddNode(int nodeindex, int direction, Node* pNode);
    bool DeleteNode(int nodeindex, Node* pNode);
    void PreorderTraversal();
    void InorderTraversal();
    void PostorderTraversal();
private:
    Node* m_pRoot;
};

Tree.cpp

#include "Tree.h"

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 == nullptr)return false;
    Node* node = new Node();
    if (node == nullptr)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 Tree::DeleteNode(int nodeindex, Node* pNode)
{
    Node* temp = SearchNode(nodeindex);
    if (temp == nullptr)return false;

    if (pNode != nullptr)
    {
        pNode->index = temp->index;
        pNode->data = temp->data;
    }
    temp->DeleteNode();
    return true;
}

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

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

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

源.cpp

#include<iostream>
#include"Tree.h"
using namespace std;

/*
        (0)

    5(1)    8(2)

2(3)  6(4)  9(5)  7(6)

*/

int main()
{
    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);

    cout << "前序遍历结果为:" << endl;
    tree->PreorderTraversal();

    cout << "中序遍历结果为:" << endl;
    tree->InorderTraversal();

    cout << "后序遍历结果为:" << endl;
    tree->PostorderTraversal();

    tree->DeleteNode(2, nullptr);

    cout << "前序遍历结果为:" << endl;
    tree->PreorderTraversal();

    delete tree;
    tree = nullptr;
    return 0;
}

以上是关于二叉树(链表表示)的主要内容,如果未能解决你的问题,请参考以下文章

二叉树与链表

数据结构 二叉树 用二叉链链表存储结构 写出删除二叉树所有的叶子节点的算法

设一棵二叉树以二叉链表表示,试以成员函数形式编写有关二叉树的递归算法:

如何在数据结构中,以二叉链表为存储结构,建立一棵二叉树,输出其先序,中序,后序遍历序列,统计其叶子

二叉树(链表表示)

基于二叉链表的二叉树的嵌套括号表示