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

Module-4 Trees-Search Tree

Uploaded by

alaincage442
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module-4 Trees-Search Tree

Uploaded by

alaincage442
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

Vasantdada Patil Pratishthan’s College of

Engineering & Visual Arts, Sion, Mumbai.

Data Structure (DS )


(CSC303)
S.E. A Div SEM-III(Computer Engineering)

MODULE NO. 4
TREE

TOPIC

Search Trees-AVL, rotations in AVL Tree, operations on AVL


Tree, Introduction of B Tree, B+ Tree.

11/19/2021 By Mrs. Prajakta S. Khelkar 1


Search Trees
▪ AVL Tree/Height Balanced Tree
▪ B Tree
▪ B+ Tree
▪ Splay
▪ Trie

11/19/2021 2
11/19/2021 4
11/19/2021 5
11/19/2021 6
11/19/2021 7
11/19/2021 8
11/19/2021 9
11/19/2021 10
11/19/2021 11
11/19/2021 12
11/19/2021 13
11/19/2021 14
11/19/2021 15
11/19/2021 16
11/19/2021 17
11/19/2021 23
11/19/2021 24
11/19/2021 27
11/19/2021 28
Que: Construct an AVL tree for the following data
30,31,32,23,22,28,24,29,26,27,34,36

11/19/2021 49
Operations on an AVL Tree
The following operations are performed on AVL
tree...
1.Search
2.Insertion
3.Deletion
4.Traversing

11/19/2021 51
Search Operation in AVL Tree
• 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 matched, then display "Given node is found!!!" and
terminate the function
• Step 4 - If both are not matched, 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 subtree.
• Step 6 - If search element is larger, then continue the search process in
right subtree.
• Step 7 - Repeat the same until we find the exact element or until the search
element is compared with the leaf node.
• Step 8 - If we reach to the node having the value equal to the search value,
then display "Element is found" and terminate the function.
• Step 9 - If we reach to the leaf node and if it is also not matched with the
search element, then display "Element is not found" and terminate the
function.
11/19/2021 52
Insertion Operation in AVL
Tree
• 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.
• 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.

11/19/2021 53
11/19/2021 54
Deletion Operation in AVL Tree
1. Recursive method used to delete a data element from the
tree.
2. Search the element for delete, searching operation
3. If the element to be found in AVL , then there are three
cases to be considered.
a. The node to be deleted is a leafnode
b. The node to be deleted has exactly one child
c. The node to be deleted has two child
4. But, deleting node may cause the tree to be unbalanced.
Therefore, as the tree is traversed up, the nodes are
constantly checked whether they are balanced or not.
5. If a node is out of balance, it is balanced by performing
appropriate rotations and then the process continues up to
the root.
11/19/2021 55
Traversal Operation in AVL Tree

• As similar as BST
– In-order
– Pre-order
– Post-order

11/19/2021 56
B-Trees
Considerations for disk-based storage systems.
Indexed Sequential Access Method (ISAM)
m-way search trees
B-trees

57
CSE 373 -- AU
• Different indexing techniques are used to search a
record in O(1) time.
• The index is a pair of key value and address.
• Indexing techniques are classified as
– Hashed indexing
– Tree indexing
• B-Tree
• B+ Tree
• Trie Tee

• Splay trees are self adjusting trees.

11/19/2021 58
11/19/2021 59
11/19/2021 60
Data Layout on Disk
• Track: one ring
• Sector: one pie-shaped piece.
• Block: intersection of a track and a sector.
Considerations for Disk Based
Dictionary Structures
Use a disk-based method when the dictionary is too big to fit
in RAM at once.

Minimize the expected or worst-case number of disk accesses


for the essential operations (put, get, remove).

Keep space requirements reasonable -- O(n).

Methods based on binary trees, such as AVL search trees, are


not optimal for disk-based representations. The number of
disk accesses can be greatly reduced by using m-way search
trees.
62
Indexed Sequential Access
Method (ISAM)

Store m records in each disk block.

Use an index that consists of an array with one element


for each disk block, holding a copy of the largest key that
occurs in that block.
ISAM (Continued)

1.7 5.1 21.2 26.8 ...


ISAM (Continued)
To perform a get(k) operation:

Look in the index using, say, either a sequential search or


a binary search, to determine which disk block should
hold the desired record.

Then perform one disk access to read that block, and


extract the desired record, if it exists.

65
CSE 373 -- AU
ISAM Limitations

Problems with ISAM:

What if the index itself is too large to fit entirely in RAM at


the same time?

Insertion and deletion could be very expensive if all records


after the inserted or deleted one have to shift up or down,
crossing block boundaries.

66
CSE 373 -- AU
A Solution: B-Trees
Idea 1: Use m-way search trees.
(ISAM uses a root and one level under the root.)
m-way search trees can be as high as we need.

