I've given up, programming help NEEDED

SimpleMinded

Vault Fossil
sigh, I didn't want to show you guys how super bad i am but I've tried everything and now am in such a despate spot that I've come here. And considering about 95% of the people here are/were comp sci majors or program anyway I thought this would be the best place to ask my question. So in my high school, I took Comp Sci, got a 5 on the AP exam and thus, placed out of the intro level courses of computer science. The only issue was, my course was in java, whereas everything we do at school is in c++.

However, thinking myself magnificent, I figured it didn't matter, read a book on c++ over the summer and entered the higher level courses when I got here. Now I'm realizing I don't know ANY c++. So here's my issue. We're writing a program that's just a Binary Search Tree designed to sort object Contacts. So basically, a binary search tree for the phone book.

So in my header file, I declared a struct treeNode that looks like so:

struct treeNode{
Contact *c;
treeNode *left;
treeNode *right;
};

Now I also declared a treeNode root that would represent the top of the tree. I created a method insert which takes a Contact's information, and adds him to the tree. Now the first time insert is called, it adds the node no problem, then get's the hell out of there. But then, the second time, stuff gets ugly (in the displayed code).

First, the program gets into the while loop because the current Node's contact was initialized, last time in insert. It goes into the first if statement, then into the second and should begine the code i have there. Everything there works fine until I mention curNode->c. For some reason, whenever I even put curNode->c, my program terminates. It doesn't matter if I write = NULL or = new Contact, it just kicks. Am i doing something wrong? If so, what? PLEASE, help me. I went to the campus tutor earlier and he said he hadn't programmed in a long time and thus, couldn't help.

Thanks guys, glad i could bother you. If you need more info, say, the entire function or anything, let me know and I'll see what i can do.

while(curNode->c != NULL){
if(*(curNode->c) > contact){
if(curNode->left == NULL){
curNode->left == new treeNode();
curNode = curNode->left;
curNode->c = NULL;
}
else
curNode = curNode->left;
}
}
 
My grandma sez: if it's electronic and doesn't work, just give it a good kick.

It worked for me so far.
 
Did you allocate memory for curNode prior to accessing its member in the logical statement of the while loop in the first iteration? What's up with the line "curNode->left == new treeNode()"? Please tell me you didn't compile with that statement. It would result in curNode->left memory not getting allocated, so after "curNode = curNode->left" statement, curNode would have value NULL. Also, what's up with checking if "curNode->c != NULL" in the logical statement of the while loop? Shouldn't you be be checking if "curNode != NULL"? The difference between the two that the former causes an access violation while the latter doesn't. Why are you even using a while loop to browse the tree? Everyone knows the best way to browse a tree is using a recursive function, like this:

Code:
ConTree* addNewContact( ConTree *curNode, Contact *con )
{
	if( curNode == NULL )
	{
		curNode = new ConTree();
		curNode->c = con;
		curNode->left = curNode->right = NULL;
	}
	else if( *(curNode->c) < *con )
	{
		curNode->right = addNewContact( curNode->right, con );
	}
	else if( *(curNode->c) > *con )
	{
		curNode->left = addNewContact( curNode->left, con );
	}
	else
	{
                //Contact already exists.
	}
	return curNode;
}

See if that works for you.
 
GRAZ’ZT (Demon Prince)
Large Outsider (Chaotic, Evil)
Hit Dice: 62d8+620 (899 hp)
Initiative: +10 (+6 Dex, +4 Improved Initiative)
Speed: 40 ft
AC: 49 (-1 size, +6 Dex, +29 natural, +5 shield); 44 (without shield)
Attacks: Large +5 vorpal bastard sword +79/+74/+69/+64/+59/+54 melee; or Large +5 vorpal bastard sword +79/+74/+69/+64/+59/+54 melee and +3 guisarme +74 melee
Damage: Large +5 vorpal bastard sword 2d8+17 and 2d4 acid; or Large +5 vorpal bastard sword 2d8+17 and 2d4 acid and +3 guisarme 2d4+9
Face/Reach: 5 ft by 5 ft/10 ft (15 ft with guisarme)
Special Attacks: Spell-like abilities, spells, psionics, fear aura, summon demons
Special Qualities: Damage reduction 40/+4, SR 30, demon qualities, telepathy, darkvision 60 ft
Saves: Fort +43, Ref +39, Will +44
Abilities: Str 34, Dex 22, Con 30, Int 32, Wis 32, Cha 40
Skills: Bluff +77, Concentration +72, Diplomacy +77, Escape Artist +68, Gather Information +77, Hide +64, Heal +73, Intimidate +77, Knowledge (Abyssal politics) +73, Knowledge (arcana) +73, Knowledge (planes) +73, Knowledge (religion) +73, Listen +73, Move Silently +68, Scry +73, Search +73, Sense Motive +73, Spellcraft +73, Spot +73, C/C++ programming +73
Feats: Ambidexterity, Blind-Fight, Cleave, Combat Casting, Dodge, Great Cleave, Improved Critical (bastard sword), Improved Disarm, Improved Initiative, Improved Pointer Arithmetics, Improved Trip, Mobility, Power Attack, Skill Focus (C/C++ programming), Spring Attack, Sunder, Two-Weapon Fighting, Weapon Focus (bastard sword), Whirlwind Attack
Climate/Terrain: Any land and underground
Organization: Solitary or troupe (2-4 lamias plus 2-4 succubi or mariliths)
Challenge Rating: 42
Treasure: Double standard
Alignment: Always chaotic evil
Advancement: —
 
Back
Top