Solved

BINARY SEARCH TREE IN C++

Posted on 2003-11-15
5
609 Views
Last Modified: 2010-04-02
HELLO, HOW DO I USE CLASSES IN CPP TO CREATE A BINARY SEARCH TREE, PLEASE GIVE ME A PERFECT **SIMPLE** EXAMPLE OF USING CLASSES TO CREATE A BINARY SEARCH TREE.  THANK YOU
0
Comment
Question by:needle176
5 Comments
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
Doing homework for someone is against the membership agreement (as is asking for homework solutions). Show us what you know how to do in C++. Where are you getting stuck? What are the objects in your program. That is, are you looking to create a BST objcect or a BST _OF_ objects or BOTH? Again, show us where you are in understanding the problem and we can help you figure out how to procede.

Note that in any case you are asking for a lot for 20 points.

-bcl
0
 

Expert Comment

by:rrattani
Comment Utility
A complete Binary Search Tree for just 20 pts. U must be kidding.
0
 
LVL 9

Expert Comment

by:jhshukla
Comment Utility
I will show u how data in a node of the BST looks like and how to use it in a BST. no more than that for 20 pts.

WARNING: There could be many syntactical errors in this example. if there are they all should be of the same kind: misplacing "templatizers".

template <class T>
class Node<T>{
  Node<T>::Node();
  T data;
  Node<T> *left_child, *right_child, *parent;
};

template<class T>
Node<T>::Node(Node<T> *l=NULL, T &d=T(),
                         Node<T> *r=NULL, Node<T> *p=NULL)
  :left_child(l), data(d), right_child(r), parent(p)
{}

//***********************************************//

template <class T>
class BST<T>{
  ...
  Node<T> *root;
};

template<class T>
BST<T>::BST(){
  root = NULL;
}

//***********************************************//

**NOTE**
1. The Node at which 'root' is pointing is the parent of all nodes in the tree. if root == NULL, the tree is considered empty and to have a height of -1.
2. The root node's parent must ALWAYS be NULL. you can temporarily change it for your program but be sure that you set it to NULL before you exit the function/method.

you will have to figure out algorithms for searching and moving date around in the BST without violating the property. there are many webpages that explain it very clearly. note that those pages will moslty contain algorithms or pseudo code. how to use them in C++? do it yourself. :)

Hope that buys me 20 pts.

0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ - no points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQed - no points refunded (of 20)

modulo
Community Support Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now