binary search trees

This is a discussion on binary search trees within the C++ Programming forums, part of the General Programming Boards category; I have a template binary search tree and have to create 2 trees. I am not sure how it is ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    Question binary search trees

    I have a template binary search tree and have to create 2 trees.
    I am not sure how it is done and how the BSTree class distinguishes between them.
    Code:
    template <class T>
    class BSTree
    {
    	public:
    		BSTree (){root=NULL; deleted=0;};
    		~BSTree (){clear(root);}
    
    		int isEmpty () const {return root==NULL;}
    		T retrieveData (T& data);
    		void insertData (const T& data);
    		void deleteData (const T& data);
    		void printBSTree ();
    
    	protected:
    		void clear (BTNode<T> *treeRoot);
    		void deleteHelper (BTNode<T> *&treeRoot, const T& data);
    		void insertHelper (BTNode<T> *&treeRoot, const T& data);
    		void printInOrder (BTNode<T> *treeRoot);
    		T retrieveHelper (BTNode<T> *treeRoot, T& data);
    		BTNode<T> *root;
    		int deleted;
    };
    
    //----------------------------------------
    
    class car
    {
    ....
    ....
    };
    
    //----------------------------------------
    
    class Bike
    {
    ....
    ....
    };
    Any help would be greatly appreciated.

    Cheers
    Sophie
    simple is always an understatement.....

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    You want to know how to create different BSTree objects based on your car and Bike classes?
    Code:
    BSTree<car> carTree;
    BSTree<Bike> bikeTree;
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    Hmmm...well I also have the following struct
    Code:
    struct MyString
    {
      	friend ostream& operator <<( ostream& os, const MyString& wrd);
    	int operator== (const MyString& wrd) const;
    	int operator < (const MyString& wrd) const;
    	int operator > (const MyString& wrd) const;
    
    	char *data; //could I use this for Car and Bike
    	char *key;  //could I use this for Car and Bike
    };
    Originally I was working with only the Car class as follows:
    class Car: public BSTree<MyString>

    But now I am creating the Bike class and dont know how to use it with the BSTree class as well. So I need 2 trees.

    Would the BSTree template be defined as :

    template <class T1, class T2> ??? I have seen something like that but dont understand if that is what I am suppose to do. I am not sure if I make any sense or not ...maybe it's because I am so confused.
    simple is always an understatement.....

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yes, templates may have multiple parameters, such:


    template<class T1, class T2>

    That means that you could have

    BSTree<car, Bike> myBSTree;

    but, knowing what you are trying to do with BStree class, car, Bike, myString etc. would be helpful to anyone who tries to help you sort through this. To have a BSTree that stores data about a group of cars or a group of Bikes, you would do as hk_mp5kpdw indicated. You may not need to have the car class inherit from the BSTree class at all, but then again it may be relevant. When I see the words data a key in the same class I start thinking about maps, but there are other uses for those terms, too, so having a better description of what you want to do would be helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with binary search.
    By StateofMind in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 02:14 PM
  2. Binary Search Trees
    By lorannex in forum C Programming
    Replies: 3
    Last Post: 04-25-2009, 06:24 AM
  3. Performance issue!
    By maven in forum C Programming
    Replies: 42
    Last Post: 03-23-2009, 11:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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