美文网首页
数据结构:树的C++实现

数据结构:树的C++实现

作者: 胡博要毕业 | 来源:发表于2018-10-08 19:31 被阅读0次

一、树

1、节点的定义

template<class T>
struct Tree_Node {
    T  _val;
    vector<Tree_Node<T>*> _children;

    Node() {}    

    Node(const T& val, const vector<Tree_Node<T>*>& children) {
        _val = val;
        _children = children;
    }
};

2、树的抽象数据结构

template <class T>
class Tree
{
    Tree()
    :_root(NULL)
    {}

    void Clear();      
    Tree_Node<T>* Find(T value);
    bool Add_Node(T parent, T child);
    int size();
    int Height();
    int Leaves();
    bool empty();

private:
    Tree_Node<T> *Find_R(Tree_Node<T>*root, T value);
    int Tree::Height_R(Tree_Node<T>* root);
    int Tree::Size_R(Tree_Node<T>* root);
    int Tree::Leaves_R(Tree_Node<T>* root);
private:
    Tree_Node<T>* _root;
};

3、树的基本操作

3.1、 插入

template <class T>
bool Tree::Add_Node(T parent, T child)
{
    if( _root == NULL )
    {  /*设置为根节点*/
          _root = new Tree_Node(child, NULL);
          return true;
    }
    
    /*寻找值 = parent的节点*/
    Tree_Node* cur = Find(parent);
    Tree_Node* child = new Tree_Node(child, NULL);

    cur->_children.push_back(child);
}

3.2、查询

template <class T>
Tree_Node<T>* Tree::Find( T value )
{
     return Find_R(_root, value);
}

template <class T>
Tree_Node<T>* Tree::Find_R(Tree_Node<T>* root, T value)
{
   if ( root->value == value )
       return root;
   for(auto &child:  root->children)
   {
       Find_R(child, value);
   }
   return NULL;
}

3.3、求树高、节点个数、叶子个数、判断空

template <class T>
int Tree::Height()
{
    Height_R(_root);
}

template <class T>
int Tree::Height_R(Tree_Node<T>* root)
{
    if( root == NULL )
        return 0;
    int height = 0;

    for(auto &child : root -> _children)
    {
        height = max(height, Height_R(child));
    }
    return height + 1;
}

template <class T>
int Tree::Size()
{
    return Size_R(_root);
}

template <class T>
int Tree::Size_R( Tree_Node<T> root )
{
    if( root == NULL )
        return 0;
    int size = 0;
    for( auto &child : root -> _children )
    {
        size += Size_R(child);
    }
    return size + 1;
}

template <class T>
bool Tree::Empty()
{
    if( _root == NULL )
        return true;
    else
        return false;
}

template <class T>
int Tree::Leaves()
{
    return Leaves_R(_root);
}

template <class T>
int Tree::Leaves_R(Tree_Node<T>* root)
{
    if(root == NULL)
        return 0;
    if( root -> _children.size() == 0 )
        return 1;

    int leaves = 0;

    for( auto &child : root -> _children )
    {
        leaves += Leaves_R(child);
    }
}

相关文章

  • c++ 实现队列

    相关资料: 用C++实现一个队列 数据结构代码实现之队列的链表实现(C/C++)

  • 数据结构:树的C++实现

    一、树 1、节点的定义 2、树的抽象数据结构 3、树的基本操作 3.1、 插入 3.2、查询 3.3、求树高、节点...

  • php实现二叉树

    二叉树是数据结构中不可忽略的部分,但是相关的书籍上很少有用php来实现二叉树的,下面是我用php参考C++实现二叉...

  • 414. Third Maximum Number

    此题主要是C++中set的使用。set关联式容器: 实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调...

  • [LeetCode] 226. Invert Binary Tr

    我们学过树的前序遍历(DFS)和层次遍历(BFS),在邓俊辉《数据结构(C++语言版)》的前序遍历和层次遍历的实现...

  • OC对象

    OC的本质 oc代码,底层是由c/c++实现 Objective-C的面向对象都是基于C\C++的数据结构实现的 ...

  • Stack - C语言(LeetCode107)

    前言 在实现LeetCode 107题时,发现它用到了栈、队列和树这三种数据结构,使用C++、Java或其它高级语...

  • iOS 对象和类的本质

    前言 我们编写的OC代码,其实底层实现都是C/C++代码。所以,对象和类也都是基于C/C++的数据结构实现的。 所...

  • OC 对象的本质

    OC的本质 我们平时写的OC代码的底层都是c/c++代码实现的 OC的面向对象都是基于c/c++的数据结构实现的 ...

  • Objective-C内存结构源码解读

    我们平时编写的OC代码,底层实现其实都是C\C++代码,所以OC的对象和类主要是基于C和C++的数据结构实现的,这...

网友评论

      本文标题:数据结构:树的C++实现

      本文链接:https://www.haomeiwen.com/subject/ixgjaftx.html