I've started to learn about data structures at school and I have an homework where I have to implement an Binary Search Tree and tell the memory occupied by the data structures.
I've created my BST and I believe all is working as expected but I don't have a clue how to calculate the memory used.
This is the code I've created for the data structure and the code for inserting:
class Node {
int key, totalValue;
public Node left;
public Node right;
public Node (int key, int totalValue) {
this.key= key;
this.totalValue = totalValue;
this.left = null;
this.right = null;
}
public int getkey() {
return key;
}
public int getTotalValue() {
return totalValue;
}
}
class Tree {
private Node root;
public Tree() {
this.root = null;
}
public Node getRoot() {
return this.root;
}
public void setRoot(Node node) {
this.root = node;
}
}
And this is the code for inserting:
private static void addNode(Node node, Node tempNode) {
if (tempNode.getkey() < node.getkey()) {
if (node.left == null) {
node.left = tempNode;
} else {
addNode(node.left, tempNode);
}
} else if (tempNode.getkey() > node.getkey()){
if (node.right == null) {
node.right = tempNode;
} else {
addNode(node.right, tempNode);
}
}else{
node.totalValue += tempNode.getTotalValue();
}
}
I know that for each node I need 8 bytes for the 2 int, but I don't know how much each pointer occupies.
Second question. Imagine I insert 25000 entries from 10000 keys. Each insertion will be used recursively until the new node finds it's "position". How can I calculate the memory occupied?