java.util.ConcurrentModificationException when copy a tree structure - java

I want to copy a tree structure(source) to another one(target), but I got java.util.ConcurrentModificationException when I execute the method below:
private void mergeNode(TreeLayoutNode source, TreeLayoutNode target){
List<TreeLayoutNode> children = source.getChildren();
if(CollectionUtils.isEmpty(children)){
return;
}
Iterator<TreeLayoutNode> it = children.iterator();
while(it.hasNext()){
**TreeLayoutNode child = it.next();**
TreeLayoutNode copy = new TreeLayoutNode();
copy.setName(child.getName());
copy.setCapacity(child.getCapacity());
copy.setParent(child.getParent());
copy.setChildren(child.getChildren());
copy.setProportions(child.getProportions());
target.addChildNode(copy);
mergeNode(child, copy);
}
}
The code started with "**" is where exception occurs.Could any one give any hints?

Remove the call to 'copy.setChildren(child.getChildren())'. This results in references to children from the source tree being added to the destination tree, which is not what you want. The recursive method call will handle filling in the children.
Also you need to set the parent of the new node to the correct node in the new tree rather than calling 'copy.setParent(child.getParent())', which sets the parent reference to a node in the old tree. The call should probably be 'copy.setParent(target)'.

Related

Elegant way to check whether node already in graph

I want to check whether a node with a certain ID is already in my graph or whether I have to create a new object. At the moment I am doing it with the following code:
// at this point I have the attributes for the node I need
String id = getIdOfNeededNode(); // The id is used to search for the node in the graph
// now I have to search for the node in the graph
Node node = new Node("dummy_id"); // This is the line I don't like;
// I would prefer not to have a dummy node
// but the compiler will then complain that the node might not be initialized
boolean alreadyCreated = false;
for(Node r : graph.getVertices()){ // search for the node with this id in the graph
if (r.getId().equals(portId)){
node = r;
alreadyCreated = true;
break;
}
}
if (!alreadyCreated) { // create a new object if the node was not found
node = new Resource(portId);
createdPortResources.add(port);
}
// In the remainder of the program, I am working with the node object which then is in the graph
The fact that I am creating a dummy node that is just a placeholder is really ugly. Please let me know how I can solve this problem in a more elegant way.
Well, you can just do this Node node = null;
But in general, just keep a map from the portIds to the nodes.
When you want to make that check, just consult that map.
It will be way easier and faster.

Implementing the List<Node>

I decided to implement the Abstract List<Node> . here is a piece of it:
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class myNodeList implements NodeList{
Node root = null;
int length = 0;
public myNodeList() {}
public void addNode(Node node) {
if(root == null)
{
root = node;
}
else
root.appendChild(node);
length++;
System.out.println("this is the added node " +node);
}
}
but when I try to add a node, it gives me the following exception:
Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
at com.sun.org.apache.xerces.internal.dom.NodeImpl.insertBefore(NodeImpl.java:478)
at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:235)
at pageparsertest.myNodeList.addNode(myNodeList.java:27)
is this because of the Node root = null; which makes to add a node to a null node?
then how can be fixed
You can't append to a com.sun.org.apache.xerces.internal.dom.NodeImpl, you'll need to use com.sun.org.apache.xerces.internal.dom.ParentNode.
appendChild will call insertBefore which only throws an Exception for NodeImpl
Source code
Move one or more node(s) to our list of children. Note that this
implicitly removes them from their previous parent.
By default we do not accept any children, ParentNode overrides this.
Take a look how Axis implemented their : http://grepcode.com/file/repo1.maven.org/maven2/com.ning/metrics.collector/1.0.2/org/apache/axis/message/NodeListImpl.java
It seems you're trying to build a Node tree using the first Node as the Root, not a node list. Which is not possible has your nodes are NodeImpl not ParentNode.
If you want a tree, you'll have to create (or import) somehow a parent node.
If you just need a list, then use a List.
You may need to create a fake custom parent to insert your nodes.
Take a look here : HIERARCHY_REQUEST_ERR while trying to add elements to xml file in a for loop
well this is embarressing but I changed my idea to implement that and instead usedstatic List<Node> listOfNodes = new ArrayList<Node>();
which worked well for me!

Java: How to create a queue based on a Node class

