search
HomeCommon ProblemData Structures: What is the difference between a graph and a tree

Charts and trees are both the most common nonlinear data structures, so what are the differences between them? The following article will explain the difference between charts and trees. I hope it will be helpful to everyone.

Data Structures: What is the difference between a graph and a tree

Chart

The chart is composed of two sets V and E, where V is finite is a non-empty vertex set, and E is a finite non-empty edge set. It has the following properties:

1. A vertex represents a node in the graph and can be connected to any number of other vertices using edges.

2. Two adjacent vertices are connected by an edge. The edge can be bidirectional or directional; the edge can also be weighted.

3. Any graph can be expressed as: G = {V, E}.

For example:

Data Structures: What is the difference between a graph and a tree

Then: G = {{V1, V2, V3, V4, V5}, {E1, E2, E3, E4, E5 , E6, E7}}

Tree

The tree is a finite set K containing n (n>0) nodes and has the following properties :

1. There is a designated node at the top of the tree, which is called the root of the tree.

2. The remaining data items are divided into disjoint subsets, called subtrees.

3. The height of the tree expands toward the bottom.

4. The tree must be connected, which means there must be a path from one root to all other nodes.

5. It does not contain any loops.

6. The tree has n-1 edges.

For example:

Data Structures: What is the difference between a graph and a tree

Difference between chart and tree

Graph

1. Each node in the graph can have any number of edges, and the edges can be one-way or two-way.

2. There is no concept of a root node named root in the graph.

3. Graphs can have loops and self-loops

4. In a graph, there is no predefined number of edges, it depends on the graph.

5. The graph is the network model structure.

Tree

1. A regular tree consists of nodes with any number of child nodes; but in the case of a binary tree, each node can have up to two child nodes. There is only one edge between any two nodes.

2. There is a unique node named root in the tree.

3. The tree cannot have loops or self-loops

4. The tree can have n-1 edges.

5. The tree is a hierarchical structure.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of Data Structures: What is the difference between a graph and a tree. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor