#include <iostream> struct Node{ int key; int leftchildind; int rightchildind; int…
- User
#include
struct Node{ int key; int left_child_ind; int right_child_ind; int height = 0; int balance; };
int GetHeight(Node* tree, int ind) { return tree[ind].height; }
int GetBalance(Node* tree, int ind) { return tree[ind].balance; }
Node GetNode(Node* tree, int ind) { return tree[ind]; }
void AddHeight(Node*& tree, int ind) { if (ind == 0) { return; } AddHeight(tree, tree[ind].left_child_ind); AddHeight(tree, tree[ind].right_child_ind); int left_subtree_height = GetHeight(tree, tree[ind].left_child_ind); int right_subtree_height = GetHeight(tree, tree[ind].right_child_ind); if (left_subtree_height > right_subtree_height) { tree[ind].height = left_subtree_height + 1; } else { tree[ind].height = right_subtree_height + 1; } }
void RotateLeft(Node*& tree) { Node par = GetNode(tree, tree[1].right_child_ind); //tree[1] }
int main() { int n; std::cin >> n; Node* tree = new Node[n + 1]; for (int i = 1; i < n + 1; ++i) { std::cin >> tree[i].key >> tree[i].left_child_ind >> tree[i].right_child_ind; } AddHeight(tree, 1); for (int i = 1; i < n + 1; ++i) { tree[i].balance = GetHeight(tree, tree[i].right_child_ind) - GetHeight(tree, tree[i].left_child_ind) << '\n'; } if (GetBalance(tree, tree[1].right_child_ind) != 1) {
code} return 0;} implement rotate left
- Assistant
In order to implement a left rotation on an AVL tree (or any binary search tree), you need to adjust the pointers of the nodes involved in the rotation. Suppose you're rotating around a node called (which is the right child of the root in a left rotation). Here is how the rotation will work:
- User
#include
struct Node{ int key; int left_child_ind; int right_child_ind; int height = 0; int balance; };
int GetHeight(Node* tree, int ind) { return tree[ind].height; }
int GetBalance(Node* tree, int ind) { return tree[ind].balance; }
void AddHeight(Node*& tree, int ind) { if (ind == 0) { return; } AddHeight(tree, tree[ind].left_child_ind); AddHeight(tree, tree[ind].right_child_ind); int left_subtree_height = GetHeight(tree, tree[ind].left_child_ind); int right_subtree_height = GetHeight(tree, tree[ind].right_child_ind); if (left_subtree_height > right_subtree_height) { tree[ind].height = left_subtree_height + 1; } else { tree[ind].height = right_subtree_height + 1; } }
void RotateLeft(Node*& tree) { int new_root_ind = tree[1].right_child_ind; Node new_root = tree[new_root_ind]; tree[1].right_child_ind = new_root.left_child_ind; new_root.left_child_ind = new_root_ind; AddHeight(tree, 1); AddHeight(tree, new_root_ind); }
- Assistant
- User
switch my code to a pointer-based representation with actual Node pointers
- Assistant
- User
#include
struct Node{ int key; Node* left; Node* right; int left_child_ind; int right_child_ind; int height; int balance; Node(int key, int l_ind, int r_ind) : key(key), left(nullptr), right(nullptr), left_child_ind(l_ind), right_child_ind(r_ind), height(0) {} };
- Assistant
- User
- Assistant