Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznetsov-Artyom committed Mar 22, 2024
1 parent 5cc496a commit 4e3e2d8
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions modules/kuznetsov_a_lab2/include/avltree.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ class avlTree {
[[nodiscard]] bool operator!=(const avlTree& other) const;

private:
Node<T>* insertNode(Node<T>** node, const T& value);
Node<T>* insertNode(Node<T>* node, const T& value);
Node<T>* balancing(Node<T>* node);
Node<T>* rightRotate(Node<T>* node);
Node<T>* leftRotate(Node<T>* node);
int balanceFactor(Node<T>* node);
void updHeight(Node<T>* node);
int heightNode(Node<T>* node);

Node<T>* removeNode(Node<T>** node, const T& value);
Node<T>* removeNode(Node<T>* node, const T& value);
Node<T>* findMinNode(Node<T>* node) noexcept;
Node<T>* removeMin(Node<T>* node);

const Node<T>* findNode(const Node<T>* node, const T& value) const noexcept;
Node<T>* copyTree(const Node<T>* otherRoot);
void traverseFillVector(const Node<T>* node, std::vector<T>* values) const;
void traverseFillVector(const Node<T>* node, std::vector<T>& values) const;
};

template <typename T>
Expand All @@ -86,15 +86,15 @@ avlTree<T>::avlTree(avlTree&& other) noexcept

template <typename T>
void avlTree<T>::insert(const T& value) {
mRoot = insertNode(&mRoot, value);
mRoot = insertNode(mRoot, value);
}

template <typename T>
void avlTree<T>::remove(const T& value) {
if (empty()) {
throw std::logic_error{"Tree is empty"};
}
mRoot = removeNode(&mRoot, value);
mRoot = removeNode(mRoot, value);
}

template <typename T>
Expand Down Expand Up @@ -125,7 +125,7 @@ template <typename T>

if (mSize) {
values.reserve(mSize);
traverseFillVector(mRoot, &values);
traverseFillVector(mRoot, values);
}

return values;
Expand Down Expand Up @@ -175,20 +175,20 @@ template <typename T>
////////////////////// PRIVATE METHODS //////////////////////

template <typename T>
Node<T>* avlTree<T>::insertNode(Node<T>** node, const T& value) {
if (*node == nullptr) {
Node<T>* avlTree<T>::insertNode(Node<T>* node, const T& value) {
if (node == nullptr) {
++mSize;
return new Node<T>(value, 0);
}

if (value < (*node)->key)
(*node)->left = insertNode(&(*node)->left, value);
else if (value > (*node)->key)
(*node)->right = insertNode(&(*node)->right, value);
if (value < node->key)
node->left = insertNode(node->left, value);
else if (value > node->key)
node->right = insertNode(node->right, value);
else
return *node;
return node;

return balancing(*node);
return balancing(node);
}

template <typename T>
Expand Down Expand Up @@ -255,20 +255,20 @@ int avlTree<T>::heightNode(Node<T>* node) {
}

template <typename T>
Node<T>* avlTree<T>::removeNode(Node<T>** node, const T& value) {
if (*node == nullptr) return nullptr;
Node<T>* avlTree<T>::removeNode(Node<T>* node, const T& value) {
if (node == nullptr) return nullptr;

if (value > (*node)->key) {
(*node)->right = removeNode(&(*node)->right, value);
} else if (value < (*node)->key) {
(*node)->left = removeNode(&(*node)->left, value);
if (value > node->key) {
node->right = removeNode(node->right, value);
} else if (value < node->key) {
node->left = removeNode(node->left, value);
} else {
Node<T>* leftNode = (*node)->left;
Node<T>* rightNode = (*node)->right;
Node<T>* leftNode = node->left;
Node<T>* rightNode = node->right;

(*node)->left = nullptr;
(*node)->right = nullptr;
delete *node;
node->left = nullptr;
node->right = nullptr;
delete node;
--mSize;

if (rightNode == nullptr) return leftNode;
Expand All @@ -281,7 +281,7 @@ Node<T>* avlTree<T>::removeNode(Node<T>** node, const T& value) {
return balancing(minNode);
}

return balancing(*node);
return balancing(node);
}

template <typename T>
Expand Down Expand Up @@ -323,10 +323,10 @@ Node<T>* avlTree<T>::copyTree(const Node<T>* otherRoot) {

template <typename T>
void avlTree<T>::traverseFillVector(const Node<T>* node,
std::vector<T>* values) const {
std::vector<T>& values) const {
if (node == nullptr) return;

traverseFillVector(node->left, values);
values->emplace_back(node->key);
values.emplace_back(node->key);
traverseFillVector(node->right, values);
}

0 comments on commit 4e3e2d8

Please sign in to comment.