Advertisement
Guest User

avl tree

a guest
Sep 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. class Node{ // Class creates new node objects
  7. public:
  8. Node();
  9. int value;
  10. Node* left;
  11. Node* right;
  12. ~Node();
  13. };
  14.  
  15. class AVLTree{ // class creates new tree
  16. public:
  17. Node *root; // points to root of the tree
  18. bool first; // used for spacing in output
  19. std::vector<Node*> path;
  20. AVLTree();
  21. void insert(int number); // inserts number into tree - does nothing if already in tree
  22. void remove(int number); // removes number from tree- does nothing if not in tree
  23. bool isEmpty(); // Checks if 'root' points to NULL
  24. void traverse(std::string method); // Outputs using specified method
  25. void postOrder(Node* current); // different traversing methods
  26. void preOrder(Node* current); // " "
  27. void inOrder(Node* current); // " "
  28. int height(Node* current); // returns height of subtree
  29. int levelCount(Node* current, int total, int level);
  30. int heightDiff(Node* current); // returns difference in height of two subtrees of a given node
  31. void balance(); // desides on the rotation method
  32. void leftLeft(Node* current, Node* parent); // different rotation methods
  33. void rightRight(Node* current, Node* Parent); // " "
  34. ~AVLTree();
  35. };
  36.  
  37. AVLTree::AVLTree(){
  38. root = NULL;
  39. first = true;
  40. }
  41.  
  42. Node::Node(){
  43. left = NULL; //initialtes new node pointer to NULL and digit to 0
  44. right = NULL;
  45. value = 0;
  46. }
  47.  
  48. bool AVLTree::isEmpty(){
  49. return root == NULL;
  50. }
  51.  
  52. void AVLTree::balance(){
  53. Node* current = NULL;
  54. Node* parent = NULL;
  55. for(int i = 0 ; i < path.size(); i++){
  56. current = path[i];
  57. if(i > 0){
  58. parent = path[i-1];
  59. }
  60. int bal = heightDiff(current);
  61. if(bal > 1){
  62. if(heightDiff(current->left) >= 0){
  63. // std::cout << "left" << current->value << std::endl;
  64. leftLeft(current, parent);
  65. }else{
  66. // std::cout << "left right" << std::endl;
  67. rightRight(current->left, current);
  68. leftLeft(current, parent);
  69. }
  70. }else if(bal < -1){
  71. if(heightDiff(current->right) > 0){
  72. // std::cout << "right left" << std::endl;
  73. leftLeft(current->right, current);
  74. rightRight(current, parent);
  75. }else{
  76. // std::cout << "right" << current->value << std::endl;
  77. rightRight(current, parent);
  78. }
  79. }
  80. }
  81. path.clear();
  82. }
  83.  
  84. void AVLTree::rightRight(Node* current, Node* parent){
  85. Node* temp = current;
  86. current = current->right;
  87. temp->right = current->left;
  88. if(temp == root){
  89. root = current;
  90. }
  91. current->left = temp;
  92. if(parent != NULL){
  93. if(parent->left == temp){
  94. parent->left = current;
  95. }else if(parent->right == temp){
  96. parent->right = current;
  97. }
  98. }
  99. }
  100.  
  101. void AVLTree::leftLeft(Node* current, Node* parent){
  102. Node* temp = current;
  103. current = current->left;
  104. temp->left = current->right;
  105. if(temp == root){
  106. root = current;
  107. }
  108. current->right = temp;
  109. if(parent != NULL){
  110. if(parent->left == temp){
  111. parent->left = current;
  112. }else if(parent->right == temp){
  113. parent->right = current;
  114. }
  115. }
  116. }
  117.  
  118. int AVLTree::height(Node* current){
  119. int level = 1;
  120. int total = 0;
  121. total = levelCount(current,total,level);
  122. // std::cout << total << "height" << std::endl;
  123. return total;
  124. }
  125.  
  126. int AVLTree::levelCount(Node* current, int total, int level){
  127. if(current != NULL){
  128. // we have reached a lower level, increment the count
  129. level++;
  130. // keep track of the highest level number
  131. if (level > total) {
  132. total = level;
  133. }
  134. // go to the left, if there is something there, it will
  135. // see the level, if it is greater than the highest, we will record it.
  136. levelCount(current->left,total,level);
  137. // same on the right.
  138. levelCount(current->right,total,level);
  139. // we reached a new level, now we are going back
  140. level--;
  141. }
  142. return total;
  143. }
  144.  
  145. int AVLTree::heightDiff(Node* current){
  146. int heightL = height(current->left);
  147. int heightR = height(current->right);
  148. int diff = heightL - heightR;
  149. return diff;
  150. }
  151.  
  152. void AVLTree::postOrder(Node* current){
  153. if(current == NULL){
  154. return;
  155. }
  156. postOrder(current->left);
  157. postOrder(current->right);
  158. if(first != true){
  159. std::cout << " ";
  160. }else{
  161. first = false;
  162. }
  163. std::cout << current->value;
  164. }
  165.  
  166. void AVLTree::preOrder(Node* current){
  167. if(current == NULL){
  168. return;
  169. }
  170.  
  171. if(first != true){
  172. std::cout << " ";
  173. }else{
  174. first = false;
  175. }
  176. std::cout << current->value;
  177.  
  178. preOrder(current->left);
  179. preOrder(current->right);
  180. }
  181.  
  182. void AVLTree::inOrder(Node* current){
  183. if(current == NULL){
  184. return;
  185. }
  186. inOrder(current->left);
  187.  
  188. if(first != true){
  189. std::cout << " ";
  190. }else{
  191. first = false;
  192. }
  193. std::cout << current->value;
  194.  
  195. inOrder(current->right);
  196. }
  197.  
  198. void AVLTree::traverse(std::string method){
  199. // std::cout << std::endl;
  200. if(method == "IN"){
  201. AVLTree::inOrder(root);
  202. std::cout << std::endl;
  203. }
  204. if(method == "PRE"){
  205. AVLTree::preOrder(root);
  206. std::cout << std::endl;
  207. }
  208. if(method == "POST"){
  209. AVLTree::postOrder(root);
  210. std::cout << std::endl;
  211. }
  212. }
  213.  
  214. void AVLTree::insert(int number){
  215. Node* temp = new Node;
  216. temp->left = NULL;
  217. temp->right = NULL;
  218. temp->value = number;
  219. if(isEmpty()){ // if root is NULL update root
  220. root = temp;
  221. return;
  222. }
  223. Node* current = root;
  224. // std::cout << number << std::endl;
  225. while(current->value != number){ // while number is not found in the tree
  226. path.push_back(current);
  227. if(number > current->value){
  228. // std::cout << "GREATER" << std::endl;
  229. if(current->right == NULL){
  230. current->right = temp; //add new node
  231. break;
  232. }else{
  233. current = current->right; // move right side
  234. }
  235. }else if(number < current->value){
  236. // std::cout << "LESS" << std::endl;
  237. if(current->left == NULL){
  238. current->left = temp; // add new node
  239. break;
  240. }else{
  241. current = current->left; //move left side
  242. }
  243. }
  244. }
  245. balance();
  246. }
  247.  
  248. void AVLTree::remove(int number){
  249. if(isEmpty()){ // if root is NULL do thing
  250. return;
  251. }
  252. bool found = false;
  253. Node* current;
  254. Node* parent = NULL;
  255. Node* newNode = NULL;
  256. current = root;
  257. while(current != NULL){
  258. if(current->value == number){
  259. found = true;
  260. break;
  261. }else{
  262. path.push_back(current);
  263. parent = current;
  264. if(number > current->value){
  265. //move right side
  266. current = current->right;
  267. }else{
  268. //move left side
  269. current = current->left;
  270. }
  271. }
  272. }
  273. if(found == true){
  274. // leaf node (no child)
  275. if(current->left == NULL && current->right == NULL){
  276. if(parent == NULL){
  277. delete current;
  278. root = NULL;
  279. }else if(current == parent->left){
  280. parent->left = NULL;
  281. delete current;
  282. }else{
  283. parent->right = NULL;
  284. delete current;
  285. }
  286. }
  287. if(current->right != NULL && current->left == NULL){ // single child
  288. if(parent == NULL){
  289. root = current->right;
  290. path.push_back(root);
  291. delete current;
  292. }else if(current == parent->left){
  293. parent->left = current->right;
  294. path.push_back(parent->left);
  295. delete current;
  296. }else{
  297. parent->right = current->right;
  298. path.push_back(parent->right);
  299. delete current;
  300. }
  301. }else if(current->right == NULL && current->left != NULL){
  302. if(parent == NULL){
  303. root = current->left;
  304. path.push_back(root);
  305. delete current;
  306. }else if(current == parent->left){
  307. parent->left = current->left;
  308. path.push_back(parent->left);
  309. delete current;
  310. }else{
  311. parent->right = current->left;
  312. path.push_back(parent->right);
  313. delete current;
  314. }
  315. }
  316. if(current->right != NULL && current->left != NULL){ // two child nodes
  317. // smallest value in right subtree
  318. Node* currRight = current->right;
  319. if(currRight->right == NULL && currRight->left == NULL){
  320. current->value = currRight->value;
  321. current->right = NULL;
  322. delete currRight;
  323. }else if((current->right)->left != NULL){ // right node has left children
  324. Node* swapParent = current->right;
  325. Node* swapNode = swapParent->left;
  326.  
  327. while(swapNode->left != NULL){ // until smallest value
  328. swapParent = swapNode;
  329. path.push_back(swapParent);
  330. swapNode = swapNode->left;
  331. }
  332. current->value = swapNode->value;
  333. if(swapNode->right == NULL){
  334. delete swapNode;
  335. swapParent->left = NULL;
  336. }else{
  337. swapParent->right = swapNode->right;
  338. delete swapNode;
  339. }
  340. }else{ // right node had no left children
  341. current->value = currRight->value;
  342. current->right = currRight->right;
  343. delete currRight;
  344. }
  345. }
  346. }
  347. balance();
  348. }
  349.  
  350. Node::~Node(){
  351. }
  352.  
  353. AVLTree::~AVLTree(){
  354. }
  355.  
  356. int main(){
  357.  
  358. AVLTree tree;
  359.  
  360. std::string instruct;
  361. std::vector<char> AorD;
  362. std::vector<std::string> value;
  363. std::string traversal;
  364. int count = 0;
  365. // while(std::getline(std::cin, instruct)){
  366.  
  367. std::getline(std::cin, instruct);
  368. // instruct = "A20 A4 A8 PRE";
  369. int length = instruct.length();
  370. for(int i = 0; i < length; i++){
  371. if(instruct[i] == 'A' || instruct[i] == 'D'){
  372. AorD.push_back(instruct[i]);
  373. i++;
  374. value.push_back("0");
  375. while(isdigit(instruct[i])){
  376. count = value.size()-1;
  377. value[count] += instruct[i];
  378. i++;
  379. }
  380. }
  381. if(instruct[i] == 'I'){
  382. traversal = "IN";
  383. }else if(instruct[i] == 'P' && instruct[i+1] == 'O'){
  384. traversal = "POST";
  385. }else if(instruct[i] == 'P' && instruct[i+1] == 'R'){
  386. traversal = "PRE";
  387. }
  388. }
  389. for(int i = 0; i < AorD.size(); i++){
  390. if(AorD[i] == 'A'){
  391. tree.insert(std::stoi(value[i]));
  392. }else{
  393. tree.remove(std::stoi(value[i]));
  394. }
  395. }
  396. if(tree.isEmpty()){
  397. std::cout << "EMPTY" << std::endl;
  398. }else{
  399. tree.traverse(traversal);
  400. }
  401. instruct.clear();
  402. AorD.clear();
  403. value.clear();
  404. traversal = "";
  405. tree.first = true;
  406. tree.root = NULL;
  407. tree.path.clear();
  408. // }
  409. return 1;
  410. // std::cout << tree.root->left->value << std::endl;
  411. // std::cout << tree.root->value << std::endl;
  412. // std::cout << tree.root->right->value << std::endl;
  413.  
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement