Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

BINARY SEARCH TREE IN C++

Posted on 2003-11-15
5
633 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
ID: 9759070
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
ID: 9764205
A complete Binary Search Tree for just 20 pts. U must be kidding.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 9767863
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
ID: 10249249
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
ID: 10308121
PAQed - no points refunded (of 20)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

789 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