Guest User

Untitled

a guest
May 26th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node
  5. {
  6. public:
  7. Node* left;
  8. Node* right;
  9. char height;
  10. int key;
  11. string data;
  12.  
  13. Node(int key,string* data);
  14. };
  15.  
  16. Node::Node(int key,string* data)
  17. {
  18. this->data.reserve(196); // size of int is 4 byte so 4 + 196 = 200
  19. this->key = key;
  20. this->data = (*data);
  21. height = 0;
  22. left = nullptr;
  23. right = nullptr;
  24. }
  25.  
  26. class AVLtree
  27. {
  28. private:
  29. Node* root;
  30.  
  31. bool Insert(int k,string* d, Node** leaf);
  32.  
  33. void PrettyPrint(Node* n, string pre);
  34.  
  35. void InOrder(Node* n);
  36.  
  37. Node* Search(int k,Node* leaf);
  38.  
  39. void deleteA(Node** currPtr);
  40.  
  41. void deleteBleft(Node** currPtr);
  42.  
  43. void deleteBright(Node** currPtr);
  44.  
  45. void deleteC(Node** currPtr);
  46.  
  47. bool SetRightest(Node** n,Node*** ret);
  48.  
  49. char Height(Node* n);
  50.  
  51. void RightRotate(Node** n);
  52.  
  53. void LeftRotate(Node** n);
  54.  
  55. char NewHeight(Node* n);
  56.  
  57. bool Remove(int key,Node** n);
  58.  
  59. void Free(Node* n);
  60.  
  61. public:
  62. void Add(int key,string data);
  63.  
  64. string Search(int key);
  65.  
  66. void InOrder();
  67.  
  68. void Remove(int key);
  69.  
  70. void Clear();
  71.  
  72. AVLtree();
  73.  
  74. ~AVLtree();
  75.  
  76. void PrettyPrint();
  77.  
  78.  
  79. };
  80.  
  81. bool AVLtree::Insert(int k,string* d, Node** leaf)
  82. {
  83. bool h = false;
  84. if(k<(*leaf)->key)
  85. {
  86. if((*leaf)->left!=nullptr)
  87. {
  88. h = Insert(k,d,&((*leaf)->left));
  89. if (h)
  90. {
  91. if((Height((*leaf)->right)-Height((*leaf)->left))<-1)
  92. {
  93. if(Height((*leaf)->left->right)-Height((*leaf)->left->left)>0)
  94. {
  95. LeftRotate(&((*leaf)->left));
  96. }
  97. RightRotate(leaf);
  98. }
  99. }
  100. }
  101. else
  102. {
  103. (*leaf)->left = new Node(k,d);
  104. h=true;
  105. }
  106. }
  107. else
  108. {
  109. if(k>(*leaf)->key)
  110. {
  111. if((*leaf)->right!=nullptr)
  112. {
  113. h = Insert(k,d,&((*leaf)->right));
  114. if(h)
  115. {
  116. if((Height((*leaf)->right)-Height((*leaf)->left))>1)
  117. {
  118. if(Height((*leaf)->right->right)-Height((*leaf)->right->left)<0)
  119. {
  120. RightRotate(&((*leaf)->right));
  121. }
  122. LeftRotate(leaf);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. (*leaf)->right = new Node(k,d);
  129. h = true;
  130. }
  131. }
  132. else
  133. {
  134. (*leaf)->data = (*d); //node is already in the tree
  135. }
  136. }
  137. if (h)
  138. {
  139. char nh = NewHeight((*leaf));
  140. if (nh!=(*leaf)->height)
  141. {
  142. (*leaf)->height = nh;
  143. }
  144. else
  145. {
  146. h=false;
  147. }
  148. }
  149. return h;
  150. }
  151.  
  152. void AVLtree::PrettyPrint(Node* n, string pre)
  153. {
  154. if(n!=nullptr)
  155. {
  156. cout<<pre<<(n->key)<<" "<<(n->data)<<" "<<(int)(n->height)<<"\n";
  157. PrettyPrint(n->left," "+pre);
  158. PrettyPrint(n->right," "+pre);
  159. }
  160. }
  161.  
  162. bool AVLtree::Remove(int key,Node** n)
  163. {
  164. bool h = false; // height changed
  165. bool d = false; // child has been removed
  166. if((*n)!=nullptr)
  167. {
  168. if((*n)->key==key)
  169. {
  170. d = true;
  171. Node** currPtr = n;
  172. if((*currPtr)->left==nullptr&&(*currPtr)->right==nullptr)
  173. {
  174. deleteA(currPtr);
  175. }
  176. else
  177. {
  178. if((*currPtr)->left!=nullptr&&(*currPtr)->right==nullptr)
  179. {
  180. deleteBleft(currPtr);
  181. h = true;
  182. }
  183. else
  184. {
  185. if((*currPtr)->left==nullptr&&(*currPtr)->right!=nullptr)
  186. {
  187. deleteBright(currPtr);
  188. h = true;
  189. }
  190. else
  191. {
  192. if((*currPtr)->left!=nullptr&&(*currPtr)->right!=nullptr)
  193. {
  194. deleteC(currPtr);
  195. h = true;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. else
  202. {
  203. if(key<((*n)->key))
  204. {
  205. h = Remove(key,&((*n)->left));
  206. if(h)
  207. {
  208. if((Height((*n)->right)-Height((*n)->left))>1)
  209. {
  210. d = true;
  211. if(Height((*n)->right->right)-Height((*n)->right->left)<0)
  212. {
  213. RightRotate(&((*n)->right));
  214. }
  215. LeftRotate(n);
  216. }
  217. }
  218. }
  219. else
  220. {
  221. if (key>((*n)->key))
  222. {
  223. h = Remove(key,&((*n)->right));
  224. if (h)
  225. {
  226. if((Height((*n)->right)-Height((*n)->left))<-1)
  227. {
  228. d = true;
  229. if(Height((*n)->left->right)-Height((*n)->left->left)>0)
  230. {
  231. LeftRotate(&((*n)->left));
  232. }
  233. RightRotate(n);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. if (h)
  240. {
  241. char nh = NewHeight((*n));
  242. if (nh!=(*n)->height)
  243. {
  244. (*n)->height = nh;
  245. }
  246. else
  247. {
  248. h=false;
  249. }
  250. }
  251. }
  252. return (h||d);
  253. }
  254.  
  255. void AVLtree::InOrder(Node* n)
  256. {
  257. if(n!=nullptr)
  258. {
  259. InOrder(n->left);
  260. cout<<"\n"<<n->key;
  261. InOrder(n->right);
  262. }
  263. }
  264.  
  265. Node* AVLtree::Search(int k,Node* leaf)
  266. {
  267. if (leaf==nullptr)
  268. {
  269. return nullptr;
  270. }
  271. if (leaf->key==k)
  272. {
  273. return leaf;
  274. }
  275. else
  276. {
  277. if(k<leaf->key)
  278. {
  279. return Search(k,leaf->left);
  280. }
  281. else
  282. {
  283. return Search(k,leaf->right);
  284. }
  285. }
  286. }
  287. void AVLtree::deleteA(Node** currPtr) //when Node has no children
  288. {
  289. delete (*currPtr);
  290. (*currPtr)=nullptr;
  291. }
  292.  
  293. void AVLtree::deleteBleft(Node** currPtr) // when node has only the left child
  294. {
  295. Node* l = (*currPtr);
  296. (*currPtr)=(*currPtr)->left;
  297. delete (l);
  298. }
  299.  
  300. void AVLtree::deleteBright(Node** currPtr) //when node has only the right child
  301. {
  302. Node* l = (*currPtr);
  303. (*currPtr)=(*currPtr)->right;
  304. delete (l);
  305. }
  306.  
  307. void AVLtree::deleteC(Node** currPtr) //when node has both child
  308. {
  309. Node** leftMax; //the node with the greatest value in left subtree to replace current node
  310. SetRightest(&((*currPtr)->left),&leftMax);
  311. Node* oldleft = (*leftMax)->left;
  312. Node* old = (*currPtr);
  313. *currPtr = *leftMax;
  314. *leftMax = oldleft;
  315. (*currPtr)->left = old->left;
  316. (*currPtr)->right = old->right;
  317. if((Height((*currPtr)->right)-Height((*currPtr)->left))>1) //balance also needs to be checked here
  318. {
  319. if(Height((*currPtr)->right->right)-Height((*currPtr)->right->left)<0)
  320. {
  321. RightRotate(&((*currPtr)->right));
  322. }
  323. LeftRotate(currPtr);
  324. }
  325. (*currPtr)->height = NewHeight(*currPtr);
  326. delete old;
  327.  
  328. }
  329.  
  330. bool AVLtree::SetRightest(Node** n,Node*** ret) //to get pointer to pointer to the greatest node in left subtree
  331. {
  332. bool h = false;
  333. if((*n)->right!=nullptr)
  334. {
  335. h = SetRightest(&((*n)->right),ret);
  336. if(h)
  337. {
  338. if((Height((*n)->right)-Height((*n)->left))<-1)
  339. {
  340. if(Height((*n)->left->right)-Height((*n)->left->left)>0)
  341. {
  342. LeftRotate(&((*n)->left));
  343. }
  344. RightRotate(n);
  345. }
  346. char nh = NewHeight((*n));
  347. if(nh<(*n)->height)
  348. {
  349. (*n)->height=nh;
  350. }
  351. else
  352. {
  353. h = false;
  354. }
  355. }
  356. }
  357. else
  358. {
  359. (*ret) = n;
  360. (*n)->height=-1; //height of non-existing subtree
  361. h=true;
  362. }
  363. return h;
  364. }
  365.  
  366. char AVLtree::Height(Node* n) //height of subtree + 1
  367. {
  368. if(n==nullptr)
  369. {
  370. return 0;
  371. }
  372. else
  373. {
  374. return (n->height)+1;
  375. }
  376. }
  377.  
  378. void AVLtree::RightRotate(Node** n)
  379. {
  380. Node* temp = ((*n)->left);
  381. ((*n)->left)=temp->right;
  382. temp->right=(*n);
  383. (*n)=temp;
  384. (*n)->right->height=NewHeight((*n)->right);
  385. (*n)->height = NewHeight(*n);
  386. }
  387.  
  388. void AVLtree::LeftRotate(Node** n)
  389. {
  390. Node* temp = ((*n)->right);
  391. ((*n)->right)=temp->left;
  392. temp->left=(*n);
  393. (*n)=temp;
  394. (*n)->left->height=NewHeight((*n)->left);
  395. (*n)->height = NewHeight(*n);
  396. }
  397.  
  398. char AVLtree::NewHeight(Node* n)
  399. {
  400. char lh = Height(n->left);
  401. char rh = Height(n->right);
  402. if(rh>lh)
  403. {
  404. return rh;
  405. }
  406. else
  407. return lh;
  408. }
  409.  
  410. void AVLtree::Add(int key,string data)
  411. {
  412. if (root != nullptr)
  413. {
  414. Insert(key,&data,&root);
  415. }
  416. else
  417. {
  418. root = new Node(key,&data);
  419. }
  420. }
  421.  
  422. string AVLtree::Search(int key) //returns data value of a node or nullptr if node was not found
  423. {
  424. Node* temp = Search(key,root);
  425. if (temp!=nullptr)
  426. {
  427. return temp->data;
  428. }
  429. else
  430. {
  431. return "none";
  432. }
  433. }
  434.  
  435. void AVLtree::InOrder()
  436. {
  437. InOrder(root);
  438. }
  439.  
  440. void AVLtree::Remove(int key)
  441. {
  442. Remove(key,(&root));
  443. }
  444.  
  445. void AVLtree::Free(Node* n)
  446. {
  447. if(n!=nullptr)
  448. {
  449. Free(n->left);
  450. Free(n->right);
  451. delete n;
  452. }
  453. }
  454.  
  455. void AVLtree::Clear()
  456. {
  457. Free(root);
  458. root = nullptr;
  459. }
  460.  
  461. AVLtree::AVLtree()
  462. {
  463. root = nullptr;
  464. }
  465.  
  466. AVLtree::~AVLtree()
  467. {
  468. Clear();
  469. }
  470.  
  471. void AVLtree::PrettyPrint()
  472. {
  473. PrettyPrint(root, "|--");
  474. }
  475.  
  476. int main()
  477. {
  478. AVLtree myTree = AVLtree();
  479. int in;
  480. while (true) //simple test
  481. {
  482. cin>>in;
  483. if(in == 0)
  484. {
  485. break;
  486. }
  487. if(in>0)
  488. {
  489. myTree.Add(in,"");
  490. myTree.PrettyPrint();
  491. cout<<"\n\n";
  492. }
  493. else
  494. {
  495. myTree.Remove(-1*in);
  496. myTree.PrettyPrint();
  497. cout<<"\n\n";
  498. }
  499. }
  500. myTree.Clear();
  501. return 0;
  502. }
Add Comment
Please, Sign In to add comment