I currently need to implement a max heap of nodes where my node class keeps track of the data, the parent and then the left and right child. My insert method for max heap is taking forever to fill with an array of 100 strings. Here is my code: `
public void insert(String name) {
MyNode node = new MyNode(name);
if (root ==null) {
root = node;
}
else {
MyNode parent = findSpot(root);
if(parent.lChild==null) {
parent.lChild=node;
node.setParent(parent);
}
else {
parent.rChild=node;
node.setParent(parent);
}
}
}
public MyNode findSpot(MyNode curr) {
if (curr.lChild == null) {
return curr;
}
else if (curr.rChild==null) {
return curr;
}
else {
if (findSpot(curr.lChild).findHeight(root, curr, 1) > findSpot(curr.rChild).findHeight(root, curr, 1)) {
return findSpot(curr.lChild);
}
else {
return findSpot(curr.rChild);
}
}
}`
If anyone code offer suggestions or tell me whats wrong that'd be greatly appreciated.