# What Is Degree of Node in Data Structure With Example?

//

Larry Thompson

The degree of a node in a data structure refers to the number of edges connected to that particular node. In other words, it represents the number of immediate neighbors a node has. Understanding the concept of the degree of a node is essential in various data structures such as graphs and trees.

## Degree of Node in Graphs

In graph theory, a graph consists of a set of vertices or nodes connected by edges. Each edge represents a relationship or connection between two nodes. The degree of a node in a graph is simply the count of edges that are incident to that particular node.

For example, consider a simple undirected graph with four nodes labeled A, B, C, and D:

```       B
/ \
/   \
A-----C
\   /
\ /
D
```

In this graph:

• Node A has degree 2 because it is connected to B and C.
• Node B has degree 2 because it is connected to A and C.
• Node C has degree 3 because it is connected to A, B, and D.
• Node D has degree 2 because it is connected to C.

### Degree Sequence

The sequence of degrees for all the nodes in a graph is known as the degree sequence. For the above example, the degree sequence would be {2, 2, 3, 2}.

Note: In an undirected graph, the sum of all degrees will be twice the number of edges since each edge contributes to two degrees (one for each endpoint).

## Degree of Node in Trees

In the context of trees, the degree of a node refers to the number of children it has. In a tree, each node can have at most one parent but can have multiple child nodes.

Consider a simple tree with five nodes labeled A, B, C, D, and E:

```        A
/ \
B   C
/ \
D   E
```

In this tree:

• Node A has degree 2 because it has two children (B and C).
• Node B has degree 2 because it has two children (D and E).
• Nodes C, D, and E have degree 0 since they are leaf nodes with no children.

### Degree in Binary Trees

In binary trees, each node can have at most two children: a left child and a right child. Therefore, the degree of each node in a binary tree is either 0 (leaf node), 1 (one child), or 2 (two children).

## Conclusion

The degree of a node in data structures such as graphs and trees provides valuable information about the connectivity and structure of these data structures. By understanding the concept of the degree of a node, you can analyze graphs more effectively and make informed decisions based on their properties.