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

Manralai
5 min readAug 5, 2024

--

Coding Nodes in Python from zero (Day-3)

For all new updates: Subscribe

Nodes Python Implementation

Now that you have an understanding of what nodes are (from Day-3 learning), let’s see one way they can be implemented using Python.

We will use a basic node that contains data and one link to another node. The node’s data will be specified when creating the node and immutable (can’t be updated). The link will be optional at initialisation and can be updated.

Remember that at the end of a node path, the link to the next node is null because there are no more nodes left. In Python, this means it will be set to None.

For all new updates: Subscribe

Try it yourself:

  1. Create a new class, Node. Add an .__init__() method in the Node class that takes a value and an optional link_node (default should be None). These should be saved to the corresponding self properties (self.value and self.link_node).
# creating Node class

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

2. We need methods to access the data and link within the node. For this, we will use two getters, .get_value() and .get_link_node().

These should each return their corresponding value on the self object.

## -- Implementing .get_value() and .get_link_node() getter in the Node class

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

# Defining get_value and get_link_node methods below:
def get_value(self):
return self.value

def get_link_node(self):
return self.link_node

3. See above code, We are only allowing the value of the node to be set upon creation. However, we want to allow updating the link of the node. For this, we will use a setter to modify the self.link_node attribute.

The method should be called .set_link_node() and should take link_node as an argument. It should then update the self.link_node attribute as appropriate.

For all new updates: Subscribe

## -- Implementing the .set_link_node() setter in the Node class

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

def get_value(self):
return self.value

def get_link_node(self):
return self.link_node

# Defining set_link_node method below:
def set_link_node(self,link_node):
self.link_node = link_node

4. Outside of Nodeclass, now I will instantiate three nodes. No one will have an argument for link_node:

  • first has a value of "manralai" and be assigned to a variable ai
  • second has a value of "ai career master" and be assigned to mukesh
  • the third has a value of "data science specialist" and be assigned to manral
class Node:
def __init__(self, value, link_node=None):
self.value = value
self.link_node = link_node

def set_link_node(self, link_node):
self.link_node = link_node

def get_link_node(self):
return self.link_node

def get_value(self):
return self.value

# Adding newcode below:
ai = Node("manralai")
mukesh = Node("ai career master")
manral = Node("data science specialist")

ai can keep track of manral and manral can keep up with mukesh. mukesh can’t keep track of anything though.

I will use .set_link_node() method to give:

  • ai alink_node of manral
  • manral alink_node of mukesh

For all new updates: Subscribe

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

def set_link_node(self, link_node):
self.link_node = link_node

def get_link_node(self):
return self.link_node

def get_value(self):
return self.value


ai = Node("manralai")
mukesh = Node("ai career master")
manral = Node("data science specialist")

# Adding newcode below:
ai.set_link_node(manral)
manral.set_link_node(mukesh)

Creating two new variables, manral_data, and mukesh_data. Use both getter methods to get manral‘s value from ai and get mukesh‘s value from manral. Print manral_data and mukesh_data to the console to see the results!

Take a moment to consider:

  • How would you get mukesh‘s value?
  • How could you get from ai to mukesh‘s value?
  • How do you think nodes could be helpful for keeping track of and storing information?

For all new updates: Subscribe

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

def set_link_node(self, link_node):
self.link_node = link_node

def get_link_node(self):
return self.link_node

def get_value(self):
return self.value


ai = Node("manralai")
mukesh = Node("ai career master")
manral = Node("data science specialist")

ai.set_link_node(manral)
manral.set_link_node(mukesh)

# Adding newcode below:
manral_data = ai.get_link_node().get_value()
mukesh_data = manral.get_link_node().get_value()

print(manral_data)
print(mukesh_data)

Quiz:

  1. Which two features do most nodes contain?
  • Data and an array containing other nodes
  • Data and null pointers
  • Data and links to other nodes **
  • Arrays and pointers to other nodes

2. Consider the following nodes and links: a -> n -> t. If you want to remove node n, but preserve node t, what are the steps you would take?

  • Remove the link on n using n.set_link_node(None)
  • Change the link on a to point to t using a.set_link_node(t) **
  • Change the link on a to point to t using t.set_link_node(a)
  • Delete the link on a that points to n using a.set_link_node(None)

3. A node containing only null pointers indicates what?

  • You are at the end of the node path you were following **
  • The node has no data
  • No other nodes link to this node
  • There are no other nodes in the data structure

4. Which of the following methods implemented in the Node class are required to establish a Node class with an accessible but immutable value?

For all new updates: Subscribe

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

def get_value(self):
return self.value

def get_link_node(self):
return self.link_node

def set_link_node(self, link_node):
self.link_node = link_node

def set_value(self, value):
self.value = value

def increment_value(self):
self.value = self.value + 1
  • .__init__(), .get_value(), .get_link_node(), .set_link_node(), and .increment_value()
  • .__init__(), .get_link_node(), and .set_link_node()
  • These methods do not provide a method to access the value attribute
  • .__init__(), .get_value(), .get_link_node(), .set_value() and .set_link_node()
  • .__init__(), .get_value(), .get_link_node(), and .set_link_node() **

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)