What's the difference between the data structure Tree and Graph?

后端 未结 10 1807
醉话见心
醉话见心 2021-01-29 17:35

Academically speaking, what\'s the essential difference between the data structure Tree and Graph? And how about the tree based search and Graph based search?

相关标签:
10条回答
  • 2021-01-29 17:59

    Trees are obvious: they're recursive data structures consisting of nodes with children.

    Map (aka dictionary) are key/value pairs. Give a map a key and it will return the associated value.

    Maps can be implemented using trees, I hope you don't find that confusing.

    UPDATE: Confusing "graph" for "map" is very confusing.

    Graphs are more complex than trees. Trees imply recursive parent/child relationships. There are natural ways to traverse a tree: depth-first, breadth-first, level-order, etc.

    Graphs can have uni-directional or bi-directional paths between nodes, be cyclic or acyclic, etc. I would consider graphs to be more complex.

    I think a cursory search in any decent data structures text (e.g. "Algorithms Design Manual") would give more and better information than any number of SO answers. I would recommend that you not take the passive route and start doing some research for yourself.

    0 讨论(0)
  • 2021-01-29 18:06

    A tree is a digraph such that:

    a) with edge directions removed, it is connected and acyclic

    1. You can remove either the assumption that it is acyclic
    2. If it is finite, you can alternatively remove the assumption that it is connected

    b) every vertex but one, the root, has indegree 1

    c) the root has indegree 0

    1. If there are only finitely many nodes, you can remove either the assumption that the root has indegree 0 or the assumption that the nodes other than the root have degree 1

    Reference: http://www.cs.cornell.edu/courses/cs2800/2016sp/lectures/lec27-29-graphtheory.pdf

    0 讨论(0)
  • 2021-01-29 18:08

    The other answers are useful, but they're missing the properties of each:

    Graph

    Undirected graph, image source: Wikipedia

    Directed graph, image source: Wikipedia

    • Consists of a set of vertices (or nodes) and a set of edges connecting some or all of them
    • Any edge can connect any two vertices that aren't already connected by an identical edge (in the same direction, in the case of a directed graph)
    • Doesn't have to be connected (the edges don't have to connect all vertices together): a single graph can consist of a few disconnected sets of vertices
    • Could be directed or undirected (which would apply to all edges in the graph)
      As per Wikipedia:

      For example, if the vertices represent people at a party, and there is an edge between two people if they shake hands, then this graph is undirected because any person A can shake hands with a person B only if B also shakes hands with A. In contrast, if any edge from a person A to a person B corresponds to A admiring B, then this graph is directed, because admiration is not necessarily reciprocated.

    Tree

    Image source: Wikipedia

    • A type of graph
    • Vertices are more commonly called "nodes"
    • Edges are directed and represent an "is child of" (or "is parent of") relationship
    • Each node (except the root node) has exactly one parent (and zero or more children)
    • Has exactly one "root" node (if the tree has at least one node), which is a node without a parent
    • Has to be connected
    • Is acyclic, meaning it has no cycles: "a cycle is a path [AKA sequence] of edges and vertices wherein a vertex is reachable from itself"

    There is some overlap in the above properties. Specifically, the last two properties are implied by the rest of the properties. But all of them are worth noting nonetheless.

    0 讨论(0)
  • 2021-01-29 18:10

    In tree, each node (except the root node) has exactly one predecessor node and one or two successor nodes. It can be traversed by using In-order, Pre-order, Post-order, and Breadth First traversals​. Tree is a special kind of graph that has no cycle so that is known as DAG (Directed Acyclic Graph). Tree is a hierarchical model.

    In graph, each node has one or more predecessor nodes and successor nodes. The graph is traversed by using Depth First Search (DFS) and Breadth First Search (BFS) algorithms. Graph has cycle so it is more complex than tree. Graph is a network model. There are two kinds of graph: directed graphs and undirected graphs.

    0 讨论(0)
  • 2021-01-29 18:14

    A Tree is just a restricted form of a Graph.

    Trees have direction (parent / child relationships) and don't contain cycles. They fit with in the category of Directed Acyclic Graphs (or a DAG). So Trees are DAGs with the restriction that a child can only have one parent.

    One thing that is important to point out, Trees aren't a recursive data structure. They can not be implemented as a recursive data structure because of the above restrictions. But any DAG implementation, which are generally not recursive, can also be used. My preferred Tree implementation is a centralized map representation and is non recursive.

    Graphs are generally searched breadth first or depth first. The same applies to Tree.

    0 讨论(0)
  • 2021-01-29 18:14

    Tree is basically undirected graph which not contain cycle,so we can say that tree is more restricted form of graph. However tree and graph have different application to implement various algorithm in programming. For example graph can be used for model road map and tree can be used for implement any hierarchical data structure.

    0 讨论(0)
提交回复
热议问题