Nodes : Most basic building blocks of data structures(day-2)

Manralai
5 min readAug 4, 2024

--

What is a Node? It’s implementation!! Node Linking!!(day-2)

For all new updates: Subscribe
If you don’t want to read, please like this article and continue with the above video.

What are nodes?

Nodes are the fundamental building blocks of many data structures. They form the basis for linked lists, stacks, queues, trees, and more.

Let’s explore 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 behaviours to these features to create the desired structure.

For all new updates: Subscribe

Consider the node depicted above. This node (node_a) contains a piece of data (the number 5) and a link to another node (node_b):

Node implementations

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:

For all new updates: Subscribe

For all new updates: Subscribe

Node linking

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:

For all new updates: Subscribe

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.

For all new updates: Subscribe

Recap

Let’s take a minute to review what we’ve covered about nodes in this article:

  • Nodes contain data, there 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.

Node: An individual part of a larger data structure

Nodes are a basic data structure which contain data and one or more links to other nodes. Nodes can be used to represent a tree structure or a linked list. In such structures where nodes are used, it is possible to traverse from one node to another node.

For all new updates: Subscribe

Orphaned nodes

Nodes that have no links pointing to them except for the head node, are considered “orphaned.” In the illustration, if the nodes a4 and a5 are removed, they will be orphaned.

For all new updates: Subscribe

Null node link

Data structures containing nodes have typically two bits of information stored in a node: data and link to next node.

The first part is a value and the second part is an address of sorts pointing to the next node. In this way, a system of nodes is created. A NULL value in the link part of a node’s info denotes that the path or data structure contains no further nodes.

For all new updates: Subscribe

Python Node implementation

A Node is a data structure that stores a value that can be of any data type and has a pointer to another node. The implementation of a Node class in a programming language such as Python, should have methods to get the value that is stored in the Node, to get the next node, and to set a link to the next node.

For all new updates: Subscribe


class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node

def set_next_node(self, next_node):
self.next_node = next_node

def get_next_node(self):
return self.next_node

def get_value(self):
return self.value

In next write-up I will cover: How to implement above code with deep analysis and in depth explanation!! (Day-3)

I will add more here if I will get 500 claps.

— — — — — — — — — —

If you like the article and would like to support me, make sure to:

— — — — — — — — — —

Day-1: Why Data Structure and Algorithm

Day-2: Nodes : Most basic building blocks of data structures

Day-3: Implementing Nodes : Most basic building blocks of data structures

Day-4: Data Structures: The Building Blocks of Coding Mastery

--

--

Manralai
Manralai

Written by Manralai

Level Up Your AI Game: Subscribe now and start your AI journey!

Responses (1)