0% found this document useful (0 votes)
0 views

Midsem_Level_Tree_AVL_Questions_with_Answers

The document contains practice questions and solutions related to Tree and AVL Tree data structures. It covers time complexities for inserting nodes, recursive functions for counting nodes, tree traversals, array and linked list representations, and comparisons between AVL Trees and Binary Search Trees. Additionally, it includes a step-by-step construction of an AVL Tree with specific keys and the resulting tree structure after each insertion.

Uploaded by

malviyat42
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Midsem_Level_Tree_AVL_Questions_with_Answers

The document contains practice questions and solutions related to Tree and AVL Tree data structures. It covers time complexities for inserting nodes, recursive functions for counting nodes, tree traversals, array and linked list representations, and comparisons between AVL Trees and Binary Search Trees. Additionally, it includes a step-by-step construction of an AVL Tree with specific keys and the resulting tree structure after each insertion.

Uploaded by

malviyat42
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tree & AVL Tree Practice Questions with Solutions

Q1. Q1.a) Calculate the time complexity for inserting n nodes in a Binary Search Tree (BST) in the b

Ans: a) In BST:

- Best case (balanced tree): O(log n)

- Worst case (skewed tree): O(n)

b) AVL Tree insertion: O(log n)

AVL maintains a balanced structure using rotations, limiting height to log n.

Q2. Q2.Write a recursive function to count the total number of nodes in a binary tree.Explain how th

Ans: Function:

int countNodes(Node* root) {

if (root == NULL) return 0;

return 1 + countNodes(root->left) + countNodes(root->right);

Explanation:

The call stack stores recursive calls until base cases are hit, and then combines results on backtracking.

Q3. Q3 a.Perform In-order, Pre-order, and Post-order traversal of the following binary tree: A

Ans: In-order: D B E A C

Pre-order: A B D E C

Post-order: D E B C A

Q4. Q3 b.Represent the above tree using:i) Array Representation (index starts from 1)ii) Linked List
Tree & AVL Tree Practice Questions with Solutions

Ans: i) Array (index): [_, A, B, C, D, E]

B is at index 2, its children are at 2*2=4 (D) and 2*2+1=5 (E)

Parent of B is at index 2//2 = 1 (A)

ii) Linked List:

struct Node {

char data;

Node* left;

Node* right;

};

Q5. Q4 a.Compare AVL Tree vs Binary Search Tree:Balance factorTime complexity (Search, Insert, D

Ans: AVL Tree:

- Balance Factor: Always in [-1, 0, 1]

- Time Complexity: O(log n) for all

- Height: log n

- Rotations used to maintain balance

BST:

- Can become skewed

- Worst-case O(n)

Applications of AVL:

1. Databases for indexing


Tree & AVL Tree Practice Questions with Solutions

2. Memory management in OS

Q6. Q4 b.Construct an AVL Tree by inserting the keys:30, 20, 10, 25, 40, 50Draw tree after each inse

Ans: Insert 30: Root = 30

Insert 20: Left of 30

Insert 10: Causes LL imbalance at 30 -> perform Right Rotation

Tree: 20

/ \

10 30

Insert 25: Right of 20, left of 30 -> Balanced

Insert 40: Right of 30

Insert 50: Causes RR imbalance at 30 -> Left Rotation

Final Tree:

20

/ \

10 30

/\

25 40

50

You might also like