【数据结构】51_树的定义与操做

树的定义

  • 树是一种非线性数据结构
  • 树是由 n(n>=0) 个节点组成的有限集合node

    • 若是 n = 0, 称为空树;
    • 若是 n > 0, 则:编程

      • 有一个特定的称之为根(root)的节点
      • 根节点只有直接后继,但没有直接前驱
      • 除根之外的其它节点划分为m(m>=0)个互不相交的有限集合T0,T1,...,Tm-1,每一个集合又是一颗树,而且称之为根的子树(sub tree)

树的示例

image.png

树中度的概念

  • 树的节点包含一个数据及若干指向子树的分支
  • 节点拥有的子树数目称为节点的度数据结构

    • 度为 0 的节点称为叶节点
    • 度不为 0 的节点称为分支节点
  • 树的度定义为全部节点中度的最大值

树的度的示例

度为 3 的树

image.png

树中的前驱和后继

  • 节点的直接后继称为该节点的孩子spa

    • 相应的,该节点称为孩子的双亲
  • 节点的孩子的孩子的......称为该节点的子孙指针

    • 相应的,该节点称为子孙的祖先
  • 同一个双亲的孩子之间互称为兄弟

image.png

数中的节点层次

  • 根为第 1 层
  • 根的孩子为第 2 层
  • ...

image.png

树中节点的最大层次称为树的深度或高度code

树的有序性

若是树中节点的各子树从左到右是有次序的,子树之间不能互换位置,则称为该树为有序树,不然为无序树。blog

image.png

森林的概念

  • 森林是由 n(n>=0) 颗互不相交的树组成的集合

image.png

树的操做

  • 将元素插入数中
  • 将元素从树中删除
  • 在树中查找元素
  • 获取树的节点数
  • 获取数的高度
  • 获取数的度
  • 清空数的元素

。。。rem

树的数据结构

数在程序中表现为一种特殊的数据类型
template <typename T>
class Tree : public Object
{
public:
    Tree() { m_root = NULL; }
    virtual bool insert(TreeNode<T> *node) = 0;
    virtual bool insert(const T &value, TreeNode<T> *parent) = 0;
    virtual SharedPointer<Tree<T>> remove(const T &value) = 0;
    virtual SharedPointer<Tree<T>> remove(TreeNode<T> *node) = 0;
    virtual TreeNode<T>* find(const T &value) const = 0;
    virtual TreeNode<T>* find(TreeNode<T> *node) const = 0;
    virtual TreeNode<T>* root() const = 0;
    virtual int degree() const = 0;
    virtual int count() const = 0;
    virtual int height() const = 0;
    virtual void clear() = 0;
    
protected:
    TreeNode<T> *m_root;
};

节点的数据结构

树中的节点也表现为一种特殊的数据类型
template <typename T>
class TreeNode : public Object
{
public:
    T value;
    TreeNode<T> *parent;
    
    TreeNode()
    {
        parent = NULL;
    }
    
    virtual ~TreeNode() = 0;
};

树与节点的类关系

image.png

编程实验:树与节点抽象类的建立

文件:TreeNode.hit

#ifndef TREENODE_H
#define TREENODE_H

#include "Object.h"

namespace DTLib
{

template <typename T>
class TreeNode : public Object
{
public:
    T value;
    TreeNode<T> *parent = nullptr;

    virtual ~TreeNode() = 0;
};

template <typename T>
TreeNode<T>::~TreeNode()
{

}

}

#endif // TREENODE_H

文件:Tree.hclass

#ifndef TREE_H
#define TREE_H

#include "Object.h"
#include "TreeNode.h"
#include "SharedPointer.h"

namespace DTLib
{

template <typename T>
class Tree : public Object
{
public:
    Tree() = default;
    virtual bool insert(TreeNode<T> *node) = 0;
    virtual bool insert(const T &value, TreeNode<T> *parent) = 0;
    virtual SharedPointer<Tree<T>> remove(const T &value) = 0;
    virtual SharedPointer<Tree<T>> remove(TreeNode<T> *node) = 0;
    virtual TreeNode<T>* find(const T &value) const = 0;
    virtual TreeNode<T>* find(TreeNode<T> *node) const = 0;
    virtual TreeNode<T>* root() const = 0;
    virtual int degree() const = 0;
    virtual int count() const = 0;
    virtual int height() const = 0;
    virtual void clear() = 0;

protected:
    TreeNode<T> *m_root = nullptr;
};

}

#endif // TREE_H

小结

  • 树是一种特殊的数据结构
  • 节点拥有惟一前驱(父节点)和若干后继(子节点)
  • 树的节点包含一个数据及若干指向其它节点的指针
  • 树与节点在程序中表现为特殊的数据类型

以上内容整理于狄泰软件学院系列课程,请你们保护原创!

相关文章
相关标签/搜索