UNIT 3 Some Questions Ans
UNIT 3 Some Questions Ans
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:
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:
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.