DS - Unit 3 - Notes
DS - Unit 3 - Notes
DS - Unit 3 - Notes
of AI
DATA STRUCTURES
In binary search tree all the nodes in left sub-tree of any node contains smaller
values and all the nodes in right sub-tree of that contains larger values as shown in
following figure:
In the above tree, left sub-tree of every node contains nodes with smaller values
and right sub-tree of every node contains larger values.
DrAA Unit-3 1
II B.Tech – I Sem Data Structures Dept. of AI
DrAA Unit-3 2
II B.Tech – I Sem Data Structures Dept. of AI
DrAA Unit-3 3
II B.Tech – I Sem Data Structures Dept. of AI
The above tree is a binary search tree and every node is satisfying balance
factor condition. So this tree is said to be an AVL tree.
Note: Every AVL Tree is a binary search tree but every Binary Search Tree need not be
AVL tree.
DrAA Unit-3 4
II B.Tech – I Sem Data Structures Dept. of AI
Single Left Rotation (LL Rotation): In LL Rotation, every node moves one position
to left from the current position. To understand LL Rotation, let us consider the
following insertion operation in AVL Tree...
Single Right Rotation (RR Rotation): In RR Rotation, every node moves one
position to right from the current position. To understand RR Rotation, let us
consider the following insertion operation in AVL Tree...
DrAA Unit-3 5
II B.Tech – I Sem Data Structures Dept. of AI
Left Right Rotation (LR Rotation): The LR Rotation is a sequence of single left
rotation followed by a single right rotation. In LR Rotation, at first, every node
moves one position to the left and one position to right from the current position.
To understand LR Rotation, let us consider the following insertion operation in
AVL Tree...
Right Left Rotation (RL Rotation): The RL Rotation is sequence of single right
rotation followed by single left rotation. In RL Rotation, at first every node moves
one position to right and one position to left from the current position. To
understand RL Rotation, let us consider the following insertion operation in AVL
Tree...
DrAA Unit-3 6
II B.Tech – I Sem Data Structures Dept. of AI
Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said
to be imbalanced. In this case, perform suitable Rotation to make it balanced
and go for next operation.
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
DrAA Unit-3 7
II B.Tech – I Sem Data Structures Dept. of AI
3.6 B Tree:
B Tree is a self-balancing tree data structure that maintains sorted data and
allows sequential access, insertions, deletions, and searches in logarithmic time.
The B-tree generalizes the binary search tree, allowing for nodes with more than
two children. A B-Tree of order m can have at most m-1 keys and m children. One
of the main reasons of using B tree is its capability to store large number of keys in
a single node and large key values by keeping the height of the tree relatively
small.
B- Tree Properties:
1. Every node in a B-Tree contains at most m children.
2. Every node in a B-Tree except the root node and the leaf node contain at
least m/2 children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.
It is not necessary that, all the nodes contain the same number of children but, each
node must have m/2 number of nodes. For example a B tree of order 4 is shown in
the following image.
@ B Tree
DrAA Unit-3 8
II B.Tech – I Sem Data Structures Dept. of AI
Advantages of B- Tree:
1. Keeps keys in sorted order for sequential traversing.
2. Uses a hierarchical index to minimize the number of disk reads.
3. Uses partially full blocks to speed up insertions and deletions.
4. Keeps the index balanced with a recursive algorithm.
3.7 B+ Tree
B+ Tree is an extension of B Tree which allows efficient insertion, deletion
and search operations. In B Tree, Keys and records both can be stored in the
internal as well as leaf nodes. Whereas, in B+ tree, records (data) can only be
stored on the leaf nodes while internal nodes can only store the key values. The
leaf nodes of a B+ tree are linked together in the form of a singly linked list to
make the search queries more efficient.
B+ Tree are used to store the large amount of data which cannot be stored in
the main memory. Due to the fact that, size of main memory is always limited, the
internal nodes (keys to access records) of the B+ tree are stored in the main
memory whereas, leaf nodes are stored in the secondary memory.
The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is
shown in the following figure.
@ B+ Tree
Advantages of B+ Tree
1. Records can be fetched in equal number of disk accesses.
2. Height of the tree remains balanced and less as compare to B tree.
3. We can access the data stored in a B+ tree sequentially as well as directly.
4. Keys are used for indexing.
5. Faster search queries as the data is stored only on the leaf nodes.
DrAA Unit-3 9
II B.Tech – I Sem Data Structures Dept. of AI
B Tree Vs B+ Tree
SN B Tree B+ Tree
1 Search keys cannot be repeatedly stored. Redundant search keys can be present.
2 Data can be stored in leaf nodes as well Data can only be stored on the leaf
as internal nodes nodes.
5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make
the search operations more efficient.
Optional
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
B tree and B+ tree are widely used in file system for indexing. Unlike other
self-balancing binary search trees, the B-tree is well suited for storage systems that
read and write relatively large blocks of data, such as disks. Disk is divided into
many tracks and sectors.
We read and write in terms of “blocks” and any block on the disk can be
addressed by tracker number and sector number. When we reach the block then we
can visit a particular byte on disk with the offset. [tracker number, block number,
offset]. Data can’t read and write directly from disk, we will put them into main
memory (RAM), after the operation then put back. The below picture shows how
data is stored and manage on the disk specifically.
DrAA Unit-3 10
II B.Tech – I Sem Data Structures Dept. of AI
128 Bytes
The no of blocks for each record = 512 / 128 = 4 Blocks
The no. of blocks for 100 Records = 100 / 4 = 25 Blocks
Using Index
Block size = 512 Bytes
Record Size = EmpID ---- 10 bytes
Pointer----- 6 bytes
____________
16 Bytes
The no of blocks for each record = 512 / 16 = 32 Blocks
The no. of blocks for 100 Records = 100 / 32 = 4 Blocks
The indexes of tracker and sector are also stored in blocks.
How many blocks we need for accessing the records in disk?
We need 4 blocks to store the index and 1 block to assess the data. [The example above]
Ans: (4 + 1) blocks are required to accessing every data of the database. [not 25 blocks]
How many blocks we need to storage those records?
Ans: 25 blocks for records and 4 blocks for Index.
When the records extremely large. How we handle this situation. For example, we have
1000 records. [250 blocks], and we need 40 blocks for index. The problem is that the
index itself is large, so we need multi-level indexing to reduce the access time.
So how many blocks do we need after we design a new index for indexing?
Second level index: 2 blocks.
First level index: 40 blocks.
Total: 2 + 1 + 1 = 4 blocks.
Adding one more index will reduce the access of blocks.
++++++++++++++++++++++++++++++++++++++++++++++++++
DrAA Unit-3 11
II B.Tech – I Sem Data Structures Dept. of AI
Note: Every Red Black Tree is a binary search tree but every Binary Search Tree need not be
Red Black tree.
DrAA Unit-3 12
II B.Tech – I Sem Data Structures Dept. of AI
M-way search trees give the same advantages to m-way trees that binary
search trees gave to binary trees - they provide fast information retrieval and
update. However, they also have the same problems that binary search trees had -
they can become unbalanced, which means that the construction of the tree
becomes of vital importance.
DrAA Unit-3 13