anonymous / BinaryTreeLinkedList.h secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View BinaryTreeLinkedList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#pragma once
#include<vector>
#include<queue>
#include<stack>
#include "BinaryTreeADT.h"
using namespace std;
template<typename T>
struct Node
{
Node<T> *left,*right;
T val;
Node(T val):left(NULL),right(NULL),val(val){}
};
template<typename T>
class BinaryTreeLinkedList:BinaryTreeADT<T>
{
 
Node<T>* root;
void removeNode(Node<T>* &p);
void inorder(Node<T>* r)const;
void preorder(Node<T>* r)const;
void postorder(Node<T>* r)const;
void insertRec(T item,Node<T>*& root);
bool searchRec(T item,Node<T>* root);
Node<T>* findMin(Node<T> *t);
void bstHelper(Node<T>* root,vector<T> &v );
Node<T>* findMax(Node<T> *t);
bool isSorted(const vector<T>& v);
void removeRec(T item,Node<T>*& t);
void copyTree(Node<T>*& copiedTree,Node<T>* othertree);
void destroy(Node<T>*& r);
void destroy();
void linkedListToArray(Node<T>* r,vector<T> &v);
public:
BinaryTreeLinkedList():root(NULL){}
void insert(T item);
void remove(int item);
void search(T toFind);
void insertRec(T item);
void searchRec(T item);
void removeRec(T item);
void inOrder() const;
void preOrder() const;
void postOrder()const;
bool isBst();
void levelOrder();
BinaryTreeLinkedList(const BinaryTreeLinkedList<T>& other);
const BinaryTreeLinkedList& operator=(const BinaryTreeLinkedList<T>& other);
~BinaryTreeLinkedList();
friend bool operator==(BinaryTreeLinkedList<T>& T1,BinaryTreeLinkedList<T>& T2);
vector<T> linkedListToArray();
void arrayToLinkedList();
};
 
