Ads 2
Ads 2
2
THREADED BINARY TREES
• Binary trees have a lot of wasted space: the leaf nodes each have 2 null
pointers
• We can generalize it that for any binary tree with n nodes there will be
(n+1) null pointers and 2n total pointers
• We can use these pointers to help us in traversals??
THREADED BINARY TREES
• Main Idea: To use null pointers to make the inorder and preorder
traversal of the tree faster without using any additional data
structure(e.g auxilary stack) or memory to do the traversal
• The tree has 9 nodes and 10 null links which have been replaced by threads.
• If we traverse the tree in in-order the nodes will be visited in the order H D I B
EAFCG
• For Example, node E has predecessor thread which points to node B and
successor thread which points to node A
THREADED BINARY TREES
• Two extra one bit fields LBIT and RBIT are added
• Example code:
class Node {
type data
Node left, right;
boolean lbit, rbit;
}
THREADED TREE EXAMPLE
6
3 8
1
1 5 7
1 1
9
3
TBT IN-ORDER TRAVERSAL
• We start at the leftmost node in the tree, print it, and follow its right
thread
• If we follow a thread to the right, we output the node and continue to its
right
• If we follow a link to the right, we go to the leftmost node, print it, and
continue
THREADED TREE TRAVERSAL
Output
6 1
3 8
1
1 5 7
1 1
9
3
Start at leftmost node, print it
THREADED TREE TRAVERSAL
Output
6 1
3
3 8
1
1 5 7
1 1
9
3
Follow thread to right, print node
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8
1
1 5 7
1 1
9
3
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8 6
1
1 5 7
1 1
9
3
Follow thread to right, print node
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8 6
1 7
1 5 7
1 1
9
3
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1
9
3
Follow thread to right, print node
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1 9
9
3
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1 9
11
9
3
Follow thread to right, print node
THREADED TREE TRAVERSAL
Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1 9
11
9 13
3
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL CODE
• We’re still wasting pointers, since half of our leafs’ pointers are still
null
• We can add threads to the previous node in an inorder traversal as
well, which we can use to traverse the tree backwards or even to do
postorder traversals
THREADED TREE MODIFICATION
6
3 8
1
1 5 7
1 1
9
3
TYPES OF TBT
27
AVL TREES - INTRODUCTION
• Operations can be extended to O(n) time if the BST becomes skewed (i.e.
worst case).
• By limiting this height to log n, AVL tree imposes an upper bound on each
operation to be O(log n) where n is the number of nodes.
28
AVL TREES - INTRODUCTION
29
AVL TREE EXAMPLE
30
AVL TREES - OPERATIONS
• AVL tree is also a BST therefore, all the operations are performed in
the same way as BST
• However, insertion and deletion may violate AVL property hence need
ROTATION OPERATION
31
AVL TREES - ROTATIONS
• Suppose node A is the node whose balance Factor is other than -1, 0, 1.
There are four types of rotations:
32
RR - ROTATION
33
LL - ROTATION
• When BST becomes unbalanced, due to a node is inserted into the left
subtree of the left subtree of C, then we perform LL rotation
• LL rotation is clockwise rotation
• It is applied on the edge below a node having balance factor 2.
34
LR - ROTATION
35
LR - ROTATION
1) 1) Node B has been inserted into the right subtree of A the left
subtree of C, because of which C has become an unbalanced node
2) having balance factor 2. This case is L R rotation
• When BST becomes unbalanced, due to a node is inserted into the left
(part) subtree of the right subtree of C, then we perform RL rotation
• RL rotation = LL rotation + RR rotation
• First LL rotation is performed on subtree and then RR rotation is
performed on full tree, by full tree we mean the first node from the
path of inserted node whose balance factor is other than -1, 0, or 1.
37
RL - ROTATION
1) node B has been inserted into the left subtree of C the right
1) subtree of A, because of which A has become an unbalanced node
having balance factor - 2. This case is RL rotation
2)
14
15
Single rotations:
14
15
16
• Need to rotate.
AVL TREE ROTATIONS
Single rotations:
• Rotation type: RR
14
15
16
AVL TREE ROTATIONS
Single rotations:
15
14 16
AVL TREE ROTATIONS
Single rotations:
14 16
13
12
Single rotations:
• Rotation type: LL
15
14 16
13
12
AVL TREE ROTATIONS
Single rotations:
15
13 16
12 14
Single rotations:
15
13 16
12 14
11
Single rotations:
• Rotation type:LL
15
13 16
12 14
11
AVL TREE ROTATIONS
Single rotations:
13
12 15
16
11 14
Single rotations:
13
12 15
16
11 14
10
Single rotations:
• Rotation type:LL
13
12 15
16
11 14
10
AVL TREE ROTATIONS
Single rotations:
13
11 15
10 12 14 16
11 15
10 12 14 16
AVL TREE ROTATIONS
Double rotations:
13
11 15
16
10 12 14
Double rotations:
13
11 15
16
10 12 14
2 • Rotation type:LR
AVL TREE ROTATIONS
Double rotations:
• AVL balance restored:
13
11 15
2 12 14 16
1 10
• Now insert 3.
AVL TREE ROTATIONS
Double rotations:
11 15
2 12 14 16
1 10
3
AVL TREE ROTATIONS
Double rotations:
13
11 15
2 12 14 16
1 10
• Rotation type: LR
3
AVL TREE ROTATIONS
Double rotations:
10 15
2 11 14 16
1 3 12
AVL TREE ROTATIONS
Double rotations:
10
4 13
2 11 15
5
7 12 14 16
1 3
Double rotations:
10
4 13
2 11 15
5
7 12 14 16
1 3
6
• AVL violation - rotate.
AVL TREE ROTATIONS
Double rotations:
10
4 13
2 11 15
5
7 12 14 16
1 3
• Rotation type: RL
AVL TREE ROTATIONS
2 11 15
6
12 14 16
1 3 5 7
Double rotations:
10
4 13
2 11 15
6
12 14 16
1 3 5 7
Double rotations:
10
4 13
2 11 15
6
12 14 16
1 3 5 7
9
• Rotation type: RL
8
AVL TREE ROTATIONS
Final tree:
10
4 13
2 11 15
6
12 14 16
1 3 5 8
7 9
66
AVL TREE CONSTRUCTION
2. Insert B, A
67
AVL TREE CONSTRUCTION
3. Insert E
3. Insert E
69
AVL TREE CONSTRUCTION
4. Insert C, F, D
4. Insert C, F, D
72
WHAT IS RED BLACK TREE
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node
follows following rules:
1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot have a red parent or
red child).
4) Every path from root to a NULL node has same number of black nodes.
EXAMPLES
WHY RED BLACK TREE
• The AVL trees are more balanced compared to Red Black Trees,
but they may cause more rotations during insertion and deletion.
• And if the insertions and deletions are less frequent and search is
more frequent operation, then AVL tree should be preferred over
Red Black Tree.
•
RED BLACK TREE INSERTION
• In AVL tree insertion, we used rotation as a tool to do balancing after insertion
caused imbalance. In Red-Black tree, we use two tools to do balancing.
1)Recoloring 2) Rotation
• The algorithms has mainly two cases depending upon the color of uncle. If
uncle is red, we do recoloring. If uncle is black, we do rotations and/or
recoloring.
98
INDEXING
• Data is read from the disk block wise. Searching a record from database may
require several block access which will be slow. So we need appropriate data
structure to reduce the number of block accesses. This can be done with help of
indexing
99
DENSE INDEX
In a dense index, a record is created for every search key valued in the database.
This helps you to search faster but needs more space to store index records. In
this Indexing, method records contain search key value and address of block
containing the real record on the disk.
100
SPARSE INDEX
It is an index record that appears for only some of the values in the file. Sparse
Index helps you to resolve the issues of dense Indexing in DBMS.
101
MULTI LEVEL INDEXING
Multilevel Indexing in Database is created when a primary index does not fit in
memory.
102
B TREES- INTRODUCTION
103
B-TREES
104
B-TREES PROPERTIES
1. Every internal node contains at most m children and m - 1 keys along with a
pointer to each child and address of record.
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 children and 1 key
4. All leaf nodes must be at the same level
105
B-TREES INSERT OPERATION
1. If the tree is empty, allocate a root node and insert the key
107
B-TREES TRAVERSAL(IN-ORDER)
1 12 23 42 55 58 60 65 67 78 80 96
108
B-TREES SEARCH OPERATION
109
B TREES
Tree Pointer
Leaf Pointer
110
B+ TREES
• The data pointers are present only at the leaf nodes on a B+ tree whereas the
data pointers are present in the internal, leaf or root nodes on a B-tree.
• The leaves are not connected with each other on a B-tree whereas they are
connected on a B+ tree.
111
B+ TREES (CONTD..)
• This is because the B+tree holds no block adresss in the internal nodes. This
maximizes the number of keys stored in a node thereby minimizing the
number of levels needed in a tree. Smaller tree depth invariably means faster
search.
• Leaf nodes in a B+tree are linked together making range search operations
efficient and quick
112
TO BE DISCUSSED
113
SPLAY TREES
• The main idea of splay tree is to bring the recently accessed item to root of the
tree, this makes the recently searched item to be accessible in O(1) time if
accessed again.
• The idea is to use locality of reference (In a typical application, 80% of the
access are to 20% of the items). Imagine a situation where we have millions or
billions of keys and only few of them are accessed frequently, which is very
likely in many practical applications.
• All splay tree operations run in O(log n) time on average, where n is the
number of entries in the tree
114
SEARCH IN SPLAY TREES
• The search operation in Splay tree does the standard BST search
• In addition to search, it also splays (move a node to the root).
• If the search is successful, then the node that is found is splayed and becomes the new root.
• Else the last node accessed prior to reaching the NULL is splayed and becomes the new root.
• There are following cases for the node being accessed.
1. Node is root: We simply return the root, don’t do anything else as the
accessed node is already root.
2. Zig: Node is child of root (the node has no grandparent)
115
SEARCH IN SPLAY TREES
116
SEARCH IN SPLAY TREES
117
EXAMPLE
118