HELP!!! Binary Search Tree!!

This is a discussion on HELP!!! Binary Search Tree!! within the C++ Programming forums, part of the General Programming Boards category; i'm having alot of problems with this code... The purpose of it is to grab a word from a text ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    HELP!!! Binary Search Tree!!

    i'm having alot of problems with this code... The purpose of it is to grab a word from a text file, (text1.txt), and then organize the word into a BST, then grab another word, put it in the appropriate node on the BST, and repeat until EOF is reached in text1.txt.... here's the code, please help someone!! i'm in pointer hell!!!! lol.
    Code:
    #include <iostream.h>
    #include <math.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <string.h>
    
    struct node 
    {
    
    	char wordFromFile [20];
    	node *left;
    	node *right;
    
    	treeNodeNew(char newWord[]);
    
    };
    
    struct node* allNode (char data[20])
    {
    	struct node* node = new(struct node);
    	node->wordFromFile[20] = data[20];
    	node->left = NULL;
    	node->right = NULL;
    
    	return (node);
    };
    
    
    
    struct node* insert(struct node* node, char data[20]) 
    {
      if (node == NULL) 
      {
        return(allNode(data));
      }
      else 
      {
      
        if (data <= node->wordFromFile) 
    		node->left = insert(node->left, data);
        else 
    		node->right = insert(node->right, data);
    
        return(node); 
      }
    } 
    
    
    
    int lookup(struct node* node, int target);
    
    
    
    
    
    int main()
    
    {
    
    	node newNode;
    	char word [20];
    	
    	ifstream inFile1 ("text1.txt");
    	if (!inFile1)
    	{
    		cout<<"Error opening file!"<<endl;
    		exit (1);
    	}
    	
    	while (!inFile1.eof())
    	{
    	
    		inFile1>>word;
    		strcpy (newNode.wordFromFile, word);
    		
    	}
    	
    
    
    inFile1.close();
    
    
    
    return 0;
    }
    
    
    int lookup(struct node* allNode, char target[20])
    {
    	if (allNode == NULL)
    		return (false);
    	else
    	{
    		if (target == allNode->wordFromFile)
    			return (true);
    	else
    	{
    		if (target < allNode->wordFromFile)
    			return (lookup(allNode->left, target));
    		else
    			return (lookup(allNode->right, target));
    	}
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > node->wordFromFile[20] = data[20];
    This should be strcpy()

    > if (data <= node->wordFromFile)
    This should be strcmp

    > if (target < allNode->wordFromFile)
    This should be the same strcmp.
    Using different comparison rules between insertion and searching will lead to grief.

    > while (!inFile1.eof())
    This is in the FAQ as to why it is a bad idea.

    Step 1 - reading files correctly
    Code:
    int main ( ) {
      ifstream inFile1 ("text1.txt");
      while ( inFile1>>word ) {
        cout << "Read a word = " << word << endl;
      }
      inFile1.close();
    }
    Play around with that until you're happy that its reading words correctly.

    Step 2 - test your tree
    Code:
    int main ( ) {
      node newNode = NULL;  // an empty tree
      newNode = insert( newNode, "hello" );
      newNode = insert( newNode, "world" );
    }
    BEFORE you throw several thousand words at your buggy tree code, use some really simple cases which you can easily debug using a debugger.
    Add several more words.

    When you're happy with both mains, then join them together.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21