Jovaughn
BAN USERAlgorithm enthusiast who O(1)ing seeking to learn new things
Here is how I did this in Java. I have the full code committed on my github SerialCoderer under JCollections repo
public void swapNodes(T firstValue, T secondValue) {
JTreeNode<T> firstValueParentNode = findParentNode(this.rootNode, new JTreeNode<T>(null), firstValue);
JTreeNode<T> secondValueParentNode = findParentNode(this.rootNode, new JTreeNode<T>(null), secondValue);
if (( (firstValueParentNode.getLeftNode() != null &&
secondValueParentNode.getLeftNode() != null)
&& firstValueParentNode.getLeftNode().isEqual(firstValue)
&& secondValueParentNode.getLeftNode().isEqual(secondValue))){
JTreeNode<T> tempNode = firstValueParentNode.getLeftNode();
firstValueParentNode.setLeftNode(secondValueParentNode.getLeftNode());
secondValueParentNode.setLeftNode(tempNode);
}else if (((firstValueParentNode.getRightNode() != null &&
secondValueParentNode.getRightNode() != null)
&& firstValueParentNode.getRightNode().isEqual(firstValue)
&& secondValueParentNode.getRightNode().isEqual(secondValue))) {
JTreeNode<T> tempNode = firstValueParentNode.getRightNode();
firstValueParentNode.setRightNode(secondValueParentNode.getRightNode());
secondValueParentNode.setRightNode(tempNode);
} else if (((firstValueParentNode.getLeftNode() != null &&
secondValueParentNode.getRightNode() != null)
&& firstValueParentNode.getLeftNode().isEqual(firstValue)
&& secondValueParentNode.getRightNode().isEqual(secondValue))) {
JTreeNode<T> tempNode = firstValueParentNode.getLeftNode();
firstValueParentNode.setLeftNode(secondValueParentNode.getRightNode());
secondValueParentNode.setRightNode(tempNode);
} else {
JTreeNode<T> tempNode = firstValueParentNode.getRightNode();
firstValueParentNode.setRightNode(secondValueParentNode.getLeftNode());
secondValueParentNode.setLeftNode(tempNode);
}
}
public <T extends Comparable<T>> JTreeNode<T> findParentNode(
JTreeNode<T> node, JTreeNode<T> parentNode, T value) {
JTreeNode<T> tempNode = null;
if (node == null)
return null;
if (node.isEqual(value)) {
tempNode = parentNode;
} else {
if (tempNode == null)
tempNode = findParentNode(node.getLeftNode(), node, value);
if (tempNode == null)
tempNode = findParentNode(node.getRightNode(), node, value);
}
return tempNode;
}
In Java a HashMap is a key value pair data structure that is not thread safe. A hashmap internally uses an array. This internal array contains Entry objects with fields that encapsulates the key, value and some other fields used internally by the hashmap. Hashmaps are generally used when you need to access data in constant time O(1) and order is not a concern when traversing the elements of the hashmap. Objects are stored in the hashmap using the key's hashcode to calculate the location in the hashmap. If an object already exist at a location where new object is to be inserted then a comparison is done on the two objects keys, hashvalue and equals is also called on the 2 keys to see if they are equal. If they are equal the old value is replaced with the new, if not the object is inserted into the hashmap.
- Jovaughn July 10, 2013
Got it thanks!
- Jovaughn July 25, 2014