数据结构之二叉树(链表)

来源:互联网 发布:淘宝车商城 编辑:程序博客网 时间:2024/06/03 18:14

 链表实现二叉树的原理其实和数组实现的原理大同小异,但是因为是链表的缘故,所以操作的灵活性要比数组更加好,难度也比数组更高。链表实现二叉树,多了一个很关键的东西,那就是遍历的方法:前序遍历、中序遍历和后序遍历。

课程要求:完成树的基本操作
    1、树的创建和销毁
    2、树中结点的搜索
    3、树中结点的添加和删除
    4、树中结点的遍历

    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();                                            //后序遍历
    
    结点要素:索引  数据  左孩子指针 右孩子指针 父结点指针
    
    前序遍历:0 1 3 4 2 5 6        (上到下)
                a e b c h q
    中序遍历:3 1 4 0 5 2 6        (左到右)
              e a b   h c q
    后序遍历:3 4 1 5 6 2 0        (左右根)
              e b a h q c  
             (0)
            
        a(1)    c(2)
        
    e(3)  b(4)  h(5)  q(6)    

程序实现:

node.h

[cpp] view plain copy
  1. #ifndef _NODE_H  
  2. #define _NODE_H  
  3.   
  4. class Node {  
  5. public:  
  6.     Node();  
  7.     Node* SearchNode(int nodeindex);  
  8.     void DeleteNode();  
  9.     void PreorderTraversal();                                       //前序遍历  
  10.     void InorderTraversal();                                        //中序遍历  
  11.     void PostorderTraversal();                                      //后序遍历  
  12.     int index;  
  13.     char data;  
  14.     Node* pLChild;  
  15.     Node* pRChild;  
  16.     Node* pParent;  
  17. };  
  18.   
  19. #endif  

node.cpp

[cpp] view plain copy
  1. #include "node.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. Node::Node()  
  7. {  
  8.     index = 0;  
  9.     data = ' ';  
  10.     pLChild = NULL;  
  11.     pRChild = NULL;  
  12.     pParent = NULL;  
  13. }  
  14.   
  15. Node* Node::SearchNode(int nodeindex)  
  16. {  
  17.     if(this->index == nodeindex)  
  18.     {  
  19.         return this;  
  20.     }  
  21.       
  22.     Node *temp;  
  23.     if(this->pLChild != NULL)  
  24.     {  
  25.         if(this->pLChild->index == nodeindex)  
  26.         {  
  27.             return this->pLChild;  
  28.         }  
  29.         else   
  30.         {  
  31.             temp = this->pLChild->SearchNode(nodeindex);  
  32.             if(temp != NULL)  
  33.             {  
  34.                 return temp;                      
  35.             }  
  36.         }  
  37.     }  
  38.       
  39.     if(this->pRChild != NULL)  
  40.     {  
  41.         if(this->pRChild->index == nodeindex)  
  42.         {  
  43.             return this->pRChild;  
  44.         }  
  45.         else   
  46.         {  
  47.             temp = this->pRChild->SearchNode(nodeindex);  
  48.             if(temp != NULL)  
  49.             {  
  50.                 return temp;                      
  51.             }  
  52.         }  
  53.     }  
  54.       
  55.     return NULL;  
  56. }  
  57.   
  58. void Node::DeleteNode()  
  59. {  
  60.     if(this->pLChild != NULL)  
  61.     {  
  62.         this->pLChild->DeleteNode();  
  63.     }  
  64.     if(this->pRChild != NULL)  
  65.     {  
  66.         this->pRChild->DeleteNode();  
  67.     }  
  68.     if(this->pParent != NULL)  
  69.     {  
  70.         if(this->pParent->pLChild == this)  
  71.         {  
  72.             this->pParent->pLChild = NULL;  
  73.         }  
  74.         if(this->pParent->pRChild == this)  
  75.         {  
  76.             this->pParent->pRChild = NULL;  
  77.         }  
  78.     }  
  79.     delete this;  
  80. }  
  81.   
  82. void Node::PreorderTraversal()  
  83. {  
  84.     cout<<this->index<<"    "<<this->data<<endl;  
  85.     if(this->pLChild != NULL)  
  86.     {  
  87.         this->pLChild->PreorderTraversal();  
  88.     }  
  89.     if(this->pRChild != NULL)  
  90.     {  
  91.         this->pRChild->PreorderTraversal();  
  92.     }  
  93. }  
  94.   
  95. void Node::InorderTraversal()  
  96. {  
  97.     if(this->pLChild != NULL)  
  98.     {  
  99.         this->pLChild->InorderTraversal();  
  100.     }  
  101.     cout<<this->index<<"    "<<this->data<<endl;  
  102.     if(this->pRChild != NULL)  
  103.     {  
  104.         this->pRChild->InorderTraversal();  
  105.     }     
  106. }     
  107.   
  108. void Node::PostorderTraversal()  
  109. {  
  110.     if(this->pLChild != NULL)  
  111.     {  
  112.         this->pLChild->PostorderTraversal();  
  113.     }  
  114.     if(this->pRChild != NULL)  
  115.     {  
  116.         this->pRChild->PostorderTraversal();  
  117.     }         
  118.     cout<<this->index<<"    "<<this->data<<endl;  
  119. }             

