1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include<iostream>
#include <cstdlib>
#include <bits/stdc++.h>
#include <array>
#include <string>
#include <fstream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;
/* Create an AVL tree as subclass BST */
template <class T>
class AVLTree{
public:
AVLTree() // constructor
{
int key;
AVLTree *left;
AVLTree *right;
}
// create a new node
AVLTree<AVLTree>* addNode(int data)
{
AVLTree<AVLTree>* node = new AVLTree;
node->key = data;
node->left = node->right = NULL;
return (node);
}
//destructor
~AVLTree()
{
delete left;
delete right;
cout << "\nDeleting " << this->key << endl;
}
// Function visit inorder traversal on tree
void printBT(AVLTree<AVLTree>* root)
{
if (root == NULL) {
return;
}
printBT(root->left);
cout << root->key << " ";
printBT(root->right);
}
// Function: add a key into a BST (overloaded)
AVLTree<AVLTree>* addNode(AVLTree* root, T key)
{
// if the root is null, create a new node and return it
if (root == NULL) {
return addNode(key);
}
// if key < root node, recurse left subtree
if (key < root->key) {
root->left = addNode(root->left, key);
}
// if key > node, recurse right subtree
else {
root->right = addNode(root->right, key);
}
return root;
}
// Function: Construct balanced BST from given sorted array.
void convert(T keys[], T low, T high, AVLTree* &root)
{
// base case
if (low > high) {
return;
}
// find the middle element of the current range
int mid = (low + high) / 2;
// construct a new node from the middle element and assign it to the root
root = addNode(keys[mid]);
// left subtree of the root will be formed by keys less than middle element
convert(keys, low, mid - 1, root->left);
// right subtree of the root will be formed by keys more than the middle element
convert(keys, mid + 1, high, root->right);
}
// Function: Construct balanced BST from the given unsorted array
// AVLTree* BuildAVLTree(T arr[])
AVLTree* BuildAVLTree(T filename)
{
ifstream outputFile (filename);
if (outputFile.is_open()) {
string line;
getline (filename,line);
cout << line << '\n';
outputFile.close();
}
else cout << "Unable to open file";
/*
intention is to read filenmame from a text file then create an array
based on the reading. BUT still need the above code to work first
*/
const int arrSize = 8;
int arr[] = {1,2,3,4,5,6,7}; // anything just to make things work for now
// sort the keys first
std::sort(arr, arr + arrSize);
// construct a balanced BST
AVLTree* root = NULL;
convert(arr, 0, arrSize - 1, root);
// return root node of the tree
return root;
}
};
int main() {
string filename ="inputFile_A3"; // input file name
AVLTree<string>* avl;
avl = avl->BuildAVLTree(filename); // this line is the source of the error
}
|