0% found this document useful (0 votes)
42 views10 pages

UNIT 3 Some Questions Ans

Uploaded by

Prafull Achari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views10 pages

UNIT 3 Some Questions Ans

Uploaded by

Prafull Achari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT 3 Some Questions

Q) AVL Tree
i) An AVL tree is a type of binary search tree (BST) that automatically
keeps itself balanced to ensure operations like searching, inserting,
and deleting are fast.
ii) It does this by maintaining a property called the balance factor,
which is the height difference between the left and right subtrees of
any node. This balance factor must always be -1, 0, or 1.
iii) When an operation (like inserting or deleting a node) makes the
tree unbalanced, the AVL tree restores balance using rotations.
Balance Factor
Balance factor of a node in an AVL tree is the difference between the
height of the left subtree and that of the right subtree of that node.
Balance Factor = (Height of Left Subtree - Height of Right Subtree)
or (Height of Right Subtree - Height of Left Subtree)
The self balancing property of an avl tree is maintained by the balance
factor. The value of balance factor should always be -1, 0 or +1.
An example of a balanced avl tree is:

Rotating the subtrees in an AVL Tree:


An AVL tree may rotate in one of the following four ways to keep
itself balanced:
Left Rotation:
When a node is added into the right subtree of the right subtree, if the
tree gets out of balance, we do a single left rotation.

Left-Rotation in AVL tree


Right Rotation:
If a node is added to the left subtree of the left subtree, the AVL tree
may get out of balance, we do a single right rotation.

Right-Rotation in AVL Tree


Left-Right Rotation:
A left-right rotation is a combination in which first left rotation takes
place after that right rotation executes.
Left-Right Rotation in AVL tree
Right-Left Rotation:
A right-left rotation is a combination in which first right rotation takes
place after that left rotation executes.

Right-Left Rotation in AVL tree

Advantages of AVL Tree:


1. Balanced Structure: Ensures the height is always O(log
n)O(\log n), making operations fast.
2. Efficient Searching: Performs search, insertion, and deletion in
O(log n)O(\log n) time.
3. Sorted Data: Always keeps data in sorted order, ideal for range
queries.
4. Self-adjusting: Automatically balances itself after any
operation.
Disadvantages of AVL Tree:
1. Complex Rotations: Insertion and deletion require extra time
for rotations to maintain balance.
2. More Memory: Each node stores additional information (height
or balance factor), increasing overhead.
3. Not Ideal for Frequent Insertions/Deletions: Rotations can
slow down performance when there are many changes.

Applications of AVL Tree:


1. Databases: To maintain sorted records and perform efficient
range queries.
2. Memory Management: Used in dynamic memory allocation
systems.
3. File Systems: Helps manage hierarchical structures like
directories.
4. Network Routing: Efficient lookups in routing tables.
5. Gaming: For storing and searching game leaderboards.

Q) Threaded Binary Tree

A Threaded Binary Tree is a special type of binary tree that helps in


making the process of traversing the tree faster and easier. In a normal
binary tree, many pointers (especially in leaf nodes) are null because
they don’t have children. These null pointers are wasted space.
In a Threaded Binary Tree, these null pointers are used to create
threads, which are special links to help with in-order traversal
(visiting the nodes in left-root-right order).
How It Works:
1. If a node doesn’t have a left child, its left pointer (which would
be null in a normal tree) is used to point to its in-order
predecessor (the node that comes just before it in in-order
traversal).
2. If a node doesn’t have a right child, its right pointer (which
would also be null in a normal tree) is used to point to its in-
order successor (the node that comes just after it in in-order
traversal).
These threads act as shortcuts that make traversal easier and remove
the need for using recursion or a stack.

Types of Threaded Binary Trees:


1. Single Threaded Tree:
Only one of the null pointers (left or right) is replaced by a
thread:
o Left pointer stores the in-order predecessor, OR
o Right pointer stores the in-order successor.
2. Double Threaded Tree:
Both left and right null pointers are replaced with threads:
o Left pointer stores the in-order predecessor.
o Right pointer stores the in-order successor.
Benefits of a Threaded Binary Tree:
1. Faster Traversal: Traversing the tree in in-order is faster
because threads provide direct links to the next node.
2. No Recursion/Stack Needed: Simplifies traversal by
eliminating the need for recursion or an explicit stack.
3. Efficient Memory Usage: Utilizes null pointers, reducing
wasted space.
Drawbacks:
1. Complicated Insertions and Deletions: Modifying the tree
(like adding or removing nodes) requires careful updates to the
threads.
2. Limited Use: It’s mainly useful for scenarios where frequent in-
order traversal is needed.
Where It’s Used:
1. In-Order Traversals: For efficiently visiting nodes in sorted
order.
2. Expression Trees: To evaluate or simplify mathematical
expressions.
3. Database Indexing: For maintaining sorted data structures.

