📜  图和树的区别

📅  最后修改于: 2021-09-12 11:35:31             🧑  作者: Mango

图表

图是两个集合 V 和 E 的集合,其中 V 是有限的非空顶点集,E 是有限的非空边集。

  • 顶点只不过是图中的节点。
  • 两个相邻的顶点由边连接。
  • 任何图都表示为 G = {V, E}。

例如:

G = {{V 1 , V 2 , V 3 , V 4 , V 5 , V 6 }, {E 1 , E 2 , E 3 , E 4 , E 5 , E 6 , E 7 }}

树 :

一棵树是一个或多个节点的有限集合,使得 –

  1. 有一个特别指定的节点称为根。
  2. 剩余的节点被划分为 n>=0 个不相交的集合 T 1 , T 2 , T 3 , …, T n
    其中 T 1 , T 2 , T 3 , …, T n称为根的子树。

树的概念如下图所示。

图 vs 树

No. Graph Tree
1 Graph is a non-linear data structure. Tree is a non-linear data structure.
2 It is a collection of vertices/nodes and edges. It is a collection of nodes and edges.
3 Each node can have any number of edges. General trees consist of the nodes having any number of child nodes. But in case of binary trees every node can have at the most two child nodes.
4 There is no unique node called root in graph. There is a unique node called root in trees.
5 A cycle can be formed. There will not be any cycle.
6 Applications: For finding shortest path in networking graph is used. Applications: For game trees, decision trees, the tree is used.

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。