linktree.h

[cpp] view plain copy
  1. /*****************链表实现二叉树*********************/  
  2. #ifndef _LINKTREE_H  
  3. #define _LINKTREE_H  
  4.   
  5. #include "node.h"  
  6.   
  7. class Tree  
  8. {  
  9.     Node* m_pRoot;  
  10. public:  
  11.     Tree();                                                             //创建树  
  12.     ~Tree();                                                            //销毁树  
  13.     Node* SearchNode(int nodeindex);                                    //根据索引寻找结点  
  14.     bool AddNode(int nodeindex, int direction, Node* pNode);            //添加结点  
  15.     bool DeleteNode(int nodeindex, Node* pNode);                        //删除结点  
  16.     void PreorderTraversal();                                           //前序遍历  
  17.     void InorderTraversal();                                            //中序遍历  
  18.     void PostorderTraversal();                                          //后序遍历  
  19. };  
  20.   
  21. #endif  

linktree.cpp

[cpp] view plain copy
  1. /*****************链表实现二叉树*********************/  
  2. #include "linktree.h"  
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. Tree::Tree()        //初始化这棵树  
  8. {  
  9.     m_pRoot = new Node();  
  10. }  
  11.   
  12. Tree::~Tree()  
  13. {  
  14.     //DeleteNode(0, NULL);  
  15.     m_pRoot->DeleteNode();  
  16. }  
  17.   
  18. Node* Tree::SearchNode(int nodeIndex)  
  19. {  
  20.     return m_pRoot->SearchNode(nodeIndex);  
  21. }  
  22.   
  23. bool Tree::AddNode(int nodeIndex, int direction, Node* pNode)  
  24. {  
  25.     Node* temp = SearchNode(nodeIndex);  
  26.     if(temp == NULL)  
  27.     {  
  28.         return false;  
  29.     }  
  30.       
  31.     Node* node = new Node();  
  32.     if(node == NULL)  
  33.     {  
  34.         return false;  
  35.     }  
  36.     node->index = pNode->index;  
  37.     node->data = pNode->data;  
  38.     node->pParent = temp;  
  39.       
  40.     if(direction == 0)  
  41.     {  
  42.         temp->pLChild = node;  
  43.     }  
  44.     else if(direction == 1)  
  45.     {  
  46.         temp->pRChild = node;  
  47.     }  
  48.     return true;  
  49. }  
  50.   
  51. bool Tree::DeleteNode(int nodeIndex, Node* pNode)  
  52. {     
  53.     Node* temp = SearchNode(nodeIndex);  
  54.     if(temp == NULL)  
  55.     {  
  56.         return false;  
  57.     }  
  58.       
  59.     if(pNode != NULL)  
  60.     {  
  61.         pNode->data = temp->data;  
  62.     }  
  63.       
  64.     temp->DeleteNode();  
  65.     return true;  
  66. }  
  67.   
  68. void Tree::PreorderTraversal()  
  69. {  
  70.     m_pRoot->PreorderTraversal();  
  71. }  
  72.   
  73. void Tree::InorderTraversal()  
  74. {  
  75.     m_pRoot->InorderTraversal();  
  76. }     
  77.   
  78. void Tree::PostorderTraversal()  
  79. {  
  80.     m_pRoot->PostorderTraversal();  
  81. }         


main.cpp

[cpp] view plain copy
  1. #include "linktree.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     Tree *tree = new Tree();  
  9.     Node* node1 = new Node;  
  10.     node1->index = 1;  
  11.     node1->data = 'a';  
  12.       
  13.     Node* node2 = new Node;  
  14.     node2->index = 2;  
  15.     node2->data = 'c';  
  16.       
  17.     Node* node3 = new Node;  
  18.     node3->index = 3;  
  19.     node3->data = 'e';  
  20.       
  21.     Node* node4 = new Node;  
  22.     node4->index = 4;  
  23.     node4->data = 'b';  
  24.       
  25.     Node* node5 = new Node;  
  26.     node5->index = 5;  
  27.     node5->data = 'h';  
  28.       
  29.     Node* node6 = new Node;  
  30.     node6->index = 6;  
  31.     node6->data = 'q';  
  32.       
  33.     tree->AddNode(0, 0, node1);  
  34.     tree->AddNode(0, 1, node2);  
  35.     tree->AddNode(1, 0, node3);  
  36.     tree->AddNode(1, 1, node4);  
  37.     tree->AddNode(2, 0, node5);  
  38.     tree->AddNode(2, 1, node6);  
  39.       
  40.     tree->DeleteNode(2, NULL);  
  41.     //tree->PreorderTraversal();  
  42.     //tree->InorderTraversal();        
  43.     tree->PostorderTraversal();  
  44.           
  45.     delete tree;  
  46.       
  47.     return 0;  
  48. }