Thread: problem with binary search trees

  1. #1
    Registered User Daniel's Avatar
    Join Date
    Jan 2003
    Posts
    47

    problem with binary search trees

    Hi,

    When I try use my insert function I get an unhandled exception error any reason why?

    Thanks for your help,
    Daniel

    [tag]
    Code:
    void insertBST(node *&root, node *temp)
    {
    	if(root == NULL)
    		root = temp;
    	else
    	{
    		
    		while((temp->data <= root->data && root->left != NULL) || (temp->data >= root->data && root->right != NULL))
    		{
    			if(temp->data < root->data)
    				root = root->left;
    			else
    				root = root->right;
    		}
    		if(temp->data <= root->data)
    			root->left=temp;
    		else
    			root->right = temp;
    	}
    }
    [/tag]

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do you really want to modify root like that?
    Shouldnt you be iterating the tree with a current node principle?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in storing data in a binary search tree
    By alavardi in forum C Programming
    Replies: 5
    Last Post: 02-13-2005, 03:20 PM
  2. Problem with fprintf and binary search.
    By tallgeese84 in forum C Programming
    Replies: 4
    Last Post: 12-06-2004, 09:37 PM
  3. STL and Binary Search Trees
    By xshapirox in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2004, 12:12 PM
  4. Problem with binary search tree :(
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-01-2002, 10:31 PM
  5. Newbie questions regarding binary search trees
    By Ham in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2001, 07:48 AM