Solved

BINARY SEARCH TREE IN C++

Posted on 2003-11-15
5
651 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

691 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