There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Is it necessary to “delete” node_b if we change node_a to reference node_c instead?
My answer: It depends on what you intend to do and the capabilities of the data structure and language used.
For the purposes of this exercise my guess is no, we don’t have to delete node_b if we solely intend to redirect the node that node_a will point to but node_b will be orphaned in this instance.
Blockquote
Is it necessary to “delete” node_b if we change node_a to reference node_c instead?
I think its necessary in this case. If node_b is not deleted it will be orphaned with no way of accessing the data inside the node. This would mean we are wasting memory. If we kept ‘deleting’ nodes in this manner we would fill up our memory with orphaned nodes that are useless to us.
If we did want to access it again in the future for some reason I think the node would have to have a link to it somewhere.
Python can automatically delete objects with no references with garbage collection algorithms. If an object has no references pointing to it, the object’s memory space can be deallocated (and therefore it is “deleted”). I find this article and this article about garbage collection using CPython helpful, maybe you will too.
There’s a difference between the simple reference counting and some of the smarter garbage collection. If you removed every reference to an object then the memory is deallocated for you and that’s just standard Python. There are times when you’d want to be a bit more cautious though. A basic introduction to this is to consider circular references e.g. two nodes that have been orphaned (no external references) but still refer to each other. This won’t be caught by reference counting alone (both articles cover this issue).
A simple example like that would instead be caught by the gc garbage collector but you can cause memory leaks with lots of cyclical references, especially with customised destructors which the garbage collector does not deal with. You can read up on it a bit if you like, from a quick search but there may be better examples around-
So normally you don’t need to worry, reference counting is in-built and straightforward but under certain circumstances you may need to take more care.
I think in this case you have to assume there’s a reference or head node kept for node_a such that node_a is not orphaned in this example. If you were to remove this reference (not displayed in the diagram) then you’d be quite right node_a would also be orphaned and you’d lose access to all the nodes in this example.
So a node is considered orphaned at the moment it has no input link? (no parent)
Is it necessary to “delete” node_b if we change node_a to reference node_c instead? In python since memory managment is automatic, I think it should not something we should take care. But in term of code readability, it may be a good habit to do.
What I made out of it is that node_b is orphaned because its link from node_a was removed. But even node_a doesn’t have any links from any node to itself, but it is not orphaned. Why? Couldn’t node_b as node_a act as a beginner code. Two nodes can point to a single node, right?
A node is a basic building block for many data structures. You are correct that if we’ve defined three nodes (node_a, node_b, node_c), link them together, and then remove a link to one of them, that they all three still exist. This lesson would make more sense if it mentioned that what we had built by linking them together was a linked list. When you get to the linked list section (which is next - I believe), the concept of orphaned nodes will be much more clear. The node with no other node pointing to it is orphaned from the structure of linked nodes, but could still be referenced directly if necessary.
hi, i am new to datastructures.
here it says we need to delete node b after linking node a to node c. But I dont understand how to access node b, if there is no link to it? how to delete it , if we cant access it?
Where does it say that? You may have misread the instruction:
It’s a question asking if it is necessary to delete node_b. If the only reference to node_b was the link from node_a, then we have no way to access it, and it is automatically removed. If, on the other hand, we had assigned variables to each of the nodes prior to linking them together, we would still have access to the node, and may want to manually delete it.
In this case node_b will not be garbage collected because it still points to node_c and node_a is pointing to node_c.
If we wanted it to be garbage collected then we have to remove the pointer from node_b to node_c too.
So yes it’s necessary in this case to remove the pointer from node_b to node_c also inorder to be garbage collected.
Python’s basic method of freeing up memory is reference counting. If nothing references an object there is no route to access that object and it can be safely deleted. Where node_bpoints to is unimportant as as reference counting only cares about whether or not there are any references TO node_b.
Not every language implements reference counting though so it is worth being careful about in many cases.
I’ve been writing programs in JS and worked with arrays, objects, data in general for several years but I don’t really understand what this lesson is talking about in terms of what nodes… are.
Like, are nodes a paradigm/concept that we’ve been unknowingly using this whole time by other names? In what context(in JavaScript) would I ever have “linked” a node to another node, or oprhaned a node and lost access to its data, or ended a node with a null value to denote the path ending there? What would that look like on a lower level than this lesson uses in its diagrams? Basically, is it something I’ve already been doing all along, or a whole new approach I’ll be learning from scratch?
I had the exact same question in my mind, so the only thing that I could remember was the JS lesson covering Nodes and Elements in the DOM. When you manipulate the DOM using JS, you work with nodes to update the content, structure, or style of a web page.
// Example of updating content using DOM nodes
let element = document.getElementById(‘exampleElement’);
element.textContent = ‘New Content’;