Making a binary search tree into a template - c++

So, I've made a binary search tree, but now I'm stuck, as I have to make it into a template, yet my knowledge of templates are rather limited and if someone could, I'd appreciate it if they could "spoonfeed" me how to convert the tree into one that would work for any numerical data, thanks in advance!
Main.cpp
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
BST tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}
Bst.cpp
#include <iostream>
#include "bst.h"
using namespace std;
BST::node* BST::makeEmpty(node* tree)
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
BST::node* BST::insert(int x, node* tree)
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
BST::node* BST::findMin(node* tree)
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
BST::node* BST::findMax(node* tree)
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
BST::node* BST::remove(int x, node* tree)
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
void BST::inorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
void BST::preorder(node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
void BST::postorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
BST::node* BST::find(node* tree, int x)
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
BST::BST()
{
root = NULL;
}
BST::~BST()
{
root = makeEmpty(root);
}
void BST::insert(int x)
{
root = insert(x, root);
}
void BST::remove(int x)
{
root = remove(x, root);
}
void BST::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
void BST::search(int x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
Bst.h
#ifndef BST_H
#define BST_H
class BST
{
struct node
{
int data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(int x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(int x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, int x);
void insert(int x);
void remove(int x);
void displayorder();
void search(int x);
};
#endif // BST_H

You could do that by using the following changes:
In your BST.h file, add the following above the class decleration:
template < typename T >
class BST {
[...]
}
Then, foreach method definition, change it to:
template < typename T >
typename BST<T>::node* BST<T>::makeEmpty(BST<T>::node* tree)
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
Then, search and replace int with T in both BST.h and BST.cpp.
And then in your main.cpp change the variable to BST<int> tree.
The result should be this:
#include <iostream>
using namespace std;
template < typename T >
class BST
{
struct node
{
T data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(T x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(T x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, T x);
void insert(T x);
void remove(T x);
void displayorder();
void search(T x);
};
template < typename T >
typename BST<T>::node* BST<T>::makeEmpty(BST<T>::node* tree)
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
template < typename T >
typename BST<T>::node* BST<T>::insert(T x, BST<T>::node* tree)
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
template < typename T >
typename BST<T>::node* BST<T>::findMin(BST<T>::node* tree)
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
template < typename T >
typename BST<T>::node* BST<T>::findMax(BST<T>::node* tree)
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
template < typename T >
typename BST<T>::node* BST<T>::remove(T x, BST<T>::node* tree)
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
template < typename T >
void BST<T>::inorder(BST<T>::node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
template < typename T >
void BST<T>::preorder(BST<T>::node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
template < typename T >
void BST<T>::postorder(BST<T>::node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
template < typename T >
typename BST<T>::node* BST<T>::find(BST<T>::node* tree, T x)
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
template < typename T >
BST<T>::BST()
{
root = NULL;
}
template < typename T >
BST<T>::~BST()
{
root = makeEmpty(root);
}
template < typename T >
void BST<T>::insert(T x)
{
root = insert(x, root);
}
template < typename T >
void BST<T>::remove(T x)
{
root = remove(x, root);
}
template < typename T >
void BST<T>::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
template < typename T >
void BST<T>::search(T x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
int main()
{
BST<int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}

Related

Can't display binary search tree C++

I tried creating a binary search tree, using youtube and examples from my professor to help, but display in Driver.cpp doesn't show any node of the tree except "null" (which was intended to represent nullptr). I think it's because my "root" remains as nullptr, even though I've inserted a new node. The output should be "23 null null". Sorry if the codes aren't short and optimized, I want to make things clear for myself when I reread them later.
P.S.: I've deleted some unfinished functions in the code posted here so there might be minor mistakes
//Driver.cpp
#include <iostream>
#include "Overall Tree.h";
using namespace std;
int main()
{
BinaryTree tree_1;
tree_1.insertNode(23);
tree_1.display();
return 0;
}
//Overall Tree.h
#include <iostream>
using namespace std;
class BinaryTree
{
struct Node
{
int data;
Node* left;
Node* right;
};
private:
Node* root;
public:
// Constructors and Destructors
BinaryTree();
//traversal functions
void preOrder();
void inOrder();
void postOrder();
void preOrderTraverse(Node*);
void inOrderTraverse(Node*);
void postOrderTraverse(Node*);
//display function
void display();
//insert functions
void insertNode(int);
void insert(Node*, Node*);
};
BinaryTree::BinaryTree()
{
root = nullptr;
}
//display traversals
void BinaryTree::display()
{
cout << "Pre-order traversal: " << endl;
preOrder();
cout << endl << endl;
cout << "In-order traversal: " << endl;
inOrder();
cout << endl << endl;
cout << "Post-order traversal: " << endl;
postOrder();
}
// traversals
void BinaryTree::preOrder()
{
preOrderTraverse(root);
}
void BinaryTree::inOrder()
{
inOrderTraverse(root);
}
void BinaryTree::postOrder()
{
postOrderTraverse(root);
}
void BinaryTree::preOrderTraverse(Node* root)
{
if (root != nullptr)
{
cout << root->data << " ";
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
else
cout << "null ";
}
void BinaryTree::inOrderTraverse(Node* root)
{
if (root != nullptr)
{
preOrderTraverse(root->left);
cout << root->data << " ";
preOrderTraverse(root->right);
}
else
cout << "null ";
}
void BinaryTree::postOrderTraverse(Node* root)
{
if (root != nullptr)
{
preOrderTraverse(root->left);
preOrderTraverse(root->right);
cout << root->data << " ";
}
else
cout << "null ";
}
//insert
void BinaryTree::insertNode(int x)
{
Node* newNode = new Node;
newNode->data = x;
newNode->left = nullptr;
newNode->right = nullptr;
insert(newNode, this->root);
}
void BinaryTree::insert(Node* newNode, Node* p)
{
if (p == nullptr)
p = newNode;
else
if (newNode->data < p->data)
insert(newNode, p->left);
else if (newNode->data > p->data)
insert(newNode, p->right);
else
cout << "Value already existed in the tree.";
}
You should pass pointer to pointer to root in your insert function like:
void BinaryTree::insert(Node* newNode, Node** p)
{
if (*p == nullptr) {
*p = newNode;
} else {
if (newNode->data < (*p)->data) {
insert(newNode, &(*p)->left);
} else if (newNode->data > (*p)->data) {
insert(newNode, &(*p)->right);
} else {
cout << "Value already existed in the tree.";
}
}
}
and then use this function like:
insert(newNode, &this->root);
Why pointer to pointer?
Because you want modifications of the parameter (in this case p parameter), made in the insert function, to affect the argument which the caller passed in.
EDIT
You can also use reference for this purpose:
void BinaryTree::insert(Node* newNode, Node*& p)
{
if (p == nullptr) {
p = newNode;
} else {
if (newNode->data < p->data) {
insert(newNode, p->left);
} else if (newNode->data > p->data) {
insert(newNode, p->right);
} else {
cout << "Value already existed in the tree.";
}
}
}
and then use it like:
insert(newNode, this->root);
I would suggest using reference because of the cleaner syntax IMO.

c++ template binary search tree

What I want is a rather simple binary search tree that via the template tag, allows for any numerical data to be used within it, but I'm having some rather obnoxious issues that I have no clue of how to get rid off, if anyone can help, it would be much appreciated. The error message that keeps popping up for me is "Invalid use of template-name 'BST' without an argument list" - and quite frankly I have no clue how to solve it. It occurs on line 31, 89, 105, 120, 130, 141 in the bst.cpp file:
Main.cpp
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
BST <int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}
bst.h
#ifndef BST_H
#define BST_H
template <class T>
class BST
{
struct node
{
T data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(T x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(T x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, T x);
void insert(T x);
void remove(T x);
void displayorder();
void search(T x);
};
#endif // BST_H
bst.cpp
#include <iostream>
#include "bst.h"
using namespace std;
template <class T>
void BST<T>::preorder(node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
template <class T>
void BST<T>::postorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
template <typename T>
BST::node* BST<T>::find(node* tree, T x) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
template <typename T>
BST<T>::BST()
{
root = NULL;
}
template <typename T>
BST<T>::~BST()
{
root = makeEmpty(root);
}
template <class T>
void BST<T>::insert(T x)
{
root = insert(x, root);
}
template <class T>
void BST<T>::remove(T x)
{
root = remove(x, root);
}
template <class T>
void BST<T>::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
template <class T>
void BST<T>::search(T x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
template <class T>
BST::node* BST<T>::insert(T x, node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
BST::node* BST::findMin(node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
BST::node* BST::findMax(node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
template <typename T>
BST::node* BST<T>::remove(T x, node* tree) //AN ERROR OCCURS HERE
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
template <class T>
void BST<T>::inorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
You need to qualify BST::node too, as BST<T>::node. Also, keep in mind that all the template code will need to live in bst.h because it will need to be instantiated by each source file that uses it (like main.cpp).
Add the
Add the BST::node in your function definitions. In addition include the .ipp (cpp file) as shown in line 27 in order to have the class methods in the header and the implementation in a .ipp file. You don't have to place all your template code in the header file if you don't wan't to.
Another tip is the fact that you must not pass the root node to your functions because anyone outside the class can have access to your tree root. Aim for something like the following code
1 #ifndef BST_H
2 #define BST_H
3
4 #include <memory>
5
6 template<typename T>
7 class node;
8
9 template <typename T>
10 class BST
11 {
12 public:
13 BST();
14 BST(std::initializer_list<T> init);
15 ~BST();
16
17 void make_empty();
18 bool is_empty() const noexcept;
19 bool is_full() const;
20 void insert(const T&);
21 void remove(const T&);
22 void print_tree() const noexcept;
23 private:
24 std::unique_ptr<node<T>> root;
25 };
26
27 #include "binary_search_tree.ipp"
28
29 #endif

C++ template binary search tree - Argument list error

What I want is a rather simple binary search tree that via the template tag, allows for any numerical data to be used within it, but I'm having some rather obnoxious issues that I have no clue of how to get rid off, if anyone can help, it would be much appreciated. The error message that keeps popping up for me is "Invalid use of template-name 'BST' without an argument list" - and quite frankly I have no clue how to solve it. It occurs on line 31, 89, 105, 120, 130, 141 in the bst.cpp file. Given that I'm not as proficient when it comes to binary search trees, I'd prefer as conclusive of an answer as possible (even going as far as to mention exactly where and what needs to be changed):
Main.cpp
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
BST <int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}
BST.cpp
#include <iostream>
#include "bst.h"
using namespace std;
template <class T>
void BST<T>::preorder(node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
template <class T>
void BST<T>::postorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
template <typename T>
BST::node* BST<T>::find(node* tree, T x) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
template <typename T>
BST<T>::BST()
{
root = NULL;
}
template <typename T>
BST<T>::~BST()
{
root = makeEmpty(root);
}
template <class T>
void BST<T>::insert(T x)
{
root = insert(x, root);
}
template <class T>
void BST<T>::remove(T x)
{
root = remove(x, root);
}
template <class T>
void BST<T>::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
template <class T>
void BST<T>::search(T x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
template <class T>
BST::node* BST<T>::insert(T x, node* tree) //ERROR HERE
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
BST::node* BST::findMin(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
BST::node* BST::findMax(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
template <typename T>
BST::node* BST<T>::remove(T x, node* tree) //ERROR HERE
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
template <class T>
void BST<T>::inorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
BST.h
#ifndef BST_H
#define BST_H
template <class T>
class BST
{
struct node
{
T data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(T x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(T x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, T x);
void insert(T x);
void remove(T x);
void displayorder();
void search(T x);
};
#endif // BST_H
By example, in
BST::node* BST<T>::find(node* tree, T x)
you forget the <T> component for the first BST.
Should be
// vvv
BST<T>::node* BST<T>::find(node* tree, T x)
All other errors are of the same type.
A class template like the BST you are apparently using is not a type. It's a recipe for creating a class type. What the error message is trying to tell you is that (almost) anywhere you use the name BST, you need to supply template arguments immediately after it inside <angle brackets>.
For example, in
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //ERROR HERE
the compiler is complaining about the first instance of BST in the return type, not the one that correctly specifies BST<T> as the class type. This should probably be:
template <class T>
BST<T>::node* BST<T>::makeEmpty(node* tree)
[There are at least two exceptions to this general rule. One is that the name of a template alone can be used as a template argument to another template which expects a template instead of a type or value.
The other is called the "injected class name": Inside the scope of a class template, including a class template member, you can use just the name of the template as an alias to the "current" specialization.
So in fact you could also use a trailing return type and do:
template <class T>
auto BST<T>::makeEmpty(node* tree) -> BST::node*
In the above, since the return type now comes after the BST<T>:: and not before, it is now in the scope of the class template, so you are allowed to use just BST as an alias for BST<T>.]

Binary search tree C++ need the search and display functions to work

Need a void function Display that will display the binary search tree in tree shape
Need a value return or void function Search that will search for a specific data.
Not really sure What to do from this point except trial and error and trying to read off of others code...
I need help
Help
Here is my code...
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root == NULL; }
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
bool Search(int, tree_node*);
void Display(tree_node*, int);
};
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
system("cls");
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << " ----------------------------- " << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Pre-Order Traversal " << endl;
cout << " 4. Post-Order Traversal " << endl;
cout << " 5. Removal " << endl;
cout << " 6. Display " << endl;
cout << " 7. Display Message From Creator " << endl;
cout << " 8. EXIT " << endl;
cout << " Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1: cout << " Enter Number to be inserted : ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal " << endl;
cout << " -------------------" << endl;
b.print_inorder();
break;
case 3: cout << endl;
cout << " Pre-Order Traversal " << endl;
cout << " -------------------" << endl;
b.print_preorder();
break;
case 4: cout << endl;
cout << " Post-Order Traversal " << endl;
cout << " --------------------" << endl;
b.print_postorder();
break;
case 5: cout << " Enter data to be deleted : ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: cout << " Enter data to be deleted : ";
b.Display(NULL, tmp);
break;
case 7: cout << " I think you are a great person. Smile be happy ";
case 8:
return 0;
}
}
}
bool BinarySearchTree::Search(int d, tree_node* curr){
if (curr == NULL) {
return 0;
}
if (curr->data == d) {
return 1;
}
if (d > curr->data) {
return Search(d, curr->right);
}
else {
return Search(d, curr->left);
}
}
// Smaller elements go left
// larger elements go right
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if (isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::remove(int d)
{
//Locate the element
bool found = false;
if (isEmpty())
{
cout << " This Tree is empty! " << endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else
{
parent = curr;
if (d>curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout << " Data not found! " << endl;
return;
}
// 3 cases :
// 1. We're removing a leaf node
// 2. We're removing a node with a single child
// 3. we're removing a node with 2 children
parent = NULL;
// Node with single child
if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL
&& curr->right == NULL))
{
if (curr->left == NULL && curr->right != NULL)
{
if (parent->left == curr)
{
parent->left = curr->right;
delete curr;
}
else
{
parent->right = curr->right;
delete curr;
}
}
else // left child present, no right child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
//We're looking at a leaf node
if (curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else // right child has children
{
//if the node's right child has a left child
// Move all the way down left to locate smallest element
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while (lcurr->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else
{
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout << " " << p->data << " ";
if (p->right) inorder(p->right);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left) preorder(p->left);
if (p->right) preorder(p->right);
}
else return;
}
void BinarySearchTree::print_postorder()
{
postorder(root);
}
void BinarySearchTree::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout << " " << p->data << " ";
}
else return;
}
void BinarySearchTree::Display(tree_node *curr, int indent)
{
if (curr != NULL)
{
cout << "My Tree. The left is top and right is bottom" << endl;
Display(curr->left, indent + 4);
if (indent > 0)
cout << setw(indent) << " ";
cout << curr->data << endl;
Display(curr->right, indent + 4);
}
}

Binary Search Tree Potentially Uninitiated Parent Pointer

I couldnt seem to resolve the unitialized local pointer "Parent". There isnt any other issue with this code other than the local pointer error I keep receiving Its driving me insane.
this is just me writing to post the simplest of questions
please aide me
I couldnt seem to resolve the unitialized local pointer "Parent". There isnt any other issue with this code other than the local pointer error I keep receiving Its driving me insane.
this is just me writing to post the simplest of questions
please aide me
//Binary Search Tree
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool IsEmpty() const {
return root == NULL;
}
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
};
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if (IsEmpty()) root = t;
else
{
tree_node* curr;
curr = root;
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::remove(int d)
{
tree_node* curr;
tree_node* parent;
bool found= false;
if (IsEmpty())
{
cout << " This Tree is Empty!" << endl;
return;
}
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else {
parent = curr;
if (d > curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout << " Data is not found!" << endl;
return;
}
if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL))
{
if (curr->left == NULL && curr->right !=NULL)
{
if (parent -> left == curr)
{
parent->left = curr->right;
delete curr;
}
else {
parent->right = curr->right;
delete curr;
}
}
else //Left child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
if (curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 Children
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else
{
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while(lcurr ->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else {
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout << " " << p->data << " ";
if (p->right) inorder(p->right);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left)preorder(p->left);
if (p->right) preorder(p->right);
}
else return;
}
void BinarySearchTree::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout << " " << p->data << " ";
}
else return;
}
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << "---------------------------------" << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Pre-Order Traversal " << endl;
cout << " 4. Post-Order Traversal " << endl;
cout << " 5. Removal " << endl;
cout << " 6. Exit " << endl;
cout << " Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1: cout << "Enter Number to be Inserted: ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal: " << endl;
cout << "---------------------" << endl;
b.print_inorder();
break;
case 3: cout << endl;
cout << " Pre-Order Traversal: " << endl;
cout << "------------------------" << endl;
b.print_preorder();
break;
case 4: cout << endl;
cout << " Post-Order Traversal: " << endl;
cout << "--------------------------" << endl;
b.print_postorder();
break;
case 5: cout << " Enter data to be deleted: ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: system("pause");
return 0;
break;
}
}
}
tree_node* parent;
You declare parent but never assign it a tree_node memory address to point to. Thus, performing operations on it or accessing its members (like parent->left) will cause issues, and raise the error you are getting.
Solution: Assign a value to parent before accessing it's members.