zoukankan      html  css  js  c++  java
  • 第五十二课 树的存储结构与实现

    GTree是通用树结构。

    每个节点都包含了指向父节点的指针。

    添加GTreeNode.h文件:

     1 #ifndef GTREENODE_H
     2 #define GTREENODE_H
     3 
     4 #include "TreeNode.h"
     5 #include "LinkList.h"
     6 
     7 namespace DTLib
     8 {
     9 
    10 template < typename T >
    11 class GTreeNode : public TreeNode<T>
    12 {
    13 public:
    14     LinkList<GTreeNode<T>*> child;
    15 
    16 };
    17 
    18 }
    19 
    20 #endif // GTREENODE_H

    添加GTree.h文件:

     1 #ifndef GTREE_H
     2 #define GTREE_H
     3 
     4 #include "Tree.h"
     5 #include "GTreeNode.h"
     6 
     7 namespace DTLib
     8 {
     9 
    10 template < typename T >
    11 class GTree : public Tree<T>
    12 {
    13 public:
    14     bool insert(TreeNode<T>* node)
    15     {
    16         bool ret = true;
    17 
    18         return ret;
    19     }
    20 
    21     bool insert(const T& value, TreeNode<T>* parent)
    22     {
    23         bool ret = true;
    24 
    25         return ret;
    26     }
    27 
    28     //删除的节点的子节点我们还需要处理,因此要返回删除节点的指针,这样有机会对里面的元素做进一步操作
    29     SharedPointer< Tree<T> > remove(const T& value)
    30     {
    31         return NULL;
    32     }
    33 
    34     SharedPointer< Tree<T> > remove(TreeNode<T>* node)
    35     {
    36         return NULL;
    37     }
    38 
    39     GTreeNode<T>* find(const T& value) const  // 返回GTreeNode,赋值兼容性
    40     {
    41         return NULL;
    42     }
    43 
    44     GTreeNode<T>* find(TreeNode<T>* node) const
    45     {
    46         return NULL;
    47     }
    48 
    49     GTreeNode<T>* root() const
    50     {
    51         return dynamic_cast<GTreeNode<T>*>(this->m_root);
    52     }
    53 
    54     int degree() const
    55     {
    56         return 0;
    57     }
    58     int count() const
    59     {
    60         return 0;
    61     }
    62 
    63     int height() const
    64     {
    65         return 0;
    66     }
    67 
    68     void clear()
    69     {
    70         this->m_root = NULL;
    71     }
    72 
    73     ~GTree()
    74     {
    75         clear();
    76     }
    77 };
    78 
    79 }
    80 
    81 #endif // GTREE_H

    从上往下看是非线性的,从下往上看是线性的,也就是类似于链表结构,加入父节点指针后,我们就可以用一些链表的知识来处理树了。

    在工程中这种方式使用很广泛。

  • 相关阅读:
    线程安全(1)--demo1
    java--构造器与static
    I/O---读取txt文件----demo
    阳光餐厅--oracle---建表---danrong
    定位程序问题出现的原因工具-jstack
    守护线程
    线程的交互:互斥与同步
    正确的停止java中的线程
    使用GSON来生成JSON数据
    使用JSONObject类来生成json格式的数据
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9690773.html
Copyright © 2011-2022 走看看