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

DSA mock

The document explains various tree structures and operations, including AVL trees, Red-Black trees, Splay trees, B-trees, and Trie trees, detailing their balancing mechanisms and deletion algorithms. It also covers file handling in C++, including file opening modes and the concept of inverted files. Additionally, it discusses sequential file organization, its advantages, and disadvantages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

DSA mock

The document explains various tree structures and operations, including AVL trees, Red-Black trees, Splay trees, B-trees, and Trie trees, detailing their balancing mechanisms and deletion algorithms. It also covers file handling in C++, including file opening modes and the concept of inverted files. Additionally, it discusses sequential file organization, its advantages, and disadvantages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

--------UNIT 4 ---------

Q) Explain following terms w.r.t. height balance tree LL, RR, LR, RL

1. LL (Left-Left) Rotation

Cause: Insertion happens in the left subtree of the left child.

Solution: Perform a single right rotation.

Example:

30

20

10

Unbalanced at 30 (balance factor = 2).

LL case → Right rotate at 30.

---

2. RR (Right-Right) Rotation

Cause: Insertion happens in the right subtree of the right child.

Solution: Perform a single left rotation.

Example:

10

20

30

Unbalanced at 10 (balance factor = -2).

RR case → Left rotate at 10.

---

3. LR (Left-Right) Rotation

Cause: Insertion happens in the right subtree of the left child.

Solution: Perform a left rotation on left child, then right rotation on node.

Example:
30

10

20

Unbalanced at 30.

LR case → First left rotate 10, then right rotate 30.

---

4. RL (Right-Left) Rotation

Cause: Insertion happens in the left subtree of the right child.

Solution: Perform a right rotation on right child, then left rotation on node.

Example:

10

30

20

Unbalanced at 10.

RL case → First right rotate 30, then left rotate 10.

Q) Demonstrate Deletion Operation in AVL with example

Step-by-Step Example:

Initial AVL Tree:

30

/ \

20 40

10

1. Delete Node 40

After deleting 40:


30

20

10

Now check balance factors:

Balance of 30 = height(left subtree) - height(right subtree) = 2 - 0 = +2

This is unbalanced → LL Case

2. Apply Right Rotation on 30

After right rotation:

20

/ \

10 30

This is now a balanced AVL tree.

---

Key Steps in Deletion in AVL Tree:

1. Perform normal BST deletion.

2. Update height of nodes.

3. Check balance factor at each node from the deleted node up to root.

4. Apply rotations if unbalanced:

LL Case → Right Rotate

RR Case → Left Rotate

LR Case → Left Rotate + Right Rotate

RL Case → Right Rotate + Left Rotate

Q) Explain with example i) Red - Black Tree ii) Splay Tree

i) Red-Black Tree

A Red-Black Tree is a type of self-balancing binary search tree where each node has an extra bit for
color (either red or black). The tree is balanced by applying color rules, not strict height balancing like
AVL.

Properties of Red-Black Tree:


1. Every node is either red or black.

2. The root is always black.

3. No two red nodes can be adjacent (i.e., a red node cannot have a red parent or child).

4. Every path from a node to its descendant NULL nodes must have the same number of black nodes.

5. All leaves (NULL pointers) are black.

Example:

Insert the following keys: 10, 20, 30

Steps:

Insert 10 → root is black.

Insert 20 → red child (OK).

Insert 30 → two consecutive red nodes → violation!

Fix by Left Rotation and color flip:

Final Tree:

20(B)

/ \

10(R) 30(R)

ii) Splay Tree

A Splay Tree is a self-adjusting binary search tree. When you access (search/insert/delete) a node, it
is moved to the root using rotations. This keeps frequently accessed nodes near the top.

Operations in Splay Tree:

Zig: Node is child of root → single rotation.

Zig-Zig: Node and parent are both left or both right children → double rotation.

Zig-Zag: Node is left child of right parent or vice versa → two different rotations.

Example:

Insert: 10, 20, 30

1. Insert 10 → becomes root.

2. Insert 20 → 20 becomes root (zig rotation).

3. Insert 30 → 30 becomes root (zig-zig rotation).


Final Tree:

30

20

10

After every access, the accessed node is moved to root.

-----------------------UNIT 5 ------------------------------

Q) Write an algorithm of B tree deletion

B-Tree Deletion (Short Steps)

