ok, here are the questions.
QUESTION NO. 1:
I am asked to build a complete binary tree using linked list. According to my knowledge, you are not able to sort the tree with a complete binary tree otherwise it is impossible to get it complete. I searched thru google and I found no examples on building a CBT with linked list, only couple pages discuss about implementing using array which isn't what I am looking for.
Here is my code, I know it's not gonna work well but I really have no idea how to fix it. Please have a look:
void IntegerNode::insertNode( intNode *root, Item_type item )
// Modificaton Member Function
// PRE: root is a valid intNode pointer, value is a valid Item_type object
// POST: value is inserted to the original tree
{
if ( root == NULL ) {
root -> data = item;
root -> right = root -> left = NULL;
// size ++;
// break;
}
else if ( root -> left = NULL ) {
insertNode ( root -> left, item );
}
else if ( root -> right = NULL ) {
insertNode ( root -> right, item );
}
else {
insertNode ( root -> left, item );
insertNode ( root -> right, item );
}
size ++;
}
I hope the code is clear enough. Users can call the function with a node to the root and an integer which the user wanna insert. To make a tree complete, all nodes have to be added to the as left as possible. SO this tree wont care about the value of the integer, it basically finds the most appropriate slot and place the integer in. For the first three IF statement, I basically check the root itself and left and right pointer, and I believe that so far I am doing correct. The problem raises when tree contains more than 3 nodes, and now with my code, you can see that at the forth IF statement, the function recurrsively calls with left and right pointers, this will make it go thru every node and check if there is space for the new item, but this will eventually result the item inserted into EVERY LEAF! which isn't what I want, but I have no idea how to fix this. Anyone helps?
QUESTION NO.2:
In the same souce file, I have another few functions and Dev-C++ reports errors during the compilation and I have no idea how to fix them since the code looks so right.
In my .h file I have
// the node to hold information
struct intNode {
Item_type data;
intNode* left; // to the left child
intNode* right; // to the right child
};
and
intNode* makenode ( Item_type item );
in the .cpp file, I implemented the code for the function makenode which has a return type of intNode pointer.
intNode* IntegerNode::makenode ( Item_type item )
// Access Member Function
// PRE: item is a valid Item_type object
// POST: A new intNode is created and the address is returned
{
intNode *newNode = new intNode;
newNode -> data = item;
newNode -> left = intNode -> right = NULL;
return newNode;
}
It looks right but Dev-C++ always reports error like
"sybtax error before '*' token"
"sybtax error before '->" token"
lots thanks to those who reads this thread!!