GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2006, 15:50
rpg3 rpg3 is offline
New Member
 
Join Date: Mar 2006
Posts: 4
rpg3 is on a distinguished road

Binary Search Tree in C++


hi guys,

I am having trouble with a binary search tree. I have a .h and .cc file for my binary search tree. and then i have a test file that takes a key, value pair from the user and inserts them into the tree by key. I figured out how to make methods to find keys remove keys and everything else, i am just struggling with how to create a tree in my test file and how the insert method should work. I thought i had it working but it seems its not. Any hints or suggestions would be greatly appreciated.
Thank you
  #2  
Old 26-Mar-2006, 22:24
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Binary Search Tree in C++


First I take out my magic wand and read your mind...Then you post the code you have and your specific problems and questions. Then, and only then, can we help.
  #3  
Old 27-Mar-2006, 12:37
cjavac#c++_vbP cjavac#c++_vbP is offline
Junior Member
 
Join Date: Mar 2006
Location: Miami, FL
Posts: 42
cjavac#c++_vbP is on a distinguished road

Re: Binary Search Tree in C++


search like you normally do down the tree..........and the first spot of null..........insert the new node there. like ubergeek above said............post your code.
  #4  
Old 01-Apr-2006, 12:46
rpg3 rpg3 is offline
New Member
 
Join Date: Mar 2006
Posts: 4
rpg3 is on a distinguished road

Re: Binary Search Tree in C++


insert function from BSTDictionary.cc
CPP / C++ / C Code:
template <class Key, class Item>

bool BSTDictionary<Key, Item>::recinsert(Key& key, Item& item, Kid* pointer)
{

Key* pkey;
Item* pitem;

if(pointer == NULL)
{

cout<<"root is null"<<endl;
root = new  Kid();

pkey = new Key(key);
pitem = new Item(item);

root->item = pitem;
root->key = pkey;

cout<<"Root values " + *root->key+ "  " + *root->item <<endl;
}

else if(key < *root->key){
cout<<"inserting to left of pointer"<<endl;
recinsert(key, item, root->left);
}

else if(key > *root->key){
cout<<"inserting to right of pointer"<<endl;
recinsert(key, item, root->right);
}


return true;

}

template <class Key, class Item>
bool BSTDictionary<Key, Item>::insert(Key& key, Item& item)
{

recinsert(key, item, root);


 return true;
}

BSTDictionary.h
CPP / C++ / C Code:
#include "AbsDictionary.h"

template <class Key, class Item>
class BSTDictionary : public virtual AbsDictionary<Key, Item>
{
   public:


      BSTDictionary();

      BSTDictionary(bool v);

      bool insert(Key& key, Item& item);

//    Item* find(Key& key);

//    void remove(Key& key);

//    void removeAll(Key&);

//    ~BSTDictionary();

protected:

        class Kid
        {

        public:
        ~Kid()
        {
        delete key;
        delete item;
        }

  Key* key;
       Item* item;
       Kid* right;
       Kid* left;

    };

As you can see its just a simple insert method into a binary search tree(recursive). it works ok for the first 2 elements, can distinguish weather to put them on the right or left of the root, but after that it only compares down to the first level, and then just inserts it. i can not figure out what i did wrong to cause it to do that. any hints or help would be greatly appreciated, because i am very very confused. Thanks all.
Last edited by cable_guy_67 : 02-Apr-2006 at 11:03. Reason: Please enclose c++ code in [c++] ... [/c++] tags
  #5  
Old 01-Apr-2006, 13:36
rpg3 rpg3 is offline
New Member
 
Join Date: Mar 2006
Posts: 4
rpg3 is on a distinguished road

Re: Binary Search Tree in C++


i understand what is wrong with it. lets say you insert 6 2 100 9 4 in that order, it will make 6 the root, then put 2 on its left. Then compare 2 and 100 and put 100 on 2s right and then compare 100 and 9 etc. I just am not sure how to get it to stick to the original tree, it seems like ive tried everything..
  #6  
Old 02-Apr-2006, 09:53
rpg3 rpg3 is offline
New Member
 
Join Date: Mar 2006
Posts: 4
rpg3 is on a distinguished road

Re: Binary Search Tree in C++


from testing and looking at it.. it seems like there might be something wrong with the way i define Kid in my header file.. but i am real confused to what it is wrong with it...
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Search Tree.. triples1488 C++ Forum 7 21-Mar-2006 07:16
Linear Search eccoflame C Programming Language 3 19-Apr-2005 08:36
printing binary search tree nkhambal C Programming Language 2 26-Mar-2005 03:01
Please help! Dynamic binary tree problem robsmith C Programming Language 3 15-Mar-2005 21:20
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 10:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.