Idea 2: Don’t require that each node always be full. Empty


space will permit insertion without rebalancing. Allowing
empty space after a deletion can also avoid rebalancing.

Idea 3: Rebalancing will sometimes be necessary: figure out


how to do it in time proportional to the height of the tree.

67
To reduce disc access, the following
points applicable
• Height is kept minimum
• All leaves are kept at the same level
• All nodes other than leaves must have at least
minimum no. of children.

11/19/2021 68
Inserting a key into a B-Tree
BST grow at their leaves, but B tree grow at the root
1. New key searched in the tree. If new key found, then
search terminates at a leaf.
2. Attempt to insert the new key into a leaf.
3. If the leaf node is not full, then the new key is added to it
and insertion is finished.
4. If the leaf is full, then it split into two nodes on the same
level, except that the median key is sent up the tree to be
inserted into the parent node.
5. If this would result in the parent becoming too big, split
the parent into two, promoting the middle key.
6. Repeated

11/19/2021 69
• Inserting element
78,21,14,11,97,85,74,63,45,42,57,20,16,19,52,30
,21

11/19/2021 70
11/19/2021 71
11/19/2021 72
B-Tree Example with m = 5

12

2 3 8 13 27

The root has been 2 and m children.


Each non-root internal node has between m/2 and m children.
All external nodes are at the same level. (External nodes are actually
represented by null pointers in implementations.)
Insert 10

12

2 3 8 10 13 27

We find the location for 10 by following a path from the root using the stored
key values to guide the search.
The search falls out the tree at the 4th child of the 1st child of the root.
The 1st child of the root has room for the new element, so we store it there.

74
CSE 373 -- AU
Insert 11

12

2 3 8 10 11 13 27

We fall out of the tree at the child to the right of key 10.
But there is no more room in the left child of the root to hold 11.
Therefore, we must split this node...

75
CSE 373 -- AU
Insert 11 (Continued)

8 12

2 3 10 11 13 27

The m + 1 children are divided evenly between the old and new nodes.
The parent gets one new child. (If the parent become overfull, then it,
too, will have to be split).
Remove 8

8 12

2 3 10 11 13 27

Removing 8 might force us to move another key up from one of the


children. It could either be the 3 from the 1st child or the 10 from the
second child.
However, neither child has more than the minimum number of children (3),
so the two nodes will have to be merged. Nothing moves up.
Remove 8 (Continued)

12

2 3 10 11 13 27

The root contains one fewer key, and has one fewer child.

78
CSE 373 -- AU
Remove 13

12

2 3 10 11 13 27

Removing 13 would cause the node containing it to become underfull.


To fix this, we try to reassign one key from a sibling that has spares.

79
CSE 373 -- AU
Remove 13 (Cont)

11

2 3 10 12 27

The 13 is replaced by the parent’s key 12.


The parent’s key 12 is replaced by the spare key 11 from the left sibling.
The sibling has one fewer element.

80
CSE 373 -- AU
Remove 11

11

2 3 10 12 27

11 is in a non-leaf, so replace it by the value immediately preceding: 10.


10 is at leaf, and this node has spares, so just delete it there.

81
CSE 373 -- AU
Remove 11 (Cont)

10

2 3 12 27

82
CSE 373 -- AU
Remove 2

10

2 3 12 27

Although 2 is at leaf level, removing it leads to an underfull node.


The node has no left sibling. It does have a right sibling, but that node is at its
minimum occupancy already.
Therefore, the node must be merged with its right sibling.

83
CSE 373 -- AU
Remove 2 (Cont)

3 10 12 27

The result is illegal, because the root does not have at least 2 children.
Therefore, we must remove the root, making its child the new root.

84
CSE 373 -- AU
Remove 2 (Cont)

3 10 12 27

The new B-tree has only one node, the root.

85
CSE 373 -- AU
Insert 49

3 10 12 27

Let’s put an element into this B-tree.

86
CSE 373 -- AU
Insert 49 (Cont)

3 10 12 27 49

Adding this key make the node overfull, so it must be split into two.
But this node was the root.
So we must construct a new root, and make these its children.

87
CSE 373 -- AU
Insert 49 (Cont)

12

3 10 27 49

The middle key (12) is moved up into the root.


The result is a B-tree with one more level.

88
CSE 373 -- AU
● 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.
● B+ Tree are used to store the large amount of data which
● The leafbenodes
can not storedofinathe
B+maintree memory.
are linked
Duetogether in the
to the fact that,
form ofmain
size of a singly linked
memory is lists
alwaysto make thethe
limited, search queries
internal nodes
more
(keysefficient.
to access records) of the B+ tree are stored in the
main memory whereas, leaf nodes are stored in the
secondary memory.

89
11/19/2021 90
The internal nodes of B+ tree are often called index nodes. A B+
tree of order 3 is shown in the following figure.

91
Advantages of B+ Tree
● Records can be fetched in equal number of disk accesses.
● Height of the tree remains balanced and less as compare to B
tree.
● We can access the data stored in a B+ tree sequentially as
well as directly.
● Keys are used for indexing.
● Faster search queries as the data is stored only on the leaf
nodes.

92
93
Insertion in B+ Tree
Step 1: Insert the new node as a leaf node

Step 2: If the leaf doesn't have required space, split the node and copy the
middle node to the next index node.

Step 3: If the index node doesn't have required space, split the node and
copy the middle element to the next index page.

Example :
Insert the value 195 into the B+ tree of order 5 shown in the following
figure.

94
195 will be inserted in the right sub-tree of 120 after 190. Insert it
at the desired position.

The node contains greater than the maximum number of elements


i.e. 4, therefore split it and place the median node up to the
parent.

95
Now, the index node contains 6 children and 5 keys which violates
the B+ tree properties, therefore we need to split it, shown as
follows.

96
Deletion in B+ Tree
Step 1: Delete the key and data from the leaves.

Step 2: if the leaf node contains less than minimum number of


elements, merge down the node with its sibling and delete the key in
between them.

Step 3: if the index node contains less than minimum number of


elements, merge the node with the sibling and move down the key in
between them.

Example
Delete the key 200 from the B+ Tree shown in the following figure.
97
200 is present in the right sub-tree of 190, after 195. delete it

98
Merge the two nodes by using 195, 190, 154 and 129.

Now, element 120 is the single element present in the node which is
violating the B+ Tree properties. Therefore, we need to merge it by
using 60, 78, 108 and 120.

Now, the height of B+ tree will be decreased by 1.

99
100
Splay Tree/ Self adjusting Tree
• It is invented by Daniel sleater.
• Splay Tree is a self - adjusted Binary Search Tree in
which every operation on element rearranges the tree so
that the element is placed at the root position of the tree.
• Its form of BST
• Its not like AVL tree, splay tree do not have to find out
Balanced Factor of every node.
• This save space and simplifies algorithm to a great
extent.
• It has two technique a) Bottom up b) Top Down
• Example in hospital maintain patient records.

11/19/2021 101
Each splay tree depends on three factors
1. Whether N is the left or right child of its
parent P.
2. Whether P is the root or not & if not
3. Whether P is left or right child of its
parent, G(N’s grandparent)

Rotations in Splay Tree


•1. Zig Rotation
•2. Zag Rotation
•3. Zig - Zig Rotation
•4. Zag - Zag Rotation
•5. Zig - Zag Rotation
•6. Zag - Zig Rotation

11/19/2021 102
The Zig Rotation in splay tree is similar to the single right rotation in
AVL Tree rotations. In zig rotation, every node moves one position to
the right from its current position. Consider the following example...

11/19/2021 103
Zig-Zig Rotation
The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation,
every node moves two positions to the right from its current position. Consider
the following example..

Zag-Zag Rotation
The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-
zag rotation, every node moves two positions to the left from its
current position. Consider the following example...

11/19/2021 104
Zig-Zag Rotation
The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed by zag
rotation. In zig-zag rotation, every node moves one position to the right followed
by one position to the left from its current position. Consider the following
example...

11/19/2021 105
Zag-Zig Rotation
The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig
rotation. In zag-zig rotation, every node moves one position to the left followed
by one position to the right from its current position. Consider the following
example...

11/19/2021 106
Trie (Keyword Tree)
Strings can essentially be viewed as the most important and
common topics for a variety of programming problems.
String processing has a variety of real world applications
too, such as:
•Search Engines
•Genome Analysis
•Data Analytics

Tries:
Tries are an extremely special and useful data-structure that
are based on the prefix of a string. They are used to represent
the “Retrieval” of data and thus the name Trie.

11/19/2021 107
Prefix : What is prefix:
The prefix of a string is nothing but any n letters n≤|S| that
can be considered beginning strictly from the starting of a
string. For example , the word “abacaba” has the following
prefixes:
a
ab
aba
abac
abaca
abacab

11/19/2021 108
A Trie is a special data structure used to store strings that can be visualized
like a graph. It consists of nodes and edges. Each node consists of at max 26
children and edges connect each parent node to its children. These 26
pointers are nothing but pointers for each of the 26 letters of the English
alphabet A separate edge is maintained for every edge.
Strings are stored in a top to bottom manner on the basis of their prefix in a
trie. All prefixes of length 1 are stored at until level 1, all prefixes of length
2 are sorted at until level 2 and so on.

11/19/2021 109
11/19/2021 110

You might also like