0

I am trying to create a new node based on an Entry object. I retrieve 2 different entries and print out their key and value. Both of the keys printed out are different. I store each key and value into seperate "Nodes", n1, and n2. Node is a custom class I created, the code for it can be found below. When I print out the value of n1's key and n2's key, they should be different since the key of the entry was, but for some reason it prints out the same value. I'm not sure why this happens and I can't figure it out. This is the code I have currently:

Node Class:

import java.util.HashMap;
import java.util.Map.Entry;

public class Node {
    public static Entry<String, Integer> keyVal;
    public static Node leftChild;
    public static Node rightChild;
    public static String bitCode;

    public Node() {
        keyVal = null;
        leftChild = null;
        rightChild = null;
    }

    public Node(Entry<String, Integer> entry) {
        keyVal = entry;
        leftChild = null;
        rightChild = null;
    }

    public Node(Entry<String, Integer> entry, Node n1, Node n2) {
        keyVal = entry;
        leftChild = n1;
        rightChild = n2;
    }

    public Entry<String, Integer> getEntry() { return keyVal; }

    public Node getLeftChild() { return leftChild; }

    public Node getRightChild() { return rightChild; }

    public void setLeftChild(Node lc) {
        leftChild = lc;
    }

    public void setRightChild(Node rc) {
        leftChild = rc;
    }
}

createTree method in Main class:

private static Node createTree(PriorityQueue<Entry<String, Integer>> pq) {
    map = new HashMap<>();    // Defined as a public attribute to the Main class
    Entry<String, Integer> entry1 = null;
    Entry<String, Integer> entry2 = null;
    Entry<String, Integer> parent = null;
    Node n1 = null;
    Node n2 = null;
    Node n3 = null;
    System.out.println("PQ size: " + pq.size());

    while (pq.size() > 1) {
        entry1 = pq.poll();
        // Prints "Entry1: R 1"
        System.out.println("Entry1: " + entry1.getKey() + " " + entry1.getValue());    

        entry2 = pq.poll();
        // Prints "Entry2: e 1"
        System.out.println("Entry2: " + entry2.getKey() + " " + entry2.getValue());

        // Never enters if, goes to else
        if (map.containsKey(entry1.getKey())) {
            System.out.println("Map contains entry1!");
            n1 = map.get(entry1.getKey());
        } else {
            n1 = new Node(entry1);              
        }
        // Never enters if, goes to else
        if (map.containsKey(entry2.getKey())) {
            System.out.println("Map contains entry2!");    
            n2 = map.get(entry2.getKey());
        } else {
            n2 = new Node(entry2);              
        }
        // Should print "N1: R 1", Instead prints "N1: e 1"
        System.out.println("N1: " + n1.getEntry().getKey() + " " + n1.getEntry().getValue());
        // Prints "N2: R 1"
        System.out.println("N2: " + n2.getEntry().getKey() + " " + n2.getEntry().getValue());
    }

1 Answer 1

2

This happens because the members in class Node are static:

//From the 'Node' class:
public static Entry<String, Integer> keyVal;
public static Node leftChild;
public static Node rightChild;
public static String bitCode;
//...

So, when you create a second Node, doing:

 n1 = new Node(entry1);

You override the value entered in the first place, at:

n2 = new Node(entry2);
Sign up to request clarification or add additional context in comments.

2 Comments

@Michael You're welcome. If I helped, I'd love it if you could mark my answer as accepted. Thanks!
Just did! I had to wait the 5 minutes or so before it allowed me to accept

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.