View BinaryTreeLinkedList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma once
#include<vector>
template<typename T>
class BinaryTreeADT
{
public:
virtual void insert(T item)=0;
virtual void remove(T item)=0;
virtual void search(T item)=0;
virtual void insertRec(T item)=0;
virtual void removeRec(T item)=0;
virtual bool isBst()=0;
virtual void preOrder()const=0;
virtual void postOrder()const=0;
virtual void inOrder()const=0;
virtual void levelOrder()=0;
virtual std::vector<T> linkedListToArray()=0;
virtual void arrayToLinkedList()=0;
};
View BinaryTreeLinkedList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
#include "BinaryTreeLinkedList.h"
template<typename T>
void BinaryTreeLinkedList<T>::removeNode(Node<T>* &p)
{
Node<T> *current,*trailing,*temp;
if(p==NULL) cerr << "Can not remove from empty tree (1)"<<endl;
else if(p->left == NULL && p->right==NULL){
temp = p;
p=NULL;
delete temp;
}
else if(p->left==NULL){
temp = p;
p = p->right;
delete temp;
}
else if(p->right==NULL){
temp = p;
p = p->left;
delete temp;
}
else {
current = p->left;
trailing = NULL;
while (current->right != NULL)
{
trailing=current;
current = current->right;
}
p->val = current->val;
if(trailing==NULL)
p->left = current->left;
else
trailing->right = current->left;
delete current;
}
}
template<typename T>
void BinaryTreeLinkedList<T>::inorder(Node<T>* r)const
{
if(r==NULL) return;
else
{
inorder(r->left);
cout << r->val<<endl;
inorder(r->right);
}
}
template<typename T>
void BinaryTreeLinkedList<T>::preorder(Node<T>* r)const
{
if(r==NULL) return;
else
{
cout << r->val<<endl;
preorder(r->left);
preorder(r->right);
}
}
template<typename T>
void BinaryTreeLinkedList<T>::postorder(Node<T>* r)const
{
if(r==NULL) return;
else
{
postorder(r->left);
postorder(r->right);
cout << r->val<<endl;
}
}
template<typename T>
void BinaryTreeLinkedList<T>::insertRec(T item,Node<T>*& root)
{
if(root==NULL)
root = new Node<T>(item);
else if( item < root->val)
insertRec(item,root->left);
else
insertRec(item,root->right);
}
template<typename T>
bool BinaryTreeLinkedList<T>::searchRec(T item,Node<T>* root)
{
if(root==NULL)
return false;
if(root->val == item)
return true;
else if(item < root->val)
return searchRec(item,root->left);
else
return searchRec(item,root->right);
return false;
}
template<typename T>
Node<T>* BinaryTreeLinkedList<T>::findMin(Node<T> *t)
{
if(t==NULL)
return NULL;
if(t->left==NULL)
return t;
return findMin(t->left);
}
template<typename T>
void BinaryTreeLinkedList<T>::bstHelper(Node<T>* root,vector<T> &v )
{
if(root==NULL)
return;
else
{
bstHelper(root->left,v);
v.push_back(root->val);
bstHelper(root->right,v);
}
}
template<typename T>
Node<T>* BinaryTreeLinkedList<T>::findMax(Node<T> *t)
{
if(t==NULL)
return;
if(t->right==NULL)
return t;
return findMax(t->right);
}
template<typename T>
bool BinaryTreeLinkedList<T>::isSorted(const vector<T>& v)
{
for(unsigned int i=0;i<v.size()-1;i++)
if(v.at(i) <= v.at(i+1))
continue;
else
return false;
return true;
}
template<typename T>
void BinaryTreeLinkedList<T>::removeRec(T item,Node<T>*& t)
{
if(t==NULL)
return;
if(item < t->val)
removeRec(item,t->left);
else if(item > t->val)
removeRec(item,t->right);
else if(t->left!=NULL && t->right!=NULL)
{
t->val = findMin(t->right)->val;
removeRec(t->val,t->right);
}
else
{
Node<T>* old = t;
t = (t->left !=NULL)?t->left:t->right;
delete old;
}
}
template<typename T>
void BinaryTreeLinkedList<T>::copyTree(Node<T>*& copiedTree,Node<T>* othertree)
{
if(othertree==NULL)
copiedTree=NULL;
else
{
copiedTree = new Node<T>(othertree->val);
copyTree(copiedTree->left, othertree->left);
copyTree(copiedTree->right, othertree->right);
}
}
template<typename T>
void BinaryTreeLinkedList<T>::destroy(Node<T>*& r)
{
if(r!=NULL)
{
destroy(r->left);
destroy(r->right);
delete r;
r=NULL;
}
}
template<typename T>
void BinaryTreeLinkedList<T>::destroy()
{
destroy(root);
}
template<typename T>
void BinaryTreeLinkedList<T>::linkedListToArray(Node<T>* r,vector<T> &v)
{
if(!r)
return;
queue<Node<T>* > q;
q.push(root);
while(!q.empty())
{
Node<T>* temp = q.front();
q.pop();
v.push_back(temp->val);
if(temp->left!=NULL)
q.push(temp->left);
if(temp->right!=NULL)
q.push(temp->right);
}
}
template<typename T>
void BinaryTreeLinkedList<T>::insert(T item)
{
Node<T>* current;
Node<T>* trailing;
Node<T>* data = new Node<T>(item);
if(root==NULL)
root = data;
else
{
current = root;
while (current!=NULL)
{
trailing=current;
if(item < current->val)
{
current = current->left;
}
else
{
current = current->right;
}
}
if(item < trailing->val)
trailing->left = data;
else
trailing->right = data;
}
}
template<typename T>
void BinaryTreeLinkedList<T>::remove(int item)
{
Node<T>* current,*trailCurrent;
bool found = false;
if(root == NULL) cout << "Tree is Empty!!"<<endl;
else {
current=root;
while(current != NULL && !found){
if(current->val == item)
found = true;
else {
trailCurrent=current;
if(item < current->val)
current = current->left;
else
current = current->right;
}
}
if(current==NULL)
cout << "Node to be deleted is not in tree"<<endl;
else if(found)
{
if(current==root)
removeNode(root);
else if(item < trailCurrent->val)
removeNode(trailCurrent->left);
else removeNode(trailCurrent->right);
}
}//else block ends
}
template<typename T>
void BinaryTreeLinkedList<T>:: search(T toFind)
{
if(root==NULL)
cout << "Can not search empty tree"<<endl;
else
{
Node<T> *ptr = root;
bool found = false;
while (ptr!=NULL && !found)
{
if(toFind == ptr->val)
{
found=true;
}
if(toFind < ptr->val)
{
ptr = ptr->left;
}
else
{
ptr = ptr->right;
}
}
if(!found)
cout << "Vale not found"<<endl;
else
cout << "Found"<<endl;
}
}
template<typename T>
void BinaryTreeLinkedList<T>:: insertRec(T item)
{
insertRec(item,root);
}
template<typename T>
void BinaryTreeLinkedList<T>:: searchRec(T item)
{
if(root==NULL)
cout << "Can not search empty tree"<<endl;
else if(searchRec(item,root)) cout << "Found"<<endl;
else cout << "Value does not exists"<<endl;
}
template<typename T>
void BinaryTreeLinkedList<T>::removeRec(T item)
{
removeRec(item,root);
}
template<typename T>
void BinaryTreeLinkedList<T>::inOrder() const
{
inorder(root);
}
template<typename T>
void BinaryTreeLinkedList<T>::preOrder() const
{
preorder(root);
}
template<typename T>
void BinaryTreeLinkedList<T>::postOrder()const
{
postorder(root);
}
template<typename T>
bool BinaryTreeLinkedList<T>::isBst()
{
if(root==NULL)
return true;
vector<T> vec;
bstHelper(root,vec);
if(isSorted(vec))
return true;
return false;
}
template<typename T>
void BinaryTreeLinkedList<T>::levelOrder()
{
queue<Node<T>* > q;
q.push(root);
while(!q.empty())
{
Node<T>* temp = q.front();
q.pop();
cout << temp->val<<endl;
if(temp->left!=NULL)
q.push(temp->left);
if(temp->right!=NULL)
q.push(temp->right);
}
}
template<typename T>
BinaryTreeLinkedList<T>::BinaryTreeLinkedList(const BinaryTreeLinkedList<T>& other) //Copy Constructor
{
 
if(other.root==NULL)
root=NULL;
else
copyTree(root,other.root);
}
template<typename T>
const BinaryTreeLinkedList<T>& BinaryTreeLinkedList<T>::operator=(const BinaryTreeLinkedList<T>& other)//Assignement Operator
{
 
if(this!=&other)
{
if(root!=NULL)
destroy(root);
if(other.root==NULL)
root=NULL;
else
copyTree(root,other.root);
}
return *this;
}
template<typename T>
BinaryTreeLinkedList<T>::~BinaryTreeLinkedList()
{
destroy(root);
}
template<typename T>
bool operator==(BinaryTreeLinkedList<T>& T1,BinaryTreeLinkedList<T>& T2)
{
vector<T> v1;
T1.bstHelper(T1.root,v1);
vector<T> v2;
T2.bstHelper(T2.root,v2);
if(v1.size()!=v2.size())
return false;
for(int i=0;i<v1.size();i++)
if(v1.at(i)!=v2.at(i))
return false;
return true;
}
template<typename T>
vector<T> BinaryTreeLinkedList<T>::linkedListToArray()
{
vector<T> v;
linkedListToArray(root,v);
return v;
}
template<typename T>
void BinaryTreeLinkedList<T>::arrayToLinkedList()
{
cout << "Not implemented"<<endl;
}
 
