From 981037e28374f8c4ab44e26bdeda62da59f21a21 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Wed, 4 Dec 2024 17:15:45 -0500 Subject: Add sem08 --- Seminar08/src/rotations.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Seminar08/src/rotations.cpp (limited to 'Seminar08/src/rotations.cpp') diff --git a/Seminar08/src/rotations.cpp b/Seminar08/src/rotations.cpp new file mode 100644 index 0000000..59c452a --- /dev/null +++ b/Seminar08/src/rotations.cpp @@ -0,0 +1,47 @@ +#include + +struct node +{ + int data; + node* left; + node* right; + + node(int data_, node* left_ = nullptr, node* right_ = nullptr) : + data(data_), + left(left_), + right(right_) {} +}; + +node* leftRotate(node* root) +{ + if(root == nullptr || root->right == nullptr) + return nullptr; + + node* originalRight = root->right; + root->right = originalRight->left; + originalRight->left = root; + + return originalRight; +} + +node* rightRotate(node* root) +{ + if(root == nullptr || root->left == nullptr) + return nullptr; + + node* originalLeft = root->left; + root->left = originalLeft->right; + originalLeft->right = root; + + return originalLeft; +} + +int main() +{ + node* example = new node(2, new node(1), new node(3)); + example = leftRotate(example); + example = rightRotate(example); + + std::cout << example->data << " " << example->left->data << " " << example->right->data; + +} -- cgit v1.3.1-26-gb76a