473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

c++ trees

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);
}

...
Mar 13 '07 #1
0 1239

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: C++ Shark | last post by:
Hi, which stl class is good for creating search trees? I am looking for something flexible, that allows me to search for a required state (of matrices, graphs, etc.) quickly. thanks in...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
2
by: trusiki | last post by:
I am trying to use C# for my program that deals with manipulation of trees (i.e. finding distance between different nodes, assigning labels to nodes, storing trees, etc.). I know I can probably...
17
Ganon11
by: Ganon11 | last post by:
Hey guys, OK, taking care of this beforehand; I AM a student in a university. This IS part of my homework, and (as a moderator), I'm doing my best to follow the posting guidelines I work so hard...
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
8
by: Bert | last post by:
How can my response to this problem be better? The pot doesn't work that well when you enter 25, 21, 17 and some others. The problem: CHRISTMAS TREES The grade 5 class at the local primary...
6
by: rsprawls | last post by:
I found a disk for a b-tree algorithm that I purchased back in 93 or so. I'd hoped to find this, but now I'd like to know how worthwhile are b-trees in today's advancements? This is old C code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.