Creating and visualizing a Linked List - data-structures

I'm still new to coding and I'm trying to learn how to create a linked list. What does this part mean? I can't seem to visualize this in my head.
static class Node {
int data;
Node next;
Node(int d){
data = d;
next = null;}
}

So this is a simple visual of a linked list. Your code represents a node class, so thats just the framework for a node. But you can think of that code representing one node, so it could represent the fourth node in this diagram. Thus, its data value would be {D} and its next value would be null.
In a linked list, the next node object it represented by the next variable. So if the node you are looking at is the second node, then your next variable will be the third node.

Related

The repetitive step in delete a specific node in a Linked List

//Deletes data given from the linked list
public void deleteByValue(T data) {
//if empty then simply return
if (isEmpty())
return;
//Start from head node
Node currentNode = this.headNode;
Node prevNode = null; //previous node starts from null
if(currentNode.data.equals(data)) {
//data is at head so delete from head
deleteAtHead();
return;
}
//traverse the list searching for the data to delete
while (currentNode != null) {
//node to delete is found
if (data.equals(currentNode.data)){
prevNode.nextNode = currentNode.nextNode;
return;
}
prevNode = currentNode;
currentNode = currentNode.nextNode;
}
}
}
Hi all, I am quite new to data structure and I am confused when I learned how to delete one specific value in the single linked list.
So when we traverse the LinkedList, we have a line like this
prevNode.nextNode = currentNode.nextNode;
I think this already means that we have connected "the previous node before the target node" and "the next node before the current node". Why do we still have these two lines after the traversing of the linked list?
prevNode = currentNode;
currentNode = currentNode.nextNode;
Are these two lines mean we are connecting the original previous node with the original next node? I always got lost when the code referred to the "currentNode".How can we tell which is the current "currentNode"?
Could someone help me with this? Visualized answer is appreciated. Thanks so much!
Why do we still have these two lines after the traversing of the linked list?
These are not after traversing; these are the traversing itself.
Some things to note:
An assignment to an attribute will mutate the list, like prevNode.nextNode = currentNode.nextNode does.
An assignment to a variable will not mutate the list, but can make a reference to a different node, like currentNode = currentNode.nextNode does. This really is the core of traversing: it makes the variable currentNode hop from one node to the next -- without changing anything to the list it is traversing.
In this specific algorithm, when the mutation with prevNode.nextNode = currentNode.nextNode is performed, the next thing that happens is a return -- so there is no further traversal happening.

Is there any way to delete any node in Binary Tree without having root node refrence?

I was trying for this problem but not able to crack it. We have a Binary Tree and I want delete a given node from the tree but we don't have reference of root node. I am getting an idea how can we do this without having reference of root node. We do have the reference to node5, like in the image.
Update considering the new edit.
Since this is NOT a Binary Search Tree, in this case I'm assuming the node order in the tree is not relevant and the tree will not have duplicates, so removing node5 will be possible with the same data structure we defined above, but we need to include a parent node reference (for step 3 detailed below):
node {
d: data
leftChild: node
rightChild: node
parent: node
}
Now we can remove node5 modifying the tree without accessing/modifying the root (node1).
You'll have to do the following:
(Remember that node 5 is also a Binary subtree, so after
modifying it, we want the whole Binary Tree to still remain one!)
Find node5's deepest node. You need to traverse node5's node reference as a tree.
(*Be careful with how you pass the arguments. root should be a copy,
so that it doesn't mess with node5' as a root, but level and
deepestNode should be references.)
findDeepestNode(root: node, level: integer, deepestNode: node) {
if (root != null) {
level = level + 1
find(root.left, level)
if (level > deepestlevel) {
deepestNode = root; // node reference to the deepestNode
deepestlevel = level;
}
find(root.right, level);
}
}
Once executing this, deepestNode in your case will be node7 or node8. It doesn't matter.
Now make node5's value equal to deepestNode's value. In our pseudocode:
node5.data = deepestNode.data // Replacing node5's value with the deletingNode's one.
The tree now has a duplicate value. You still have deepestNode. So now you assign deepestNode parent's reference to null and delete
deepestNode as a reference. This step is the reason we need to include a parent node reference.
Your removal is complete!
Given nothing else but the tree itself, then you'll always have to search for the node you want to remove before removing it. If you can set yourself up for success by keeping pointers to the nodes you may want to remove beforehand, then the answer could be yes!

Linked List Class in MATLAB - Insert node manually without insertAfter()

