?
Solved

BINARY SEARCH TREE IN C++

Posted on 2003-11-15
5
Medium Priority
?
660 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 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