I have the following declarations:

typedef struct{
	int  year; // key 
	char title[51];
	char director[41];
} MOVIE;

typedef struct node{
	struct node *left;
	MOVIE       *movie;
	struct node *right;
} TREE_NODE;

lets say I create a TREE_NODE called root. Why can't I do this declaration?

root->movie->year = 1990

Whenever I try it in my program, it crashes :\

Because you're trying to use movie when it's pointing to some random memory address, which will cause a program crash. You need to allocate memory, something like this:

root->movie = new MOVIE;

Then you can do something with it:

root->movie->year = 2000;

Hope this helps

well the reason i was asking was that I can't get my BST to build correctly -- that is, the data inside doesn't display correctly :\

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crtdbg.h>

#include "queueADT.h"
//#include "stackADT.h"

#define FILENAME "pictures.txt"

typedef struct{
	int  year; // key 
	char title[51];
	char director[41];
} MOVIE;

typedef struct node{
	struct node *left;
	MOVIE       *movie;
	struct node *right;
} TREE_NODE;

void printMenu();

TREE_NODE *buildBST( TREE_NODE *, MOVIE * );
void breadthFirst( TREE_NODE * );
void searchManager( TREE_NODE * );
TREE_NODE *searchBST( TREE_NODE *, int );
TREE_NODE *makeNode( );
void insertManager( TREE_NODE *, MOVIE * );
void insertBST( TREE_NODE *, TREE_NODE * );
void destroyBST( TREE_NODE * );
int emptyTree ( TREE_NODE * );

int main (void)
{
//  Local Definitions 
	TREE_NODE *root = NULL;
	MOVIE film;
	char choice;

//  Statements 
	printf("\n\n\t\tLAB 5:BST\n\n");
	printf("This program reads a list of movies from a text file"
		   " and inserts them into a BST tree \n\n" );

	root = buildBST( root, &film );
	printMenu();

	while ( choice != 'E' )
	{
		printf("\nEnter a selection (M for menu):\n");
		scanf( "%c", &choice );
		choice = toupper( choice );
		
		switch ( choice )
		{
			case 'L':
				breadthFirst( root );
				break;
			case 'S':
				searchManager( root );
				break;
			case 'I':
				insertManager( root, &film );
				break;
			case 'F':
				break;
			case 'M':
				printMenu();
				break;
			case 'E':
				printf("Have a nice day!\n");
				break;
			default:
				printf("Not a valid selection!\n");
				break;
		}

		fflush( stdin );
	}

	destroyBST( root );

	printf("\n\t\tEnd of Lab 5!\n" );

    printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");

	return 0;

}// main 

void printMenu()
{
	printf("L - Print Tree using Breadth-First Traversal\n");
	printf("S - Search by year\n");
	printf("I - Insert new data\n");
	printf("F - Save to file(sorted)\n");
	printf("M - Print menu\n");
	printf("E - Exit\n");
}

TREE_NODE *makeNode()
{
	TREE_NODE *newNode;

	if ( newNode = (TREE_NODE *)malloc(sizeof(TREE_NODE)) )
	{
		newNode->left = NULL;
		newNode->right = NULL;
	}
	else
	{
		printf("Error!\n");
		exit(0);
	}

	return newNode;
}

TREE_NODE *buildBST( TREE_NODE *root, MOVIE *pMovie )
{
	FILE *fpData;
	TREE_NODE *newNode;
	char buffer[200];
	int scanRes;

	if ( ( fpData = fopen( FILENAME, "r" ) ) )
	{
		while ( fgets( buffer, sizeof(buffer), fpData ) )
		{
			if ( scanRes = ( sscanf( buffer, "%d %s %s", &pMovie->year, &pMovie->title,
				&pMovie->director ) == 3 ) )
			{
				if ( emptyTree( root ) )
				{
					root = makeNode();
					root->movie = pMovie;
				}
				else
				{
					newNode = makeNode( );
					newNode->movie = pMovie;
					insertBST( root, newNode );
				}
			}
			else
			{
				printf("Error with file!\n");
				exit(0);
			}
		}
	}
	else
	{
		printf("Couldn't open file!\n");
		exit(0);
	}

	return root;
}

void breadthFirst( TREE_NODE *root )
{
	TREE_NODE *currentNode = root;
	QUEUE *bfQueue = createQueue();

	while ( currentNode )
	{
		printf("%d\n", currentNode->movie->year);
		if ( currentNode->left )
			enqueue( bfQueue, currentNode->left );
		if ( currentNode->right )
			enqueue( bfQueue, currentNode->right );
		if ( !emptyQueue( bfQueue ) )
			dequeue( bfQueue, (void *)&currentNode );
		else
			currentNode = NULL;
	}
	destroyQueue( bfQueue );
}

void searchManager( TREE_NODE *root )
{
	int data;
	TREE_NODE *temp;

	printf("Enter the year of the movie: ");
	scanf("%d", &data);

	temp = searchBST( root, data );
	if ( temp )
		printf("%d was found\n", temp->movie->year);
	else
		printf("Item not found\n!");
}

TREE_NODE *searchBST( TREE_NODE *root, int data )
{
	if ( emptyTree( root ) )
	{
		printf("Cannot search an empty tree!\n");
		return NULL;
	}

	if ( data < root->movie->year )
		return searchBST( root->left, data );
	else if ( data > root->movie->year )
		return searchBST( root->right, data );
	else
		return root;
}

void insertManager( TREE_NODE *root, MOVIE *pMovie )
{
	TREE_NODE *newNode;

	printf("Enter the year: ");
	scanf("%d", &pMovie->year);
	fflush( stdin );
	printf("Enter the movie title: ");
	scanf("%s", &pMovie->title);
	fflush( stdin );
	printf("Enter the movie director's name: ");
	scanf("%s", &pMovie->director);

	newNode = makeNode();
	newNode->movie = pMovie;
	insertBST( root, newNode );
}

void insertBST( TREE_NODE *root, TREE_NODE *newNode )
{
	TREE_NODE *pWalk;
	TREE_NODE *parent;

	if ( emptyTree( root ) )
		root = newNode;
	else
	{
		pWalk = root;
		while ( pWalk != NULL )
		{
			parent = pWalk;
			if ( newNode->movie->year < pWalk->movie->year )
				pWalk = pWalk->left;
			else
				pWalk = pWalk->right;
		}
		if ( newNode->movie->year < parent->movie->year )
			parent->left = newNode;
		else
			parent->right = newNode;
	}
}

void destroyBST( TREE_NODE *root )
{
	if ( root )
	{
		destroyBST( root->left );
		destroyBST( root->right );
		free( root );
	}
}

int emptyTree ( TREE_NODE *root )
{
	return ( root == NULL ); 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.