How to get a node reference in a binary tree? - java

I'm doing this assignment right now and one of the methods I need to write is getting a node reference given a certain String label.
One of the fields in my node contains a String called label and I want to check my entire tree to see if a particular node contains that specific String.
How would I go about doing this?
So far I have a method called getNodeReference that takes a String label and I want to do something like:
if(root != contain the String) {
--search the left children, middle children, then right children (I guess its a ternary tree)
}
if (left.node != contain the String) {
--keep searching left, if you cant find it go on to middle. etc.
}

The kind of operation you are referring to are called Tree Traversals. A good reference on how to perform them is:
https://en.wikipedia.org/wiki/Tree_traversal
But an example could be (in pseudocode):
Set up a queue.
Enqueue the root.
While the queue is not empty:
Dequeue an item.
If the item matches, return it.
Else
Iterate over the children of the item, enqueuing them
in order from left to right.
This is referred to as a Breadth First Search. It is up to you to figure out how to do a similar Depth First Search, but I'll give you a hint. A stack might be involved.

Related

Convert General "Family" Tree to Binary Tree (Not BST)

I am currently considering different ways to accomplish the task of converting a family-type general tree (where each parent can have as many children as necessary) to a binary tree such that the left child of the parent node is inserted to the left of the parent and the right child of the parent node is inserted to the right of the (oldest) child node and so on.
This is the structure of how the binary tree must look like when converted from the general tree: https://www.geeksforgeeks.org/convert-a-generic-treen-array-tree-to-binary-tree/
I fed in my data by creating class object GTNode for the General Tree to add the nodes with the following values being passed:
//name = value1; numChildren = value2
GTNode gtn = new GTNode (String name, int numChildren);
No issues with constructor, this is purely to illustrated the main focus of the node is indeed the name of the family member. Thus this really can't be converted and sorted by a Binary Search Tree and is not the point of the resulting program I'm trying to create.
I was able to create methods to traverse the tree just fine, but it really doesn't help in the conversion process to the CONVERTED binary tree.
Therefore I created an additional section of code to create a linked list of the data. The reason why I did this was to establish pointers along the list which would point to the correct parent to add children to. Siblings would then be added to the right of the oldest sibling based on pointers based on where we were along real-time adding of nodes in the tree. By this time you can imagine I have two pointers going in my linked list traversal and three pointers along the tree nodes.
I can't seem to make good progress on an algorithm to create this binary tree from the data. here's a little sample code mostly pseudo code to show you basically how I'm adding nodes to the converted-style binary tree based on general tree data.
p=q=first; //initialize to first node f the linked list of family data
//ex. [Tom, 2] [Marc,1] [Ron,0] [Billy, 0] where Marc and Ron are Tom's children, and Billy is Marc's child)
z=root;
//I already established x, y = root earlier and moved pointer .next in order to add children nodes to the siblings
//here we establish x and y pointers for the tree and add our root node and first child node, which will always be to the left.
//I left it out of the while loop, because I was running into problems with null pointers.
{
while (p.next!=null && q.next!=null)
{
for (i = n; i>0; i--) //int n = node.numChildren; was declared earlier and then decreased
// n-=1 to accommodate the first child being added to left of parent node;
{
//add nodes left or right to parent node: still trying to figure out the appropriate condition
//statements to determine addLeft or addRight from node
}
//here we change the pointer of the GENERAL tree parent node based on the linked list
// add new to binary tree to the right of the last child i.e.
y = new BTNode(p.value,p.numChildren);
y = x.right;
x = y.parent;
x = y; //move pointer along
q = q.next; //q moves from Tom to Marc.
n = q.numChildren;
//go to beginning of while loop and as you can see, if n>0 it iterates based on the number of children
// if n=0 exits loop and creates a new node to the right
}
}
I'm wondering if this is an acceptable track to go down in terms of operability. Optimization I'm not as focused on because this is a difficult task... but could be taken into account.
Looking for pseudocode or java code shippers that would help in establishing an algorithm along these lines, or error in my logic. Again, not looking for an argument against doing a conversion like this because I want to see what it would look like as is.
Thanks!

How to remove nodes with java API for graphstream?

I currently use the Graphstream API for Java for my project.
I wan't to delete or add Nodes on command.
With JFrame & co. I initialized a console so I can just insert
"addNode()" or "removeNode(id)" in order to get the result.
A Interface shows the nodes with a number next to them(the ID).
When I delete one node, I want all nodes with higher ID to change their ID,
but I did not figure out a way jet to change the ID of one node.
F.e. I have:
graph.addNode(0);
graph.addNode(1);
graph.addNode(2);
When deleting a Node:
graph.removeNode(0);
I want 1,2 to be changed to 0,1 without reinitializing the complete graph.
Is there a way to achieve this behaviour? I thought about something like:
graph.getNode(1).setID(0);
Unfortunately I have only access to .getID() and can't manipulate it this way.
Thanks
Nodes ids are strings and they are immutable (no renaming, no setId()).
Now what you are doing in your example is different. You are using the index-based access to the nodes. Indices are integers and correspond to arbitrary nodes in the graph, they are not associated to the ids.
When you do graph.addNode(0), the integer is converted to the string "0". Then when you do graph.removeNode(0), you are removing a node that was indexed as the first of the list of nodes. But it does have to be the node this id "0".
You can remove nodes with index (integer) 0 as long as there are nodes in the graph (graph.removeNode(0)) but you can only remove the one node with id "0" once (graph.removeNode("0")).

