blob: 59c452a3b7344ab7835ce2aa1e274ff201184db7 (
plain) (
blame)
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
|
#include <iostream>
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;
}
|