Skip to main content
gamedev.net
🔒 Locked

Trees, maybe pointers

Started by JinJo Mar 9, 2003 at 6:51 AM 5 replies 250+ views
Original Post
JinJo
JinJo
every time i go to start implementing a tree i always get problems and it is always to do with pointers at some point. Here is how if defined my tree
  
typedef int ITEM_TYPE;

struct BSTreeNode
{
	 ITEM_TYPE data;
	 struct TreeNode *left;
	 struct TreeNode *right;
};

typedef struct BSTreeNode *BS_TREE;
  
i am quite sure that doesn''t have any problems. But here i have a function for inserting a new item into the tree
  
bool InsertChild(BS_TREE *tree, ITEM_TYPE item)
{
	if(tree = NULL)
	{
		tree = new BS_TREE;
		tree->data = item;
		tree->left = NULL;
		tree->right = NULL;
	}
	else
	if(item = tree->data)
	{
		return false;
	}
	else
	if(item < tree->data)
	{
		InsertChild(tree->left, item);
	}
	else
	{
		InsertChild(tree->right, item);	
	}
	return true;
};
  
but i get 7 errors. they are all like this error C2227: left of ''->data'' must point to class/struct/union Can someone please help.
Corny806
Corny806
hello,

Try making you data structure

typedef struct BSPTreeNode_typ
{
unsigned int DATATYPE;
BSPTreeNode_typ *left;
BSPTreeNode_typ *right;
} BSPTreeNode,*BSPTreeNode_ptr;

fizban75
fizban75
You''ve typedefed BS_TREE as "pointer to struct BSTreeNode," therefore, the first function parameter (BS_TREE *tree) is now defined as "pointer to pointer to struct BSTreeNode" which means that you can''t say (tree->data). You have to say ((*tree)->data).

Also, your "equal" comparison is wrong. It should use "==" instead of "="
JinJo
JinJo
Thanks for the replys. I tried them and using the
((*tree)->right) looks really ugly, should I be doing this another way? Also now that function works but my program crashes, i checked it in the debugger and it seems I can''t set my tree to NULL. I''ll post the code

  
#include <stdio.h>
#include <conio.h>

typedef int ITEM_TYPE;

struct BSTreeNode
{
ITEM_TYPE data;
struct BSTreeNode *left;
struct BSTreeNode *right;
};

typedef struct BSTreeNode *BS_TREE;

void CreateTree(BS_TREE *tree);
bool InsertChild(BS_TREE *tree, ITEM_TYPE item);


void CreateTree(BS_TREE *tree)
{
tree = NULL;
}

bool InsertChild(BS_TREE *tree, ITEM_TYPE item)
{
if(tree == NULL)
{
tree = new BS_TREE;
((*tree)->data) = item;
((*tree)->left) = NULL;
((*tree)->right) = NULL;
}
else
if(item = ((*tree)->data))
{
return false;
}
else
if(item < ((*tree)->data))
{
InsertChild(&((*tree)->left), item);
}
else
{
InsertChild(&((*tree)->right), item);
}
return true;
};

int main()
{
BS_TREE *tree;
CreateTree(tree);
InsertChild(tree, 4);
getch();
return 0;
}



I have changed things around a bit and it doesn''t let me set the variables in the structure either.

What am I doing wrong

SirHando
SirHando
Your typedefs are wrong.... try


  
typedef struct BSTreeNode BS_TREE;
typedef struct BSTreeNode* BS_TREE_PTR;


Then your functoin decs become


  
void CreateTreee( BS_TREE_PTR _tree )
{
_tree = NULL;
}

bool InsertChild( BS_TREE_PTR _tree, ITEM_TYPE item )
{
if ( _tree == NULL )
{
_tree = new BS_TREE;
_tree->data = item;
_tree->left = NULL;
_tree->right = NULL;
}
else
{
// Insertion left to reader.

}
}


Ciao

SirHando

}
tuita
tuita
if(item = ((*tree)->data))

Shouldn''t this be ==. Otherwise, this would probably prove true every time, thus return false whenever tree!=NULL.
JinJo
JinJo
Well I have finished it and sorted out all the stupid mistakes and it works, I didn''t reply to tell anyone though cos this thread looked dead.

Anyway thanks for the replys

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.