needle176
asked on
BINARY SEARCH TREE IN C++
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
A complete Binary Search Tree for just 20 pts. U must be kidding.
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.
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.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Note that in any case you are asking for a lot for 20 points.
-bcl