Understanding Nodes in Data Structures
What is a node?
Nodes are the fundamental building blocks of many computer science data structures. They form the basis for linked lists, stacks, queues, trees, and more. In this article, we’ll discuss what a node is as well as how nodes link to other nodes.
An individual node contains data and links to other nodes. Each data structure adds additional constraints or behavior to these features to create the desired structure.
Consider the node depicted below. This node (node_a
) contains a piece of data (the number 5
) and a link to another node (node_b
):
Linear Data Structures
Learn about virtualization of computer memory by building the fundamental data structures of computer science: lists, stacks, and queues.Try it for freeHow to implement nodes?
The data contained within a node can be a variety of types, depending on the language you are using. In the previous example, it was an integer (the number 5
), but it could be a string ("five"
), decimal (5.1
), an array ([5,3,4]
) or nothing (null
).
The link or links within the node are sometimes referred to as pointers. This is because they “point” to another node.
Typically, data structures implement nodes with one or more links. If these links are null
, it denotes that you have reached the end of the particular node or link path you were previously following.
A variety of node implementations are depicted in the following diagram. Examine the types of data and how some of the nodes are linked:
![Types of nodes]
How nodes connect in data structures
Often, due to the data structure, nodes may only be linked to a single other node. This makes it very important to consider how you implement modifying or removing nodes from a data structure.
If you inadvertently remove the single link to a node, that node’s data and any linked nodes could be “lost” to your application. When this happens to a node, it is called an orphaned node.
Examine the nodes in the diagram:
![Orphaned Node Animation]
node_c
is only linked to node_b
. If you would like to remove node_b
but not node_c
, you can’t simply delete the link from node_a
to node_b
.
The most straightforward method to preserve node_c
would be to change the link in node_a
to point to node_c
instead of node_b
. However, some data structures may handle this in a different manner.
Conclusion
Let’s take a minute to review what we’ve covered about nodes in this article:
- Nodes contain data, which can be a variety of data types.
- They also contain links to other nodes. If a node has no links, or they are all
null
, you have reached the end of the path you were following. - Nodes can be orphaned if there are no existing links to them.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
What is Node?
Learn about Node.js, a JavaScript runtime for building server-side or desktop applications. - Article
Setting Up Node Locally
Learn how to download Node on your local machine, so you can use the powerful Node.js runtime from your own computer. - Article
Using ChatGPT for Pair Programming
Learn how to use ChatGPT for pair programming with the driver-navigator approach. Explore BFS and DFS in Python with tips for collaboration and troubleshooting.
Learn more on Codecademy
- Course
Linear Data Structures
Learn about virtualization of computer memory by building the fundamental data structures of computer science: lists, stacks, and queues.With CertificateIntermediate4 hours - Free course
Introduction to Algorithms and Linear Data Structures in Swift
Learn about the fundamental data structures of computer science and implement them in the Swift programming language.Intermediate5 hours - Skill path
Pass the Technical Interview with JavaScript
Learn about the computer science concepts of data structures and algorithms and build implementations from scratch in modern JavaScript.Includes 8 CoursesWith CertificateIntermediate13 hours