Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,740 people online right now. Registration is fast and FREE... Join Now!




trees inserting and searching

 
Reply to this topicStart new topic

trees inserting and searching

devilbolt
12 Mar, 2007 - 09:27 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2007
Posts: 9


My Contributions
can anyone help? i think there is an error wif the insert and search part. no .cpp file everything must be done in this .h file.

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

// There is no implementation file (*.cpp) if you want to use class template...
// Everything must be defined in the *.h file.

template <class T1,class T2>
class BSTNode {
public:
BSTNode(T1 _Key,T2 _Value) {
Key = _Key;
Value = _Value;
LeftSubTree = NULL;
RightSubTree = NULL;
}

// These variables are purposely set to be public...
T1 Key;
T2 Value;
BSTNode<T1,T2>* LeftSubTree;
BSTNode<T1,T2>* RightSubTree;
};

template <class T1,class T2>
class BST {
public:
BST() {
root = NULL;
}

void INSERT(T1 _Key,T2 _Value) {
root = INSERT(root,_Key,_Value);
}

BSTNode<T1,T2>* SEARCH(T1 _Key) {
return SEARCH(root,_Key);
}

int SIZE() {
return SIZE(root);
}

void Print_Inorder() {
Print_Inorder(root);
}


private:
BSTNode<T1,T2>* INSERT(BSTNode<T1,T2>* Node,T1 _Key,T2 _Value) {
if (Node== NULL)
{
Node=new BSTNode(T1 _Key,T2 _Value);
return Node;
}
else
if(T1 _Key->Key<Node->Key)
T1 _Key->LeftSubTree = insert(Node->LeftSubTree,T1 _Key,T2 _Value);
else
if(Node>T1 _Key->Key)
T1 _Key->RightSubTree = insert(Node->RightSubTree,T1 _Key,T2 _Value);
else
return T1 _Key;
}

BSTNode<T1,T2>* SEARCH(BSTNode<T1,T2>* Node,T1 _Key) {
if(T1 _Key==NULL)
return null;
if(T1 _Key==Node->Key)
return T1 _Key;
else
if(T1 _Key<Node->Key )
return search(Node->LeftSubTree,T1 _Key);
else
return search(Node->RightSubTree,T1 _Key);
}

int SIZE(BSTNode<T1,T2>* Node) {
if(Node==NULL)
return 0;
else
return 1+SIZE(Node->LeftSubTree)+SIZE(Node->RightSubTree)
}

void Print_Inorder(BSTNode<T1,T2>* Node) {
// Don't tweak this function please, otherwise you will get different output!
if (Node == NULL)
return;

Print_Inorder(Node->LeftSubTree);
cout << "Hobby: " << Node->Key << "." << endl;
cout << " Names:";
for (vector<string>::iterator i=Node->Value.begin(); i!=Node->Value.end(); i++)
cout << " " << *i;
cout << "." << endl;
Print_Inorder(Node->RightSubTree);
}

...

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Trees Inserting And Searching
12 Mar, 2007 - 10:20 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(devilbolt @ 13 Mar, 2007 - 10:57 AM) *

can anyone help? i think there is an error wif the insert and search part. no .cpp file everything must be done in this .h file.

CODE


  void INSERT(T1 _Key,T2 _Value) {
    root = INSERT(root,_Key,_Value);
  }

  BSTNode<T1,T2>* SEARCH(T1 _Key) {
    return SEARCH(root,_Key);
  }

private:
  BSTNode<T1,T2>* INSERT(BSTNode<T1,T2>* Node,T1 _Key,T2 _Value) {
    if (Node== NULL)
    {
       Node=new BSTNode(T1 _Key,T2 _Value);
       return Node;
    }
    else
       if(T1 _Key->Key<Node->Key)
          T1 _Key->LeftSubTree = insert(Node->LeftSubTree,T1 _Key,T2 _Value);
       else
          if(Node>T1 _Key->Key)
             T1 _Key->RightSubTree = insert(Node->RightSubTree,T1 _Key,T2 _Value);
          else
             return T1 _Key;
  }

  BSTNode<T1,T2>* SEARCH(BSTNode<T1,T2>* Node,T1 _Key) {
    if(T1 _Key==NULL)
       return null;
    if(T1 _Key==Node->Key)
       return T1 _Key;
    else
         if(T1 _Key<Node->Key )
            return search(Node->LeftSubTree,T1 _Key);
         else
            return search(Node->RightSubTree,T1 _Key);
  }





I just had a look on those functions [search and insert].
And from overview I found something in Insert.

See what are you returning from there...

CODE

BSTNode<T1,T2>* INSERT(BSTNode<T1,T2>* Node,T1 _Key,T2 _Value)
{
    if (Node== NULL)
    {
       Node=new BSTNode(T1 _Key,T2 _Value);
       return Node;
    }
    else if(T1 _Key->Key<Node->Key)
          T1 _Key->LeftSubTree = insert(Node->LeftSubTree,T1 _Key,T2 _Value);
    else if(Node>T1 _Key->Key)
           T1 _Key->RightSubTree = insert(Node->RightSubTree,T1 _Key,T2 _Value);
    else
           return T1 _Key;
  }


returning new created node when "Node==NULL" is ok but look at the rest. what it will return if "node!=NULL"? I think it should return node again and not T1_Key.

well as you did not mentioned what problem you are facing all I found is this. hope it will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

devilbolt
RE: Trees Inserting And Searching
13 Mar, 2007 - 01:13 AM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2007
Posts: 9


My Contributions
okie when i try the below, i got this 64 `BSTNode' is not a type error. please advise thks in advance.

BSTNode(T1 _Key,T2 _Value) {
Key = _Key;
Value = _Value;
LeftSubTree = NULL;
RightSubTree = NULL;
}

// These variables are purposely set to be public...
T1 Key;
T2 Value;
BSTNode<T1,T2>* LeftSubTree;
BSTNode<T1,T2>* RightSubTree;
};

//other codes

BSTNode<T1,T2>* INSERT(BSTNode<T1,T2>* Node,T1 _Key,T2 _Value) {
if ((Node->Key)==NULL)
return new BSTNode(_Key);
else
if(_Key<Node->Key)
Node->LeftSubTree = insert(Node->LeftSubTree,_Key,_Value);
else
if(_Key>Node->Key)
Node->RightSubTree = insert(Node->RightSubTree,_Key,_Value);
return Node;
}

// other codes

This post has been edited by devilbolt: 13 Mar, 2007 - 01:18 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month