AVLCommands.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <iostream>
  2. #include "AVLCommands.h"
  3. #include "BST.h"
  4. // AVL explanation:
  5. // http://www.btechsmartclass.com/DS/U5_T2.html
  6. // combination of geeksforgeeks AVL insert and delete code
  7. // https://www.geeksforgeeks.org/avl-tree-set-2-deletion/
  8. // written for C
  9. // An AVL tree node struct that is probably useless to me
  10. struct Node { // already have a struct inside BST...
  11. // maybe no pointers in here...
  12. // but then why bother with a struct :/
  13. int key;
  14. //std::shared_ptr<BSTNode> currentNode = root_;
  15. //struct Node *left;
  16. //struct Node *right;
  17. int height;
  18. };
  19. // constructor: builds an AVL tree from BST tree bst
  20. AVLCommands::AVLCommands() {
  21. class BST bst(); //
  22. } // initialize BST with root null and sized 0
  23. void AVLCommands::Insert(int key) {
  24. // normal BST insertion
  25. bst.Insert(key);
  26. std::shared_ptr<BSTNode> currentNode = root_;
  27. // update height of this ancestor node
  28. // height = 1 + max(height(node->left), height(node->right))
  29. // check if the tree is unbalanced by checking
  30. // ancestor balance factor
  31. // if unbalanced
  32. // left-left
  33. // right-right
  34. // left-right
  35. // right-left
  36. }