Answer :
To construct an AVL Tree, which is a self-balancing binary search tree, you need to insert the given numbers one by one, ensuring that after each insertion, the tree remains balanced. Here's how it can be done with the provided data:
Insertion:
Insert 30:
- Tree: 30
Insert 20:
- Tree: 30
/
20
- Tree: 30
Insert 10:
- This insertion causes the tree to become unbalanced (Left-Left Case).
- Perform Right Rotation around 30.
- Tree: 20
/ \
10 30
Insert 25:
- Tree: 20
/ \
10 30
/
25
- Tree: 20
Insert 40:
- Tree: 20
/ \
10 30
/ \
25 40
- Tree: 20
Insert 50:
- Tree becomes unbalanced after insertion (Right-Right Case).
- Perform Left Rotation around 30.
- Tree: 20
/ \
10 40
/ \
30 50
/
25
Insert 22:
- Tree: 20
/ \
10 40
/ \
30 50
/
25
\
22
- Tree: 20
Insert 27:
- Tree becomes unbalanced by this insertion (Left-Right Case at 30).
- Perform Left Rotation around 25, then Right Rotation around 30.
- Tree: 20
/ \
10 40
/ \
27 50
/
25
/ \
22 30
Insert 5:
- Tree: 20
/ \
10 40
/ / \
5 27 50
/
25
/
22 30- Tree: 20
Insert 15:
- Tree becomes unbalanced at 10 after this insertion (Right-Left Case).
- Perform Right Rotation around 10.
- Tree: 20
/ \
15 40
/ \ / \
10 5 27 50
/
25
/ \
22 30
Deletion:
Now, let's perform deletions in the order provided:
Delete 20:
- 20 is replaced by its successor from the right subtree (22).
- Tree becomes: 22
/ \
15 40
/ \ / \
5 10 27 50
/
25
\
30
Delete 40:
- 40 is replaced by its successor (50).
- Tree: 22
/ \
15 50
/ \ /
5 10 27
/
25
\
30
Delete 15:
- 15 is replaced by its successor (10).
- Tree: 22
/ \
10 50
/ /
5 27
/
25
\
30
Delete 25:
- 25 is replaced by its successor (27).
- Tree: 22
/ \
10 50
/
5 27
\
30
At each step when the AVL tree is modified, rotations (left or right) are performed to ensure that the tree is balanced, maintaining the AVL property where the difference between heights of left and right subtrees for any node is no more than one.