| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include "json.hpp"
- #include "AVLCommands.h"
- int main(int argc, char** argv) {
- // read JSON file and store in jsonObject
- std::ifstream file;
- file.open(argv[1]);
- nlohmann::json jsonObject;
- // Store the contents filename into jsonObject
- if (file.is_open()) {
- file >> jsonObject;
- }
- std::string AVLOp; // heap operation to iterate
- std::string currOp; // operation name
- std::string avlOutput; // outputted AVL tree
- int key; // node key given for each operation
- // construct AVL tree class
- AVLCommands avl;
- // need to iterate through each operation
- for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
- AVLOp = itr.key();
- if (AVLOp != "metadata") {
- // get the operation name to be performed
- currOp = jsonObject[AVLOp]["operation"];
- // perform correct operation gotten from JSON Op object
- if (currOp == "Insert") {
- key = jsonObject[AVLOp]["key"];
- avl.Insert(key);
- } else if (currOp == "Delete") {
- key = jsonObject[AVLOp]["key"];
- avl.Delete(key);
- } else if (currOp == "DeleteMin") {
- avl.DeleteMin();
- }
- }
- }
- // print tree using JSON formatting
- avlOutput = avl.JSON();
- std::cout << avlOutput << std::endl;
- // clean up memory
- file.close();
- return 0;
- }
|