Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class T>
- bool binary_search_tree<T>::insert(const T &item) {
- binary_tree_node<T> *p = root;
- if(root == NULL){
- root = new binary_tree_node<T>(item);
- return true;
- }
- while((root->left() != NULL || item > root->data()) && (root->right() != NULL || item < root->data())) {
- if(item < root->data())
- root = root->left();
- else if(item > root->data())
- root = root->right();
- else
- return false;
- }
- if(item < root->data() && root->left() == NULL)
- root->set_left(new binary_tree_node<T>(item));
- if(item > root->data() && root->right() == NULL)
- root->set_right(new binary_tree_node<T>(item));
- root = p;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement