Created
April 27, 2017 09:12
-
-
Save vineethak/17505827ad705b248dc82458d796be8b to your computer and use it in GitHub Desktop.
A simple cpp code for binarysearch tree implementation. Input should be (i) insert <num> (ii) find <num> (iii) inOrder <num>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <cstdlib> | |
#include <string> | |
using namespace std; | |
template <class T> | |
struct Node{ | |
T val; | |
Node* left; | |
Node* right; | |
Node(T val){ | |
this->val = val; | |
this->left == NULL; | |
this->right == NULL; | |
} | |
Node (T val, Node* left, Node* right){ | |
this->val = val; | |
this->left = left; | |
this->right = right; | |
} | |
}; | |
template <class T> | |
class BST{ | |
private: | |
T val; | |
Node<T>* root; | |
public: | |
BST(); | |
Node<T>* createNode(T val); | |
void insertNode(Node<T>* root, T val); | |
void delNode(Node<T>* root, T val); | |
void inOrder(Node<T>* root); | |
bool findNode(Node<T>* root, T val); | |
void insertHelper(T val); | |
void findHelper(T val); | |
void delNodeHelper(T val); | |
void inOrderHelper(); | |
}; | |
template <class T> | |
BST <T>::BST(){ | |
this->root == NULL; | |
} | |
template <class T> | |
void BST <T>::insertHelper(T val){ | |
Node<T>* newNode = new Node<T>(val); | |
if (this->root == NULL){ | |
this->root = newNode; | |
} | |
else{ | |
this->insertNode(this->root,val); | |
} | |
} | |
template <class T> | |
void BST<T>::insertNode(Node<T>* root,T val){ | |
if (val > root->val){ | |
if (root->right == NULL){ | |
root->right = new Node<T> (val); | |
return; | |
} | |
else{ | |
insertNode(root->right,val); | |
} | |
} | |
else if (val < root->val){ | |
if (root->left == NULL){ | |
root->left = new Node<T> (val); | |
return; | |
} | |
else{ | |
insertNode(root->left,val); | |
} | |
} | |
} | |
template <class T> | |
void BST<T>::inOrderHelper(){ | |
this->inOrder(this->root); | |
} | |
template <class T> | |
void BST<T>::inOrder(Node<T>* root){ | |
if (root->left == NULL && root->right == NULL){ | |
cout << root->val << " "; | |
return; | |
} | |
if (root->left){ | |
inOrder(root->left); | |
} | |
cout << root->val << " "; | |
if (root->right){ | |
inOrder(root->right); | |
} | |
} | |
template <class T> | |
void BST<T>::findHelper(T val){ | |
bool found; | |
found = findNode(this->root, val); | |
cout << found << endl ; | |
} | |
template <class T> | |
bool BST<T>::findNode(Node<T>* root, T val){ | |
if (root == NULL){ | |
return false; | |
} | |
else if (root->val == val) | |
return true; | |
else if (root->val < val) | |
return(findNode (root->right, val)); | |
else | |
return(findNode (root->left, val)); | |
} | |
int main(){ | |
/* input should be of the type | |
(i) insert <5> | |
(ii) del <num> | |
(iii) search <num> */ | |
string input; | |
string query; | |
string num_str; | |
int num; | |
int pos; | |
BST <int>* myBST = new BST<int>() ; | |
while (getline(cin,input) != NULL){ | |
pos = input.find(" "); | |
query = input.substr(0,pos); | |
if (query.length() == 0){ | |
cout << "No input received: Exiting" << endl; | |
break; | |
} | |
num_str = (input.substr(pos+1,input.length())); | |
num = stoi(num_str); | |
if (!query.compare("insert")){ | |
myBST->insertHelper(num); | |
} | |
else if (!query.compare("inOrder")){ | |
myBST->inOrderHelper(); | |
} | |
else if (!query.compare("find")){ | |
myBST->findHelper(num); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above code doesn't handle deleting a node in BST.
Will soon be adding more functionalities into it.