jeudi 26 mars 2015

Binary search tree insertion method


I'm pretty sure I've isolated my bug to my insertion method. I'm not sure whether it's issues with my logic or a pointer issue. I've done the following:



bool BinarySearchTree::treeInsert(string courseNumber, string courseTitle)
{

Course * z = new Course(courseNumber, courseTitle);
Course *x, *y;
y = NULL;
x = root;

while (x != NULL){
y = x;

if(z->getCourseNumber() < y->getCourseNumber()){
x = x->getLeft();
} else{
x = x->getRight();
}
}

z->setParent(y);

if (y == NULL){
root = z;

} else {
if(z->getCourseNumber() < y->getCourseNumber()){
y->setLeft(z);
z->setParent(y);
}
else {
y->setRight(z);
z->setParent(y);
}
}
}


Is there something that stands out that I'm missing here?


Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire