DS - Unit 3 - Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

II B.Tech – I Sem Data Structures Dept.

of AI

DATA STRUCTURES

Unit 3: Advanced Trees

3.1 Binary Search Tree


In computer science, binary search tree is a rooted binary tree data structure
in which every node contains only smaller values in its left sub-tree and only larger
values in its right sub-tree. The binary search tree is an advanced algorithm used
for analyzing the node, its left and right branches, which are modeled in a tree
structure and it enables faster lookups, insertions, and removals of nodes. This
makes the program really fast and accurate. Binary Search Tree (BST) is
commonly utilized to implement complex searches, robust game logics, auto-
complete activities, and graphics.

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

3.2 Binary Search Tree Operations


 Insertion
 Deletion
 Searching
Insertion Operation in BST:
In a binary search tree, the insertion operation is performed with O(log n) time
complexity. In binary search tree, new node is always inserted as a leaf node. The
insertion operation is performed as follows...
Step 1: Create a newNode with given value and set its left and right to NULL.
Step 2: Check whether tree is Empty.
Step 3: If the tree is Empty, then set set root to newNode.
Step 4: If the tree is Not Empty, then check whether value of newNode is
smaller or larger than the node (here it is root node).
Step 5: If newNode is smaller than or equal to the node, then move to its left
child. If newNode is larger than the node, then move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (e.i., reach to NULL).
Step 7: After reaching a leaf node, then insert the newNode as left child if
newNode is smaller or equal to that leaf else insert it as right child.
Example: Construct a Binary Search Tree by inserting the following sequence of
numbers...10, 12, 5, 4, 20, 8, 7, 15 and 13
Above elements are inserted into a Binary Search Tree as follows...

DrAA Unit-3 2
II B.Tech – I Sem Data Structures Dept. of AI

Deletion Operation in BST:


In a binary search tree, the deletion operation is performed with O(log n)
time complexity. Deleting a node from Binary search tree has following three
cases...
Case 1: Deleting a Leaf node (A node with no children)
Case 2: Deleting a node with one child
Case 3: Deleting a node with two children
Example: To delete a number(or Key) 20 from a Binary Search Tree with the
following sequence of numbers 15, 10, 20, 8, 12, 18, 30, 16 and 19. The deletion
process is as follows...

Search Operation in BST:


In a binary search tree, the search operation is performed with O(log n)
time complexity. The search operation is performed as follows...
Step 1: Read the search element from the user
Step 2: Compare, the search element with the value of root node in the tree.
Step 3: If both are matching, then display "Given node found!!!" and
terminate the function
Step 4: If both are not matching, then check whether search element is
smaller or larger than that node value.
Step 5: If search element is smaller, then continue the search process in
left sub-tree.
Step 6: If search element is larger, then continue the search process in
right sub-tree.
Step 7: Repeat the same until we found exact element or we completed
with a leaf node
Step 8: If we reach to the node with search value, then display "Element
is found" and terminate the function.
Step 9: If we reach to a leaf node and it is also not matching, then display
"Element not found" and terminate the function.

DrAA Unit-3 3
II B.Tech – I Sem Data Structures Dept. of AI

Example: To search a number(or Key) 16 in a Binary Search Tree with the


following sequence of numbers 15, 10, 20, 8, 12, 18, 25, 16, 19 and 30. The Search
process is as follows...

3.3 AVL Tree


AVL tree is a height-balanced binary search tree. That means, an AVL tree
is also a binary search tree but it is a balanced tree. A binary tree is said to be
balanced if, the difference between the heights of left and right subtrees of every
node in the tree is either -1, 0 or +1. In other words, a binary tree is said to be
balanced if the height of left and right children of every node differ by either -1, 0
or +1.
In an AVL tree, every node maintains an extra information known as
balance factor. The balance factor of a node is calculated either height of left
subtree - height of right subtree (OR) height of right subtree - height of left subtree.
In the following explanation, we calculate as follows...
Balance factor = heightOfLeftSubtree - heightOfRightSubtree
Example:

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

3.4 AVL Tree Rotations


In AVL tree, after performing operations like insertion and deletion we need
to check the balance factor of every node in the tree. If every node satisfies the
balance factor condition then we conclude the operation otherwise we must make it
balanced. Whenever the tree becomes imbalanced due to any operation we use
rotation operations to make the tree balanced. There are four rotations and they are
classified into two types.

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...

AVL Tree LL Rotation

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...

AVL Tree RR Rotation

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...

AVL Tree LR Rotation

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...

AVL Tree RL Rotation

3.5 AVL Tree Operations


The following operations are performed on AVL tree:
• Insertion
• Deletion
• Search
Insertion Operation in AVL Tree
In an AVL tree, the insertion operation is performed with O(log n) time
complexity. In AVL Tree, a new node is always inserted as a leaf node. The insertion
operation is performed as follows...
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
Step 2 - After insertion, check the Balance Factor of every node.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.

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

Deletion Operation in AVL Tree


The deletion operation in AVL Tree is similar to deletion operation in BST.
But after every deletion operation, we need to check with the Balance Factor
condition. If the tree is balanced after deletion go for next operation otherwise
perform suitable rotation to make the tree Balanced.

Search Operation in AVL Tree


In an AVL tree, the search operation is performed with O(log n) time
complexity. The search operation in the AVL tree is similar to the search operation
in a Binary search tree.

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.

3 Searching for some data is a slower Searching is comparatively faster as data


process since data can be found on can only be found on the leaf nodes.
internal nodes as well as on the leaf
nodes.

4 Deletions of internal nodes are so Deletion will never be a complexed


complicated and time consuming. process since element will always be
deleted from the leaf 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

Block size = 512 Bytes


Record Size = EmpID ---- 10 bytes
Name ------ 40 bytes
Department -- 20 bytes
Salary -------- 8 bytes
Address ------ 50 bytes
____________

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

3.8 Red-Black Tree


Red - Black Tree is another variant of Binary Search Tree in which every
node is colored either RED or BLACK. In Red Black Tree, the color of a node is
decided based on the properties of Red-Black Tree. Every Red Black Tree has the
following properties.
Property #1: Red - Black Tree must be a Binary Search Tree.
Property #2: The ROOT node must be colored BLACK.
Property #3: The children of Red colored node must be colored BLACK. (There
should not be two consecutive RED nodes).
Property #4: In all the paths of the tree, there should be same number of BLACK
colored nodes.
Property #5: Every new node must be inserted with RED color.
Property #6: Every leaf (e.i. NULL node) must be colored BLACK.

Example: Following is a Red-Black Tree which is created by inserting numbers


from 1 to 9.

Note: Every Red Black Tree is a binary search tree but every Binary Search Tree need not be
Red Black tree.

3.9 M-Way Tree:


A multiway tree is a tree that can have more than two children. A multiway
tree of order m (or an m-way tree) is one in which a tree can have m children. To
make the processing of m-way trees easier some type of order will be imposed on
the keys within each node, resulting in a multiway search tree of order m ( or
an m-way search tree). By definition an m-way search tree is a m-way tree in
which following conditions should be satisfied:
 Each node has m children and m-1 key fields
 The keys in each node are in ascending order.
 The keys in the first i children are smaller than the ith key
 The keys in the last m-i children are larger than the ith key

DrAA Unit-3 12
II B.Tech – I Sem Data Structures Dept. of AI

For example a multiway tree of order 5 is shown below:

Example : 4-way search tree

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

You might also like