1. Search the key K in the tree.

2. If K is in a leaf node → delete it directly.

3. If K is in an internal node:

a. If left child has ≥ t keys → replace K with predecessor and delete it.

b. Else if right child has ≥ t keys → replace K with successor and delete it.

c. Else → merge both children with K and delete from merged node.

4. If K is not found:

Go to the correct child.

Before going, ensure the child has ≥ t keys:

Borrow from sibling or merge if needed.

5. After deletion, if the root is empty, make its child the new root.

Q) Write an algorithm to delete a node from B+tree

B+ Tree Deletion (Short Steps)

1. Search for key K in the tree.

2. Delete key K from the leaf node.

3. If leaf has at least ceil(m/2) - 1 keys → Done.


4. If not:

Borrow a key from a sibling or merge with sibling.

Update the parent node if needed.

5. If the root becomes empty, make the first child the new root.

Q) Explain with example trie tree. Give advantage and applications of trie tree

Trie Tree Explanation

A Trie (pronounced as "try") is a tree-based data structure that stores strings (or sequences of
characters). It’s often used to store a dynamic set of strings, where each node represents a character
in a string. Tries are especially useful for tasks like searching, auto-completion, and dictionary
lookups.

Example of Trie Tree:

Let’s say we want to insert the following words into a Trie: "cat", "bat", "ball", "balloon".

Trie Structure:

(Root)

/ \

c b

/ \

a a

/\ \

t l l

/ \

o o

/ \

o n

Insert "cat": Start at the root and create nodes for 'c', 'a', and 't'.

Insert "bat": Reuse the 'b' and 'a' nodes from "bat", create a 't' node.
Insert "ball": Reuse 'b' and 'a' nodes, create 'l' nodes.

Insert "balloon": Reuse 'b', 'a', 'l', and 'l' nodes, create 'o' nodes.

Advantages of Trie Tree:

1. Efficient Search Time:

2. Prefix-based Searching:

3. No Comparisons of Keys:

4. Memory Efficient for Large Datasets:

Application:

Spell Checker

Auto-complete systems

IP routing (longest prefix match)

Search engines (prefix search)

Word games like Boggle or Scrabble

Pattern matching algorithms

Text prediction in mobile keyboards

Dictionary implementation

Genome data analysis

Compression algorithms (like LZW)

--------------------------UNIT 6 ------------------------------------

Q) What is file? List different file opening modes in C++.

Explain concept of inverted files.

What is a File?
A file is a collection of data stored on a storage device (like a hard disk), used to store input/output
permanently. In C++, files are used to read and write data using file streams (ifstream, ofstream,
fstream).

File Opening Modes in C++

1. ios::in – Opens the file for reading.

2. ios::out – Opens the file for writing.

3. ios::app – Opens the file in append mode, data is added at the end.

4. ios::ate – Opens the file and moves the pointer to the end.

5. ios::trunc – Deletes the contents of the file if it already exists.

6. ios::binary – Opens the file in binary mode instead of text mode.

---

Concept of Inverted Files

An inverted file is a data structure used to index and quickly retrieve information.

It stores a mapping from content (like a word) to its locations in documents.

Commonly used in search engines and databases to improve search speed.

Q) Explain any 3 operations carried out on sequentail file and its pseudo code.

1. Insertion

Adding new records at the end of the file.

Since records are stored in a sequence, inserting in the middle requires rewriting the file.

---

2. Deletion

Deleting a record is done by marking it as deleted (logically).

The actual space is usually freed during file reorganization.

---

3. Searching

Sequential search is used where each record is checked one by one.

This is slower compared to indexed files but simple to implement.


Q) Define sequential file organization. Give it’s advantages and disadvantages.

Sequential File Organization:

Sequential file organization is a method where records are stored in a sequence, one after another, in
the order they were added or sorted by a key field (e.g., roll number, ID, etc.). Records are accessed
sequentially, starting from the beginning of the file.

Advantages:

1. Simple to understand and implement.

2. Efficient for processing large volumes of data (like reading all records).

3. Good for applications where records are processed in order (like payroll).

---

Disadvantages:

1. Slow for searching individual records, especially in large files.

2. Insertion and deletion are time-consuming (may require rewriting the file).

3. Wastes space if many records are marked for deletion and not physically removed.

You might also like