二叉树【链表实现】基础练习

来源:互联网 发布:linux elinks命令 编辑:程序博客网 时间:2024/04/30 17:53

这是二叉树结构图:
这里写图片描述
可以看出每个节点都有一个数据域、一个索引、三个节点指针:父节点指针、左孩子结点指针、右孩子结点指针。这样才能根连着枝再连着叶
父节点相当于单链表的指针,把根和非终端节点连接在一起。
demo.cpp

/**************************************//*二叉树:链表实现Tree();                                                //创建树~Tree();                                               //销毁树Node *SerachNode(int nodeIndex);                       //搜索节点bool AddNode(int nodeIndex,int direction,Node*pNode);  //添加节点bool DeleteNode(int nodeIndex,Node*PNode);             //删除节点void PreorderTraverse();                               //前序遍历void InorderTraverse();                                //中序遍历void PostorderTraverse();                              //后序遍历节点要素:索引 数据 左孩子指针 右孩子指针                 (0)       5(1)              8(2)   2(3 ) 6(4)        9(5) 7(6)前:0134256中:3140526后:3415620   *//***************************************/#include<windows.h>#include"Tree.h"void funtest1(){    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->PreorderTraversal();    //tree->InorderTraversal();    tree->InorderTraversal();    tree->DeleteNode(6, NULL);    tree->InorderTraversal();}int main(void){    funtest1();    system("pause");    return 0;}

Tree.h

#ifndef TREE_H#define TREE_H#include"Node.h"class Tree{public:    Tree();    ~Tree();    Node*SearchNode(int nodeIndex);    bool AddNode(int nodeIndex, int direction, Node*pNode);    bool DeleteNode(int nodeIndex, Node*pNode);    void PreorderTraversal();    void InorderTraversal();    void PostorderTraversal();private:    Node*m_pRoot;};#endif

Tree.cpp

#include"Tree.h"#include<iostream>Tree::Tree(){    m_pRoot = new Node();}Tree::~Tree()  //销毁整个节点,树就销毁了{    DeleteNode(0, NULL);}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)    {        return false;    }    Node*node = new Node();    if (NULL == node)    {        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 == NULL)    {        return false;    }    if (pNode != NULL)    {        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();}

Node.h

#ifndef NODE_H#define NODE_Hclass Node{public:    Node();    Node*SearchNode(int nodeIndex);    void DeleteNode();    void PreorderTraversal();    void InorderTraversal();    void PostorderTraversal();    int index;    int data;    Node*pLChild;    Node*pRChild;    Node*pParent;};#endif

Node.cpp

#include"Node.h"#include<iostream>using namespace std;Node::Node(){    index = 0;    data = 0;    pLChild = NULL;    pRChild = NULL;    pParent = NULL;}Node*Node::SearchNode(int nodeIndex){    if (this->index == nodeIndex)    {        return this;    }    Node*temp = NULL;    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        {            temp=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::PreorderTraversal(){    cout << this->index << "  " << this->data << endl;    if (this->pLChild != NULL)    {        this->pLChild->PreorderTraversal();    }    if (this->pRChild != NULL)    {        this->pRChild->PreorderTraversal();    }}void Node::InorderTraversal(){    if (this->pLChild != NULL)    {        this->pLChild->InorderTraversal();    }    cout << this->index << "  " << this->data << endl;    if (this->pRChild != NULL)    {        this->pRChild->InorderTraversal();    }}void Node::PostorderTraversal(){    if (this->pLChild != NULL)    {        this->pLChild->PostorderTraversal();    }    if (this->pRChild != NULL)    {        this->pRChild->PostorderTraversal();    }    cout << this->index << "  " << this->data << endl;}

总结:数据结构还是相当简单的,首先应该掌握的就是他的结构,他的每个单元的基本组成,之间相互关系。其次就是掌握递归,找出递归基,将问题规模逐渐缩小。

0 0