View BinaryTreeLinkedList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include<iostream>
#include"BinaryTreeLinkedList.h"
using namespace std;
void main()
{
BinaryTreeLinkedList<int> bst;
/*bst.insertRec(80);
bst.insertRec(200);
bst.insertRec(300);
bst.insertRec(150);
bst.insertRec(70);
bst.insertRec(90);
bst.insertRec(60);
*/
bool exit = false;
do
{
int val;
cout << "Enter a Val: ";cin>>val;
cout <<"*******Insertion*******"<<endl;
cout << "1 To Add New Node (Iteratively)"<<endl;
cout << "2 To Add New Node (Recursively)"<<endl;
cout <<"*******Searching*******"<<endl;
cout << "3 To Search for Node (Iteratively)"<<endl;
cout << "4 To Search for Node (Recursively)"<<endl;
cout <<"*******Removal*********"<<endl;
cout << "5 To Remove Node (Iteratively)"<<endl;
cout << "6 To Remove Node (Recursively)"<<endl;
cout <<"*******Transversal*****"<<endl;
cout << "7 In Order (Recursively)"<<endl;
cout << "8 Pre Order (Recursively)"<<endl;
cout << "9 Post Order (Recursively)"<<endl;
cout << "10 Level Order (Recursively)"<<endl;
cout << "=========================="<<endl;
int option;cin>>option;
cout << "=========================="<<endl;
switch (option)
{
case 1:
bst.insert(val);
break;
case 2:
bst.insertRec(val);
break;
case 3:
bst.search(val);
break;
case 4:
bst.searchRec(val);
break;
case 5:
bst.remove(val);
break;
case 6:
bst.removeRec(val);
break;
case 7:
bst.inOrder();
break;
case 8:
bst.preOrder();
break;
case 9:
bst.postOrder();
break;
case 10:
bst.levelOrder();
break;
default:
exit=true;
break;
}
 
} while (!exit);
 
 
/*bst.insert(80);
bst.insert(200);
bst.insert(300);
bst.insert(150);
bst.insert(70);
bst.insert(90);
bst.insert(60);*/
/*bst.levelOrder();
vector<int> v;
v = bst.linkedListToArray();
for (int i = 0; i < v.size(); i++)
{
cout << v.at(i)<< ' ';
}*/
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.