I'm trying to create a queue using two classes, a Node class and a Queue class for an assignment. Here's the node class:
class Node
{
protected Node next;
public Node()
{
next = null;
}
}
This class basically links the data together using a Node.next object. I've successfully been able to create a stack with push() and pop(), because the two operations happen on the same end, so the point are just manipulated between pointing to a new added node, and the previous node.
However, I'm having some difficulties understanding the logic to create a queue based on a similar structure. My queue class looks something like this:
class Queue
{
private Node footer;
private Node header;
public Queue()
{
footer = null;
header = null;
}
public void add(Node newNode)
{
//Adds onto the queue from the 'footer' end.
}
public Node remove()
{
//Removes from the queue from the 'header' end.
}
Here's what I understand: (1)The header and the footer point to the same first node. (2) Subsequent adding should change the footer to point to the added nodes, but the header stays on the first node added. (3) The header should point to the next oldest node upon removal.
Here's what I can't figure out (and where it's different than popping from a stack). How do I get the header to point to the 'next oldest node', given that I have more than 2 nodes in this queue? I know I can do this if I link header.next to the next node in the queue, but how can I access the next node so that it can point to it?
I thought about how in add(), the newNode.next should point to the next newNode (reverse direction of a Stack), but this can't work because the next newNode isn't in existence yet.. Another idea was to modify the Node class to have a Node.previous for a way to point backwards, but I would be breaking specification for this assignment.
My instructor hinted something about "header.next will point for second item as header and footer point to first node initially," and that the way to do this is pretty simple. However, I've been drawing how this works, and I'm confused how the initial pointing to the same node will allow header.next to "automatically" point to the next oldest node, especially if more and more nodes are added and the footer eventually is separated from the header by more than 2 nodes. Is there something about OOP I'm not seeing?
Any help would be great!
To expand on, and offer a subtle alternative to #Sanjeev's answer (one that I think your instructor was hinting to):
Rather than using footer to store "actual" nodes, use it as a placeholder: Declare it as a final variable, initialize it in your constructor and make sure that either a) it's next node is always your header (this would be called a circular list), or its next node is null.
Can you see how this solves your "this can't work because the next newNode isn't in existence yet" problem: Of course you can't point the last node added to the next one that will be added before adding it - instead, you point it to this "dummy" node - which is a placeholder for the next node that will be added, when and if it is.
add(Node newestNode){
identify the last node added as the one whose next property is the footer.
change the next property of that node from footer to this new newestNode
set the next property of this new newestNode to footer
}
It would be preferable to identify that last node added as the one that footer is pointing to (rather than the one pointing to footer), which would be easy if you were allowed to have previous as well as next properties on nodes, but it sounds like you're not allowed to do that. Of course, since we're using footer as a "dummy node", we could simply use footer.next the way we would footer.previous and have it point backwards instead of forwards, but I'll leave you to consider how clean that would be. There are other options here that I'll leave you to consider as well.
How do I get the header to point to the 'next oldest node'`
The "oldest" node was the first one added. The "newest" node is the last one added. How is the order of the rest of the nodes stored? The same way it was in your Stack - by traversing a chain of references stored as instance variables on your nodes. The main point I want to make is that Stacks and Queues, when implemented as linked data structures, are much more similar than you seem to be thinking, at least from a : Iterating through any linked data structure is done by following traversing these links - don't get too hung up on the fact that you're "moving" in a different direction - the same basic principles apply:
Node remove(){
identify the "oldest" node as header.next.
Store a reference to that node so you can return it.
identify the "second oldest node" as header.next.next
change header.next to header.next.next
return the reference to the old header.next you saved above.
(Note that using header/footer as placeholders, rather than storing "actual" nodes in them as #Sanjeev suggests, is not necessary, but it'll make your life easier - for instance, by helping you avoid a lot of null checking)
Here is the sudo code that will help you get started.
public void add(Node newNode)
{
if footer is null ?
then
header = newNode and footer = newNode;
else
footer.next = newNode and footer = newNode;
end if
}
public Node remove()
{
Node returnMe = header;
if header is not null?
then
header = header.next
if header is null
then
footer = null;
endif
end if
return returnMe;
}
How do I get the header to point to the 'next oldest node', given that
I have more than 2 nodes in this queue? I know I can do this if I link
header.next to the next node in the queue, but how can I access the
next node so that it can point to it?
To make header point to that node, you only need do header = header.next. The reason is that Java objectt assignment is by reference. Since header.next is type of Node, header is type of Node, it will copy the address of header.next to header, i.e., header is advanced one place.
I thought about how in add(), the newNode.next should point to the
next newNode (reverse direction of a Stack), but this can't work
because the next newNode isn't in existence yet..
I think it is no need to considering reverse direction. The reason is because for adding , it is to add element to the tail/footer of the queue. The only special case is that the queue didn't have any elements (footer == header == null), 1 element : (footer = header = element), other case: header won't change, but you need to append element to footer, and then make footer point to the new node.
When only 1 element, footer.next == header.next == null
The first thing that you need to do is make sure the first node you create is the oldest so it should be the first to be removed from the Queue based on First In First Out (FIFO) principle to archive this you might need to modify you're add method to something like this, by the way this example is based on single linked list implementation.
void add(char new_data)
{
/* 1. alloc the Node and put data*/
Node new_Node = new Node(new_data);
/* 2. Make next of new Node as head */
new_Node.next = head;
/* 3. Move the head to point to new Node */
head = new_Node;
}
then you will need a remove method which will remove the oldest node on the list first remember in Queue the order of remove is First In First Out (FIFO)
that being said this remove method should help you
void remove()
{
// Store head node
Node temp = head, prev = null;
// If head node itself holds the key to be deleted
if (temp != null )
{
head = temp.next; // Changed head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change temp.next
while (temp != null)
{
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if (temp == null) return;
// Unlink the node from linked list
prev.next = temp.next;
}
This worked for me on my linked list

Can you help me understand this Java code?

I am working on a small project that requires loading an XML file. I found a nice code sample that extends DefaultHandler and uses a custom TreeRender to format the XML into a treeview here. The code compiles and runs (always a plus) and gives me the starting point I'm looking for but there is one little thing I don't understand in the code.
Here's the code snippet that I don't get:
public class XmlTreeView extends DefaultHandler {
private DefaultMutableTreeNode _base;
<snip>
#Override
public void startElement(String uri, String localName, String tagName, Attributes attr) throws SAXException {
System.out.println("startElement: uri=" + uri + " localname=" + localName + " tagName=" + tagName );
DefaultMutableTreeNode current = new DefaultMutableTreeNode(tagName);
_base.add(current);
_base = current;
for (int i = 0; i < attr.getLength(); i++) {
// <snip> attribute processing
}
}
The class declares a DefaultMutableTreeNode named _base. The startElement() method instantiates a new DefaultMutableTreeNode named current then does
_base.add(current);
_base = current;
All my programming knowledge tells me that the second statement assigns the new object (current) to the _base "variable", making the first statement useless. However, if I take out the first statement the code no longer works properly. In fact, if I take out either statement it no longer works properly. Both statements are required for the element to get added to the tree.
Can you explain to me what's happening here? I just don't get it.
Thanks in advance,
Steve
_base is a reference to an object. When you say _base.add(current), you are calling a method that makes some change to that object. When you then say _base = current;, _base becomes a reference to a different object. But the first object is still there. And whatever change you made to it can still impact the rest of the program, if there's a reference to it somewhere else.
These variables are badly named which is causing most of the confusion.
The field _base should really be called something like currentNode and the new Node created in startElement should be something like childNode.
Here is the same code but rewritten using these new variable names:
currentNode.add(childNode);
currentNode= childNode;
So you see when we enter a start element in the XML file, we create a new node and add it to our Object representation of the tree structure of the XML. The new child element that we just started is added to the current node as a child. Then we change our reference of the current node to point to this new child node. This makes the new child node our current node.
I assume in code that you haven't shown, there is an endElement where we do the reverse operation and move up the tree to the currentNode's parent.
You have a strange piece of code here in my mind but this is what is happening:
private DefaultMutableTreeNode _base;
Is acting as a global currentNode for the class. When you call startElement you are doing this:
DefaultMutableTreeNode current = new DefaultMutableTreeNode(tagName);
//Create a new TreeNode item based off the tagName
So you now have:
_base (no children)
Now you are going to add to the children of _base, the freshly created node
_base.add(current);
Now that this is done you have:
_base (no children)
current (child)
Finally
_base = current;
Now you have
(parent of _base) (the old base)
_base / current
The reference to _base now points to the freshly created child. When you call endElement, you will exit out of the _base and return to your old _base
_base is storing the xmlElement that you are currently working on. When you call startElement all calls to setAttribute or startElement will be based off this.
SUMMARY:
Here is what this looks like played out in code:
XmlWriter xWriter;
xWriter.startElement("NPC"); //_base becomes new node "hello"
xWriter.startAttribute("Greeting", "Hi"); //attribute is now set to _base (or greeting)
xWriter.startElement("Data"); //_base becomes new node "data"
xWriter.startAttribute("Height", "200"); //attribute is now set to _base (or data)
xWriter.endElement(); //on end element you move to parent of data, so greeting
xWriter.endElement(); //again you move to the parent
Creating:
<NPC Greeting='Hi'>
<Data Height='200'/>
</NPC>

Threaded Binary tree implementation from binary tree

I am working on assignment for school. It manly consists of a method that takes as input a binary tree and returns a double threaded tree. Eg(if left child = null then left child will be connected with preceding inorder parent and if right child = null the it will link to its inorder succesor. Now I have an idea for the implementation...
I iterate recursively trough the original BINARY tree and store into an array the inorder traversal. Now, because my teachers implementation requires that threaded trees be a different class from binary. I must traverse again trough the binary tree and convert each node from binaryNode to threadedNode thus having at the end a "duplicate" of the initial BinaryTree but as Threadedtree type. After I do this I traverse again trough this threadedTree and whenever i see a null left or right child I refer to the inorder arraylist and find the threads.
Now as you might have noticed this is extremely inefficient, i am essentially traversing the tree 3 times. My professor has stated that this could be done recursively with only one traversal, essentially converting to threadedNode and finding the threads all at once. I have tried multiple ways but i can not find one that works. Does anyone have any kind of tip or some way i can implement it? Thanks
This is the method as specified by the instructor
public static <T> ThreadedNode<T> thread(BinaryNode<T> root)
{
//threads a binary tree
}
The instructor is correct. One traversal is sufficient.
Traverse the original binary tree, creating new ThreadedNodes as you walk this tree.
public static <T> ThreadedNode<T> thread(BinaryNode<T> root) {
// We'll be keeping track of the "previous" node as we go, so use
// a recursive helper method. At first, there is no previous.
return threadHelper(root, null);
}
private static <T> ThreadedNode<T> threadHelper(BinaryNode<T> n, ThreadedNode<T> previous) {
// Create a new threaded node from the current root. Note that the threaded nodes
// are actually created in "preorder". Assume the ThreadedNode constructor sets
// the left, right, threadLeft, and threadRight fields to null.
ThreadedNode<T> t = new ThreadedNode<T>(n.getData());
// First go down the left side, if necessary.
if (n.getLeft() != null) {
// If there is a left child we have to descend. Note that as we go down the
// left side the previous doesn't change, until we start "backing up".
t.left = threadHelper(n.getLeft(), previous);
previous = t.left;
} else {
// If there is no left child, connect our left thread to the previous.
t.threadLeft = previous;
}
// Now before we go down the right side, see if the previous
// node (it will be in the left subtree) needs to point here.
if (previous != null && previous.right == null) {
previous.threadRight = t;
}
if (n.getRight() != null) {
// If there is a right child we can descend the right. As we go down we
// update previous to the current node. We do this just by passing the current
// node as the second parameter.
t.right = threadHelper(n.getRight(), t);
} else {
// No right child, no worries. We'll hook up our thread-right pointer
// later.
}
return t;
}
Consider the tree (A (B (D) ()) C). The first node you hit in an inorder traversal is D. There is no previous node. So save D as previous. Then the next node you hit is B. The previous node was D, which had no right child, so add a threaded right pointer from D to B. Then set previous to B and continue. Next you hit A. B had no right child, so add a threaded right link from B to A. A has a right child so continue, setting previous to A. The next node is C. C has no left child, so add a threaded left link from C to the current value of previous, which is A.
You could skip the second trip of traversal that you mention in your method. You could convert the nodes from BinaryNode to ThreadedNode on the fly. You'd still need to traverse twice, I think, for the inorder traversal, and for finding the threads and converting it to aThreadedTree.
For conversion on the fly, you could use the method that your instructor has given.
HTH!

Categories

Resources