Q) B-tree
i)B-Trees, also known as B-Tree or Balanced Tree,
ii) A B-tree is a balanced tree data structure that is commonly used in
databases and file systems to store large amounts of data efficiently.
iii) It is a self-balancing tree that maintains sorted data and allows for
fast insertion, deletion, and search operations.
iv)A B-tree is a special type of tree used to store and manage large
amounts of data efficiently.
v)Unlike binary trees (where each node has at most two children), a
B-tree allows each node to have multiple children, making it very
efficient for storing large data sets that don't fit entirely in memory.
A B-tree works as follows:
1. Balanced Structure:
The tree stays balanced, meaning all leaf nodes are at the same
level, making operations like searching, inserting, and deleting
efficient and consistent.
2. Nodes with Multiple Keys:
Each node can hold multiple keys, allowing more data to be
stored in fewer levels, which reduces the height of the tree.
3. Children in a Node:
Each node points to multiple child nodes. The number of
children depends on the tree's order. For example, in a B-tree of
order 3, a node can have 2 keys and 3 children.
4. Efficient Search:
Searching in a B-tree is faster because it can check multiple
child nodes at once, unlike binary trees that check one child at a
time.
5. Balanced Insertion and Deletion:
When inserting or deleting, the tree maintains balance. If a node
becomes full, it splits. If it has too few keys, it borrows from
neighbors or merges.

Properties of a B-tree:

1. Order of the Tree (m):


o The order (m) defines how many keys and children a node
can have.
▪ For order 3: Each node can have 2 keys and 3
children.
▪ For order 4: Each node can have 3 keys and 4
children.
2. Node Requirements:
o Each node can have at least ⌈m/2⌉ - 1 keys and at most m
- 1 keys.
o Each node can have at least ⌈m/2⌉ children and at most
m children.
3. Leaf Nodes:
o All leaf nodes are at the same level, ensuring the tree
remains balanced for efficient searching and inserting.

Advantages of a B-tree:
1. Efficient Search:
A B-tree allows quick searching, insertion, and deletion because
the tree remains balanced, and nodes can hold multiple keys,
minimizing the number of operations needed.
2. Reduces Disk Access:
B-trees are designed for systems where data is stored on disk.
Since they have multiple keys in a node, the number of disk
accesses required for search operations is minimized.
3. Dynamic Growth:
The B-tree grows and shrinks dynamically. As more keys are
added, the tree automatically rebalances itself by splitting or
merging nodes as needed.
Disadvantages of a B-tree:
1. Complexity:
Managing a B-tree, especially when performing insertions and
deletions, can be more complex compared to simpler data
structures like binary search trees.
2. Memory Usage:
B-trees need more memory because each node contains multiple
keys and child pointers. This can be an issue in memory-
constrained systems.

Applications of a B-tree:
1. Databases:
B-trees are commonly used in databases for indexing and
quickly finding records in large datasets.
2. File Systems:
File systems use B-trees to organize and quickly access files and
directories, which may contain a large number of entries.
3. Operating Systems:
B-trees are used in managing disk storage and file directories,
ensuring that data is retrieved efficiently from disk.
.
Example of a B-tree (Order 3):
In a B-tree of order 3:
• Each node can hold 2 keys (at most).
• Each node can have 3 children (at most).
[30]
/ \
[10, 20] [40, 50]
• The root node has one key (30).
• The left child has two keys (10, 20), and the right child has two
keys (40, 50).
Basic Operations:
1. Search: To search for a key in the B-tree, compare it with the keys
in each node and move to the appropriate child.
2. Insertion: If a node is full, it is split into two nodes, and the middle
key is promoted to the parent node.
3. Deletion: If a key is deleted and a node has too few keys, it
borrows a key from a sibling or merges nodes.

You might also like