Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename key, typename info>
  6. class Dictionary{
  7. struct node{
  8. key nkey;
  9. info ninfo;
  10. node *left;
  11. node *right;
  12. int height;
  13. node(const key& k, const info& i):left(0),right(0),height(1),nkey(k),ninfo(i){}
  14. };
  15.  
  16. node* root;
  17.  
  18. unsigned int size=0;
  19.  
  20. int height(node* N)const{
  21. if(N==nullptr){return 0;}
  22. return N->height;
  23. }
  24.  
  25. int max(int a, int b){
  26. return(a>b?a:b);
  27. }
  28.  
  29. int getBalance(node *N){
  30. if (N->right==nullptr && N->left== nullptr){return 0;}
  31. return (height(N->left)) - height(N->right); // problem +1
  32. }
  33.  
  34. node* FindMin(node* root){
  35. while(root->left != nullptr) root = root->left;
  36. return root;
  37. }
  38.  
  39. node* rotate(node *n,bool r){
  40. node *e=nullptr;
  41. if(r){
  42. e = n->left;
  43. node *temp = e->right;
  44.  
  45. e->right = n;
  46. n->left = temp;
  47.  
  48. n->height = max(height(n->left), height(n->right))+1;
  49. e->height = max(height(e->left), height(e->right))+1;
  50. }
  51. else{
  52. e = n->right;
  53. node *temp = e->left;
  54.  
  55. e->left = n;
  56. n->right = temp;
  57.  
  58. n->height = max(height(n->left), height(n->right))+1;
  59. e->height = max(height(e->left), height(e->right))+1;
  60. }
  61. return e;
  62. }
  63.  
  64.  
  65. node* balance(node* nptr){
  66. nptr->height=1+max(height(nptr->left),height(nptr->right));
  67. int bal = getBalance(nptr);
  68. if(bal > 1){
  69. if(height(nptr->left->left) > height (nptr->left->right)){
  70. return rotate(nptr,true);
  71. }
  72. else{
  73. nptr->left=rotate(nptr->left,false);
  74. return rotate(nptr,true);
  75. }
  76. }
  77. if(bal < -1 ){
  78. if(height(nptr->right->right) > height(nptr->right->left)){
  79. return rotate(nptr,false);
  80. }
  81. else{
  82. nptr->right=rotate(nptr->right,true);
  83. return rotate(nptr,false);
  84. }
  85. }
  86.  
  87. return nptr;
  88. }
  89.  
  90.  
  91. node* insert(node* nptr,const key& k,const info& i){
  92. if (find(root,k)==true){
  93. cout << "Key: " << k << " already exists" << endl;
  94. return root;
  95. }
  96.  
  97. if(nptr == nullptr){
  98. return new node(k,i);
  99. }
  100.  
  101. if(k < nptr->nkey){
  102. nptr->left = insert(nptr->left, k,i);
  103. }
  104.  
  105. else{
  106. nptr->right = insert(nptr->right, k,i);
  107. }
  108. //cout << nptr->nkey <<" "<< height(nptr->left)<<" "<< height(nptr->right)<< endl;
  109. nptr=balance(nptr);
  110. }
  111.  
  112. node* deleteNode(node* nptr, const key& k ){
  113. if (find(nptr,k)==false){
  114. cout << "Key: " << k << " not found" << endl;
  115. return nptr;
  116. }
  117.  
  118. if(nptr == nullptr)return nptr;
  119.  
  120. if(k < nptr->nkey){
  121. nptr->left = deleteNode(nptr->left, k);
  122. }
  123. else if(k > nptr->nkey){
  124. nptr->right = deleteNode(nptr->right, k);
  125. }
  126. else{
  127. // node with only one child or no child
  128. if( (nptr->left == nullptr) || (nptr->right == nullptr) ){
  129. node *temp = nptr->left ? nptr->left : nptr->right;
  130.  
  131. // No child case
  132. if(temp == nullptr){
  133. temp = nptr;
  134. nptr = nullptr;
  135. }
  136. else{ // One child case
  137. *nptr=*temp; // Copy the contents of the non-empty child
  138. //cout<<"check:" << nptr->height <<" "<<temp->height << endl; //check copy
  139. delete temp;
  140. }
  141. }
  142. else{
  143. // node with two children, Get the inorder successor (smallest in the right subtree)
  144. node* temp = FindMin(nptr->right);
  145. *nptr=*temp;
  146. // Copy the inorder successor's data to this node
  147. /*nptr->nkey=temp->nkey;
  148. nptr->ninfo=temp->ninfo;
  149. nptr->height=temp->height;
  150. */
  151. // Delete the inorder successor
  152. nptr->right = deleteNode(nptr->right, temp->nkey );
  153. }
  154. }
  155.  
  156. if (nptr == nullptr)return nptr; // If the tree had only one node then return
  157. nptr=balance(nptr); //balance the tree after deletion
  158. }
  159.  
  160. void preOrder(node * root)const{
  161. if(root == nullptr) return;
  162. cout << root->nkey << " ";
  163. preOrder(root->left);
  164. preOrder(root->right);
  165. }
  166.  
  167. void inOrder(node *root)const {
  168. if(root == nullptr) return;
  169. inOrder(root->left); //Visit left subtree
  170. cout << root->nkey << " ";
  171. inOrder(root->right); // Visit right subtree
  172. }
  173.  
  174. void postOrder(node * root)const{
  175. if(root == nullptr) return;
  176. postOrder(root->left);
  177. postOrder(root->right);
  178. cout << root->nkey << " ";
  179. }
  180.  
  181. bool find(node* nptr,const key& k)const{
  182. if(nptr==nullptr)return false;
  183. if(nptr->nkey==k) return true;
  184. else if(k<=nptr->nkey) return find(nptr->left,k);
  185. else return find(nptr->right,k);
  186. }
  187.  
  188. node* copy(node * orig)const{
  189. if(orig == nullptr){return nullptr;}
  190.  
  191. node *n = new node(orig->nkey, orig->ninfo);
  192.  
  193. n->left = copy(orig->left);
  194. n->right = copy(orig->right);
  195. return n;
  196. }
  197.  
  198. void clearAll(node* nptr){
  199. if(nptr!=nullptr){
  200. clearAll(nptr->left);
  201. clearAll(nptr->right);
  202. delete nptr;
  203. size=0;
  204. }
  205. }
  206.  
  207. bool search(const key& k)const{
  208. return find(root,k);
  209. }
  210.  
  211. bool compare(node *n) const{
  212. if (n){
  213. if (!compare(n->left)){ //go to the left
  214. return false;
  215. }
  216.  
  217. if (!compare(n->right)){ //go to the right
  218. return false;
  219. }
  220.  
  221. if (!search(n->nkey)){ //search for corresponding key //only checks for the equal key not info
  222. return false;
  223. }
  224. }
  225.  
  226. return true;
  227. }
  228.  
  229. void recInsert(node *nptr){
  230. if (nptr){
  231. insert(nptr->nkey, nptr->ninfo);
  232.  
  233. recInsert(nptr->left);
  234. recInsert(nptr->right);
  235. }
  236. }
  237.  
  238. public:
  239. Dictionary():root(0){}
  240. Dictionary(const Dictionary& rhs){
  241. root=copy(rhs.root);
  242. this->size=rhs.size;
  243. }
  244. ~Dictionary(){clearAll(root);}
  245.  
  246. Dictionary& operator +=(const Dictionary& rhs){
  247. if(*this==rhs){
  248. Dictionary temp= rhs; //we need to check first check the roots if they are equal, we traverse to other nodes. First left then right
  249. recInsert(temp.root);
  250. }
  251. else{
  252. recInsert(rhs.root);
  253. }
  254. return *this;
  255. }
  256.  
  257. Dictionary operator +(const Dictionary& rhs){
  258. Dictionary temp=*this;
  259. temp += rhs;
  260. return temp;
  261. }
  262.  
  263. bool operator == (const Dictionary& rhs)const{
  264. if (height(root) != rhs.height(rhs.root)){
  265. return false;
  266. }
  267.  
  268. if(size!=rhs.size){
  269. return false;
  270. }
  271.  
  272. return compare(rhs.root);
  273. }
  274.  
  275. bool operator !=(const Dictionary& rhs)const{
  276. return !(*this==rhs);
  277. }
  278.  
  279. void insert(const key& k,const info& i){
  280. root = insert(root,k,i);
  281. size++;
  282. }
  283.  
  284. void remove(const key& k){
  285. root = deleteNode(root,k);
  286. size--;
  287. }
  288.  
  289. void print(int x)const{
  290. if(!root){cout << "Empty tree" << endl;return;}
  291. switch (x){
  292. case 0:
  293. cout << "Pre Order: ";
  294. preOrder(root);
  295. break;
  296. case 1:
  297. cout << "In Order: ";
  298. inOrder(root);
  299. break;
  300. case 2:
  301. cout << "Post Order: ";
  302. postOrder(root);
  303. break;
  304. default:
  305. cout << "invalid. exit." << endl;
  306. return;
  307. }
  308. cout <<endl;
  309. }
  310.  
  311.  
  312. Dictionary& operator=(const Dictionary &rhs){
  313. if(*this==rhs){return *this;}
  314. clearAll(root);
  315. root=copy(rhs.root);
  316. this->size=rhs.size;
  317. }
  318.  
  319. };
  320.  
  321.  
  322. int main(){
  323.  
  324.  
  325. Dictionary<int,int> a;
  326. a.insert(101,10);
  327. a.insert(201,20);
  328. a.insert(301,30);
  329. a.insert(401,40);
  330. a.insert(-50,50);
  331. a.insert(25,25);
  332. cout << "a: ";
  333. a.print(0);
  334.  
  335. /* a.insert(30,30);
  336. a.print();
  337. a.insert(99,0);
  338. a.print();
  339. a.remove(99);
  340. a.print();
  341. a.insert(100,100);
  342. a.print();
  343. a.insert(99,99);
  344. a.print();
  345. a.remove(100);
  346. a.print();
  347. //c.print();
  348. //a.remove(99);
  349. //a.print();
  350. /*a.remove(20);
  351. a.remove(25);
  352. a.remove(30);
  353. a.remove(40);
  354. a.remove(50);
  355. a.remove(99);
  356. a.print();
  357. //*/
  358. //a.print();
  359. ///*
  360. Dictionary<int,int> b;
  361. b.insert(30,30);
  362. b.print(0);
  363. b.insert(20,20);
  364. b.print(0);
  365. b.insert(10,10);
  366. b.print(0);
  367. b.insert(25,25);
  368. b.print(0);
  369. b.insert(50,50);
  370. b.print(0);
  371. b.insert(40,40);
  372. b.print(0);
  373. b.insert(99,99);
  374. b.print(0);
  375. b.remove(99);
  376. b.print(0);
  377. b.insert(99,100);
  378. b.print(0);
  379. b.insert(100,100);
  380. b.print(0);
  381. b.insert(115,115);
  382. b.print(0);
  383. b.insert(120,120);
  384. b.print(0);
  385. b.remove(10);
  386. b.remove(25);
  387. cout << "b: ";
  388. b.print(0);
  389. Dictionary<int,int> c=b;
  390. c.print(0);
  391. c.insert(10,0);
  392. c.insert(25,0);
  393. c.print(0);
  394. Dictionary<int,int> k;
  395. k.insert(1,1);
  396. k.print(0);
  397. k+=c;
  398. k.print(0);
  399. cout << "c: ";
  400. c.print(0);
  401. //k+=b;
  402. //k.print(0);
  403. k+=a;
  404. cout << "k: ";
  405. k.print(0);
  406. //cout << "b: ";
  407. //b.print(0);
  408. Dictionary<int,int> s;
  409. s=c+a;
  410. cout << "s: ";
  411. s.print(0);
  412. Dictionary<int,int> l=s;
  413. cout << "l: ";
  414. l.print(0);
  415. if(k!=s){
  416. cout << "k!=s" << endl;
  417. }
  418. if(l==s){
  419. cout << "l==s" << endl;
  420. }
  421. //*/
  422. /*
  423. a=b;
  424. a.print(0);
  425. a.insert(11,11); // here -1
  426. cout << a.root->right->right->right->nkey << endl;
  427. a.print(0);
  428. //*/
  429. /*
  430. Dictionary<int,int> c=b;
  431. c.print(0);
  432. c.remove(99);
  433. c.print(0);
  434.  
  435. //c.insert(100,100);
  436. ///c.print(0);
  437. c.insert(99,99);
  438. c.print(0);
  439. c.insert(100,100);
  440. c.print(0); //here +1
  441. c.remove(100);
  442. cout << c.height(c.root)<<endl;
  443. c.print(0);
  444. //Dictionary<int,int> e;
  445. //e.print(0);
  446. //c.print(0);
  447. //int b = getBalance(c.root);
  448. //cout << b;// << endl;
  449. //a.remove(100);*/
  450.  
  451. /*
  452. a.remove(30);
  453. a.print(0);
  454. a.remove(20);
  455. a.print(0);
  456. a.insert(100,100);
  457. a.insert(5,5);
  458. a.print(0);
  459. a.remove(50);
  460. a.print(0);
  461. //Dictionary<int,int> d;//=a;
  462. // d.insert(1,1);
  463. //d=a;
  464. //d.print(0);
  465. //if((d==a)){
  466. // cout << " e";
  467. //}
  468. /*a.insert(5,5);
  469. a.insert(-5,-5);
  470. a.print(0);
  471. a.insert(-10,-10);
  472. */
  473.  
  474. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement