手写 二叉树--链表实现及相关操作

Node.h

/

#pragma once


#include<iostream>

using namespace std;

//节点要素:索引 数据 左孩子指针 右孩子指针, 父节点指针
class Node
{
public:
    Node(void);
    ~Node(void);

    //搜索节点 这个函数是最先需要实现的
    Node* SerchNode(int nodeIdex);

    //添加节点
    bool AddNode(int nodeIndex, int direction, Node* pNode);
    //删除节点,删掉此节点同时它的所有子节点都要删除
    void DeleteNode();

    //前序遍历
    void PreOrderTravers();
    //中序遍历
    void InOrderTravers();
    //后序遍历
    void PostOrderTravers();

    
    int index;
    int data;
    Node* pLchild;
    Node* pRchild;
    Node* pParent;
};

Node.cpp

#include "StdAfx.h"
#include "Node.h"


Node::Node(void)
{
    index = 0;
    data = 0;
    pLchild = NULL;
    pRchild = NULL;
    pParent = NULL;
}


Node::~Node(void)
{
}

//没找到,就没在这个树上
Node * Node::SerchNode(int nodeIdex)
{
    if (this->index == nodeIdex)
    {
        return this;    
    }
    if (this->pLchild != NULL)
    {
        if (this->pLchild->index == nodeIdex)
        {
            return this->pLchild;
        }
    }

    if (this->pRchild != NULL)
    {
        if (this->pRchild->index == nodeIdex)
        {
            return this->pRchild;
        }
    }    
    
    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::PreOrderTravers()
{
    cout << this->data<<endl;

    if (this->pLchild != NULL)
    {
        this->pLchild->PreOrderTravers();
    }
    if (this->pRchild != NULL)
    {
        this->pRchild->PreOrderTravers();
    }

}

void Node::InOrderTravers()
{
    if (this->pLchild != NULL)
    {
        this->pLchild->InOrderTravers();
    }

    cout << this->data << endl;
    
    if (this->pRchild != NULL)
    {
        this->pRchild->InOrderTravers();
    }
}

void Node::PostOrderTravers()
{
    if (this->pLchild != NULL)
    {
        this->pLchild->PostOrderTravers();
    }

    if (this->pRchild != NULL)
    {
        this->pRchild->PostOrderTravers();

    }

    cout << this->data << endl;
    
}
 

//

Tree.h

#pragma once

#include "Node.h"

/*
链表实现二叉树

节点要素:索引 数据 左孩子指针 右孩子指针, 父节点指针

                (0)

        5(1)            8(2)

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

   前序遍历:0 1 3 4  2 5 6
   中序遍历:3 1 4 0  5 2 6
   后续遍历:3 4 1 5  6 2 0
*/


class Tree
{
public:
    Tree(void);

    //销毁树, 需要销毁所有节点
    ~Tree(void);

    //搜索节点 这个函数是最先需要实现的
    Node* SerchNode(int nodeIdex);

    //添加节点
    bool AddNode(int nodeIndex, int direction, Node* pNode);
    //删除节点,删掉此节点同时它的所有子节点都要删除
    bool DeleteNode(int nodeIndex, Node* pNode);

    //前序遍历
    void PreOrderTravers();
    //中序遍历
    void InOrderTravers();
    //后序遍历
    void PostOrderTravers();

private:
    Node* m_pRoot;
};

Tree.cpp

#include "StdAfx.h"
#include "Tree.h"


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


Tree::~Tree(void)
{
}

Node * Tree::SerchNode(int nodeIdex)
{
    return m_pRoot->SerchNode(nodeIdex);
    
}

bool Tree::AddNode(int nodeIndex, int direction, Node * pNode)
{
    Node* temp = SerchNode(nodeIndex);
    if (temp == NULL)
    {
        return false;
    }
    else
    {
        Node* node = new Node();
        if (node == NULL)
        {
            return false;
        }
        node->data = pNode->data;
        node->index = pNode->index;

        if (direction == 0)
        {
            temp->pLchild = node;
        }
        if (direction == 1)
        {
            temp->pRchild = node;
        }
    }
    return true;
}

bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
    Node* temp = SerchNode(nodeIndex);
    if (temp == NULL)
    {
        return false;
    }

    pNode->data = temp->data;

    temp->DeleteNode();

    return false;
}

void Tree::PreOrderTravers()
{
    this->m_pRoot->PreOrderTravers();
}

void Tree::InOrderTravers()
{
    this->m_pRoot->InOrderTravers();
}

void Tree::PostOrderTravers()
{
    this->m_pRoot->PostOrderTravers();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值