Dummy nodes in linked lists

Q: When are they used? (Homework question)
1st and last node in the list
Sometimes used as 1st and last nodes in the list
Never used as 1st and last nodes in the list
Wikipedia says,
A sentinel node is a specifically designated node used with linked
lists and trees as a traversal path terminator. A sentinel node does
not hold or reference any data managed by the data structure.
I'm thinking B but I don't really know.
Look, there is a significant difference between a "Dummy" node and a "Sentinel" node.
Dummy nodes "Sometimes get used as first and last nodes in the list".
When you initiate a linked list a common approach is to create a dummy node, and interestingly it's the last node at the same time.
Obviously not always first or last nodes of a LL are dummy records.
Please note that you can use a dummy node without any data and a null pointer as a sentinel that shows the last node in the LL.
You may wonder is it possible to have a LL without any dummy node?
Answer: Yes. You can hold initialization of your LL until the insertion of the first data entry, and to that point just have null pointer as the LL, and after insertion(s) hold a pointer to the head node and always use a null pointer as "Next" node of the tail node.
I refer you to this page for more insight.
Yes, Answer is 2. Sometimes used as first and last nodes in the list.
To answer this question you need to understand need and use of dummy node. I will explain this with the help a link list problem.
Say you have Delete a node in a singly link list and only pointer to that node is given to you, How do you delete ??
Answer : If we have HEAD node we can simply traverse until we find that node and delete it, but this will not work if we have pointer of last node, because last node points to NULL.
Here we need DUMMY node. which is a blank node that help us build new node of delete node later.
In case of doubly link list this problem can be in either direction.
Definition of dummy node : a dummy node at the front/end of the list that is there only to reduce the need for special-case code in the linked-list operations. It is an empty template to build new nodes later.
problem here is we don't have any head node. We only have pointer to target node only.
Solution will be like this we will copy data from next node to node what to delete and delete next node.
struct node *temp = node_ptr->next;
node_ptr->data = temp->data;
node_ptr->next = temp->next;
free(temp);
but this will not work if we have pointer of last node, because last node points to NULL.
Here we need DUMMY node. which is a blank node that help us build new node of delete node later.
In case of doubly link list this problem can be in either direction.
Definition of dummy node : a dummy node at the front/end of the list that is there only to reduce the need for special-case code in the linked-list operations. It is an empty template to build new nodes later.
Reference : http://pages.cs.wisc.edu/~vernon/cs367/notes/4.LINKED-LIST.html
In algorithms questions, we always pass the head of the linked list as the argument. If you are changing the position of the head node and you need to return the new head node, the problem will be how are you gonna return the new head. That's why we initially create a dummy node and dummy.next will point to the head. So, if you are potentially modifying the head of list, use dummy node
One of Apple interview questions is to swap the pair of nodes and return the new head in one way linked list.
Since, the linked list is one way, after we swap the first 2 nodes (in question u need to swap all), how are we gonna keep the reference of the head node. That's where we use the dummy node.
dummy=new ListNode(dummyValue, head)
dummy node will always point to the first node, so re return dummy.next
There is one common question in linked list questions: Remove the n'th node from the end.
To solve this, we initialize slow pointer and fast pointer. first we traverse the fast pointer n times so when we keep traversing the fast pointer and we reach the end of the list, slow pointer will be n node behind.
However, to remove a node from a linked list, we just have to cut the pointer to that node. If nothing points to that node, it will be garbage collected.
For example, if we have 5 nodes and we need to delete 2nd node from the end, the 3rd node from the end points to the node that we are trying to delete. So we actually have to keep a reference of 3rd node from the end and assign its next to the last node. So we create a dummy node and slow pointer will be starting from there. It will be easier to show on an image:
If the nth node was point to the head of the linked list, we would have returned null. since dummy.next is head, we will return dummy.next

Find the first null value in a breadth-wide search of a binary tree

I'm trying to write a binary heap implemented over a binary tree, but I'm having trouble finding a way to add a new node to the "bottom" of the heap, i.e. the first null space on the tree in a breadth-first traversal. I've already got a working heapify function, but I can't figure out how to add a new node before heapifying.
I can't seem to think of a consistent algorithm that can find the null space that I can add my node to, every time I think I come up with something, it doesn't work. What do I do?

rich:tree - programmatically set the selected node

I have a tree (a parent could have any number of child ) and an inputText for searching nodes by name. Assume the following tree:
A
--A1
----AA1
--A2
Where A has two children A1 and A2, A1 has one child AA1.
When i type A1 in input text , i want to set nodes (A1,AA1) programmatically to select and if it is necessary expands nodes.
(i have a list<T> for building my tree)
I'm not sure what exactly you're asking for but I'll assume you want to set the value of node A1 and all it's children when you type "A1".
First you need to check that the user input is formatted correctly (doesn't look like A?1 when only letters and numbers are used to identify nodes).
Second, you need to locate the indicated node.
Third, you need to set the value and check for children.
Finally, once children are discovered, go back to the third step for each child.
Recursion could make your method more sleek but would not be necessary if you don't like recursion.
Sorry for such a vague answer but without a more detailed question it's hard to give a more detailed solution.
You can use TreeNode of richfaces instead of list, so you can easily achieve your requirements.
because using it you can easily get parent-child relationship using key value

Categories