Implementing Nodes : Most basic building blocks of data structures (day-3)
Coding Nodes in Python from zero (Day-3)
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
.
Try it yourself:
- Create a new class,
Node
. Add an.__init__()
method in theNode
class that takes avalue
and an optionallink_node
(default should beNone
). These should be saved to the correspondingself
properties (self.value
andself.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 Node
class, 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 variableai
- second has a value of
"ai career master"
and be assigned tomukesh
- the third has a value of
"data science specialist"
and be assigned tomanral
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
ofmanral
manral
alink_node
ofmukesh
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
tomukesh
‘svalue
? - 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:
- 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
usingn.set_link_node(None)
- Change the link on
a
to point tot
usinga.set_link_node(t)
** - Change the link on
a
to point tot
usingt.set_link_node(a)
- Delete the link on
a
that points ton
usinga.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:
- 👏 Clap for the story (500 claps)
- For all new updates: Subscribe
- 📰 View more content on my medium profile
- 🔔 Follow Me: LinkedIn | Youtube | GitHub | Website
— — — — — — — — — —
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