Unordered_Map insert not giving me pointer

Apr 28, 2017 at 6:31am
Hello I'm having trouble with my unordered_map insert. My insert takes a Comparable, which is a template, and a BinomialNode* and inserts the Comparable as the key and the node as the value. When I inserted the keys and search for them, it's there in my map. My problem lies when I try to use the BinaryNode* from the map. When I output the address of the node to see if it's there, it returns 0x0, which I presume to be a nullptr. I'm stumped and can't figure out what I did wrong.

The insert function has my map insert and my contains function is trying to find the value and key if found.

https://pastebin.com/Nuzma42r

This is my read file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ifstream myFile;
	myFile.open(createQueue);
	if(myFile.is_open()){
		string fileLine;
		// Reads the file
		while(getline(myFile, fileLine)){
			// Converts the string into a number
			int number;
			stringstream convert(fileLine);
			convert >> number;
			// Inserts the number into the tree
			myTree.insert(number);
		}
		cout << "Success inserting " << numOfElement << " elements into the queue. The minimum element is " << myTree.findMin() << endl;
		myTree.print();
	}
	else{
		cout << "No file exist \n";
	}
	myFile.close();


Placing cout << oneItem.theTrees.at(0) << endl; inside my insert function reveals 0x0 only.
Last edited on Apr 28, 2017 at 7:08am
Apr 28, 2017 at 8:39am
I think the problem is that oneItem will be empty after calling merge(oneItem).
Apr 28, 2017 at 1:50pm
Edit: I shifted my merge to the bottom of the function, and it seems to work.
Last edited on Apr 28, 2017 at 1:58pm
Apr 28, 2017 at 2:46pm
I'm having problem with my deleteItem function. It's suppose to delete any node from the queue. I get the Node from map, percolate it up, set it as min and delete that node. My problem seems to be that curr->parent is always nullptr. I'm not sure how to set the parent* as a node like how I set curr as the element node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Deletes an item from hash table
    bool deleteItem( Comparable  &number ){
      
      Comparable minimum = findMin();
      Comparable temp = 0;
      if(contains(number) == true){
        BinomialNode *curr = hashTable.at(number);
        cout << "curr element: " << curr->element << endl;
        while(curr->parent != nullptr){
          // Swap parent with the child to percolate up
          curr->element = temp;
          cout << "Before Swap| curr: " << curr->element << " Parent: " << curr->parent->element << endl;
          curr->element = curr->parent->element;
          curr->parent->element = temp;
          curr = curr->parent;
          cout << "After Swap| curr: " << curr->element << " Parent: " << curr->parent->element << endl;
        }
        curr->element = minimum - 100;
        deleteMin(curr->element);
        hashTable.erase(hashTable.find(number));
        return true;
      }
      return false;
    }


Inside the private my struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct BinomialNode
    {
        Comparable    element;
        BinomialNode *leftChild;
        BinomialNode *nextSibling;
        BinomialNode *parent;

        BinomialNode( const Comparable & e, BinomialNode *lt, BinomialNode *rt )
          : element{ e }, leftChild{ lt }, nextSibling{ rt } { }
        
        BinomialNode( Comparable && e, BinomialNode *lt, BinomialNode *rt )
          : element{ std::move( e ) }, leftChild{ lt }, nextSibling{ rt } { }
    };
Last edited on Apr 28, 2017 at 4:47pm
Topic archived. No new replies allowed.