Answer the question
In order to leave comments, you need to log in
How to write an AVL tree template class constructor?
The task is as follows:
0. Describe the abstract template class binary_tree, which is an abstraction of the search tree (element type is specified in the only template parameter). The class must contain three pure virtual functions: adding an element to the tree, removing an element from the tree, searching for an element in the tree by value. The class constructor must take as a parameter a template comparator (the rule for calculating the order relation between data in tree nodes) implemented as a strategy object (see the “Strategy” pattern).
1. Based on the class from task 0, implement the AVL tree template class. Generate pseudo-random data samples and, based on them, perform a comparative analysis of the running time and the number of rotation operations in the insert / delete / search algorithms for the AVL tree
#pragma once
/* Comparator */
template <typename T>
class Comparator
{
public:
virtual ~Comparator() {}
virtual int compare(const T &obj1, const T &obj2) const = 0;
};
/* Template binary tree class */
template <typename T>
class binary_tree
{
protected:
struct btree_node
{
T *data;
btree_node *left;
btree_node *right;
};
protected:
Comparator<T> *compare;
struct btree_node *node;
public:
binary_tree(Comparator<T> *compare);
~binary_tree();
void set_comparation_strategy(Comparator<T> *compare);
// virtual T *add_node(const T &elem) = 0;
// virtual T *find_node(const T &elem) = 0;
// virtual void delete_node(const T &elem) = 0;
private:
void delete_tree(btree_node *node);
};
template <typename T>
binary_tree<T>::binary_tree(Comparator<T> *compare)
{
node = nullptr;
this->compare = compare;
}
template <typename T>
binary_tree<T>::~binary_tree()
{
if (compare != nullptr)
delete compare;
if (node != nullptr)
delete_tree(node);
}
template <typename T>
void binary_tree<T>::set_comparation_strategy(Comparator<T> *compare)
{
if (this->compare != nullptr)
{
delete this->compare;
this->compare = compare;
}
}
template <typename T>
void binary_tree<T>::delete_tree(btree_node *node)
{
if (node)
{
delete_tree(node->left);
delete_tree(node->right);
delete node;
}
}
#pragma once
#include "binary_tree.h"
template <typename T>
class Comporator_avl_tree : public Comparator<T>
{
virtual int compare(const T &obj1, const T &obj2) const override
{
if (obj1 > obj2)
return (1);
else if (obj1 < obj2)
return (-1);
return (0);
}
};
template <typename T>
class avl_tree : public binary_tree<T>
{
private:
int *key;
public:
avl_tree();
~avl_tree();
};
template <typename T>
avl_tree<T>::avl_tree()
{
key = new int;
if (key != nullptr)
*key = 0;
}
template <typename T>
avl_tree<T>::~avl_tree()
{
if (key != nullptr)
delete key;
}
In file included from main.cpp:3:
avl_tree.h: In instantiation of ‘avl_tree<T>::avl_tree() [with T = int]’:
main.cpp:9:16: required from here
avl_tree.h:30:23: error: no matching function for call to ‘binary_tree<int>::binary_tree()’
30 | avl_tree<T>::avl_tree()
| ^
In file included from avl_tree.h:3,
from main.cpp:3:
binary_tree.h:44:1: note: candidate: ‘binary_tree<T>::binary_tree(Comparator<T>*) [with T = int]’
44 | binary_tree<T>::binary_tree(Comparator<T> *compare)
| ^~~~~~~~~~~~~~
binary_tree.h:44:1: note: candidate expects 1 argument, 0 provided
binary_tree.h:16:7: note: candidate: ‘constexpr binary_tree<int>::binary_tree(const binary_tree<int>&)’
16 | class binary_tree
| ^~~~~~~~~~~
binary_tree.h:16:7: note: candidate expects 1 argument, 0 provided
Answer the question
In order to leave comments, you need to log in
#include <iostream>
using namespace std;
typedef enum {
NotEqual,
Less,
Equal,
Greater
} CompareResult;
template <typename _T>
struct CF {
typedef CompareResult (*CompareFunc)(const _T&, const _T&);
};
template <typename _T>
class avl_tree
{
protected:
typename CF<_T>::CompareFunc m_cf;
public:
avl_tree(typename CF<_T>::CompareFunc func) : m_cf(func), m_tree(nullptr) {}
};
CompareResult compare_int(const int& item1, const int& item2)
{
if (item1 < item2)
return Less;
if (item1 > item2)
return Greater;
return Equal;
}
int main()
{
avl_tree<int> int_tree(compare_int);
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question