buildTree.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include "json.hpp"
  7. #include "AVLCommands.h"
  8. int main(int argc, char** argv) {
  9. // read JSON file and store in jsonObject
  10. std::ifstream file;
  11. file.open(argv[1]);
  12. nlohmann::json jsonObject;
  13. // Store the contents filename into jsonObject
  14. if (file.is_open()) {
  15. file >> jsonObject;
  16. }
  17. std::string AVLOp; // heap operation to iterate
  18. std::string currOp; // operation name
  19. std::string avlOutput; // outputted AVL tree
  20. int key; // node key given for each operation
  21. // construct AVL tree class
  22. AVLCommands avl;
  23. // need to iterate through each operation
  24. for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
  25. AVLOp = itr.key();
  26. if (AVLOp != "metadata") {
  27. // get the operation name to be performed
  28. currOp = jsonObject[AVLOp]["operation"];
  29. // perform correct operation gotten from JSON Op object
  30. if (currOp == "Insert") {
  31. key = jsonObject[AVLOp]["key"];
  32. avl.Insert(key);
  33. } else if (currOp == "Delete") {
  34. key = jsonObject[AVLOp]["key"];
  35. avl.Delete(key);
  36. } else if (currOp == "DeleteMin") {
  37. avl.DeleteMin();
  38. }
  39. }
  40. }
  41. // print tree using JSON formatting
  42. avlOutput = avl.JSON();
  43. std::cout << avlOutput << std::endl;
  44. // clean up memory
  45. file.close();
  46. return 0;
  47. }