I am trying to use the Linked List Class implementation in MATLAB.
Now, it says the only way to insert a node into the list is by using insertBefore() or insertAfter().
But I want to insert the node manually, by specifying the Next value of the new node, like
newnode = dlnode(new);
ptr.Next = newnode;
newnode.Next=ptrnxt;
Would this work? I cannot use the insertBefore() or insertAfter() in my particular application since I am not maintaining a pointer of the current node.
Details regarding the linked list class is given here.
No, I don't think that would work with just those three lines of code because it ignores all the other logic that occurs to make sure that the Next and Prev are set for newnode and ignores the update to ptrnxt that would need to occur so that its Prev is now newnode. (And the Next property is private so you would have to change that to public…)
It is unclear by what you mean with I cannot use the insertBefore() or insertAfter() in my particular application since I am not maintaining a pointer of the current node. Yet you have the nodes that you want to insert newnode between? I'm guessing that the order of your nodes (prior to the insertion of the new one) is …,ptr,ptrnxt,…. Then why not just use
newnode.insertAfter(ptr);
which would change the order to …,ptr,newnode,ptrnxt,… and all three properties for each node would be set correctly/automatically.
Else, you would have to change your code to something like
newnode = dlnode(new);
ptr.Next = newnode;
newnode.Prev = ptr; % to make sure that newnode points back to ptr
newnode.Next = ptrnxt;
ptrnxt.Prev = newnode; % to make sure that ptrnxt points back to newnode
Much easier and safer to use the insertAfter and insertBefore methods.

Searching deep into Telerik Radtreview subtrees for a node with given value?

I have a RadTreeView C# component. The tree is nested, so some Nodes have their sub-trees, stored in Nodes property of upper-level Nodes.
Now I need to find a node by value. Node is hidden somewhere in subtrees. If I use call
RadTreeNode rtn= PagesTreeView.Nodes.FindNodeByValue(i.ToString());
where PagesTreeView is my tree, then it searches only across top-level nodes.
How I can Find Node by Value using not only Nodes from the current level of tree, but also dive into subtrees? Do I need to write such recursive search myself or there is a straightforward solution?
Recursively searching the RadComboBox
There isn't a built in function to recursively search, however you can roll your own pretty easiliy. This should work for you (not tested):
RadTreeNode FindNodeRecursive(RadTreeNodeCollection nodes, string value)
{
foreach (RadTreeNode node in nodes)
{
if(node.Value == value)
return node;
if (node.Nodes.Count > 0)
{
FindNodeRecursive(node.Nodes, value);
}
return null;
}
}
And then call it like this:
var node = FindNodeRecursive(PagesTreeView.Nodes, i.ToString());
Yes, you would need to write your own recursive function to do the search. Another option if you are using the ASP.NET AJAX version of the control is the GetAllNodes() method. It returns all nodes in the tree hierarchy (I'm guessing it uses recursion under the hood). Once you have the entire list you would search it for the node you care about. The big drawback with that approach is if you have a lot of nodes the search could be slow and consume a lot of memory. Doing your own recursive search is the best approach.
See this article for more info.
Old question but I faced this same problem in my application, how to search through the hierarchical tree nodes.
I would like to share one correction to previous solutions proposal. When calling FindNodeRecursive() recursively the return value of your recursive call is never being evaluated or assigned to a variable. So, you will always end up with going through the foreach loop and return value is null.
Corrected and tested function code (WPF C#):
RadTreeNode FindNodeRecursive(RadTreeNodeCollection nodes, string value)
{
RadTreeNode ret = null;
foreach (RadTreeNode node in nodes)
{
if(node.Value == value)
return node;
if (node.Nodes.Count > 0)
{
ret = FindNodeRecursive(node.Nodes, value);
}
return ret;
}
}
Function use:
var node = FindNodeRecursive(PagesTreeView.Nodes, i.ToString());
if (node != null) // found
{
;
}

How to represent data to be used for DFS/BFS

I was assigned a problem to solve using various search techniques. The problem is very similar to the Escape From Zurg problem or the Bridge and Torch problem. My issue is that I am lost as to how to represent the data as a tree.
This is my guess as to how to do it, but it doesn't make much sense for searching.
Another way could be to use a binary tree sorted by their walking time. However, I'm still not sure if I'm attacking this problem correctly since search algorithms don't necessarily require binary trees.
Any tips on representing this data would be appreciated.
Generally when you are using a tree search to solve a problem, each node represents some possible "state" of the world (who's on what side of the bridge, for example), and the children of each node represent all possible "successor states" (new states that can be reached in one move from the previous state). A depth-first search then represents trying one option until it dead-ends, then backing up to the last state where another option was available and trying it out. A breadth-first search represents trying out lots of options in parallel and seeing when the first of them find the goal node.
In terms of the actual way of encoding this, you would represent this as a multiway tree. Each node would probably contain the current state, plus a list of pointers to child nodes.
Hope this helps!
U could use something like this:
public class Node
{
public int root;
public List<Node> neighbours;
public Node(int x)
{
root=x;
}
public void setNeighboursList(List<Node> l)
{
neighbours = l;
}
public void addNeighbour(Node n)
{
if(neighbours==null) neighbours = new ArrayList<Node>();
neighbours.add(n);
}
...
}
public class Tree
{
public Node root;
....
}

Resources