d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Assignment Help
Add Reply New Topic New Poll
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 24 2013 11:57am
So basically I have to create a insert and delete function for a binary search tree. I'm having some problems with my insert function as it only add two nodes and then replaces the existing ones with any new values I add. Any help would be greatly appreciated!


Code
bool bst::insert(string StudentName, int IDNumber) {

class node *temp = root;
class node *n=new node (StudentName, IDNumber);


if (root==NULL) {
root = n;
return true;

}
if (temp -> ID == n->ID)
return false;

while (temp)
{
if (n-> ID < temp-> ID)
{
temp-> left = n;
n->parent= temp;
return true;
}
else
{
temp->right=n;
n->parent=temp;
return true;
}
}
return false;
}

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2013 12:02pm
http://en.wikipedia.org/wiki/Binary_search_tree#Insertion

trace your code by hand, add logging statements, or debug via the IDE.

This post was edited by carteblanche on Nov 24 2013 12:03pm
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 24 2013 12:42pm
Quote (carteblanche @ Nov 24 2013 02:02pm)
http://en.wikipedia.org/wiki/Binary%5Fsearch%5Ftree#Insertion

trace your code by hand, add logging statements, or debug via the IDE.


well i think i pretty much figured out my problem is that my parent node isnt being changed to my temp node so everytime it goes to add a 4th node it starts back from the beginning and replaces the left or right node with the value I enter. I'm just not sure how to make that work maybe if i add

Code

temp = temp -> right
or
temp = temp -> left


Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 24 2013 01:39pm
Quote (axumo0 @ Nov 24 2013 01:42pm)
well i think i pretty much figured out my problem is that my parent node isnt being changed to my temp node so everytime it goes to add a 4th node it starts back from the beginning and replaces the left or right node with the value I enter. I'm just not sure how to make that work maybe if i add

Code
temp = temp -> right
or
temp = temp -> left


so are you having a logic error or a different kind of error?

If it is the former, pen and paper time.
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 24 2013 05:56pm
Quote (Eep @ Nov 24 2013 03:39pm)
so are you having a logic error or a different kind of error?

If it is the former, pen and paper time.


just cant figure out if im missing something or going in completely the wrong direction
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll