0% found this document useful (0 votes)
21 views118 pages

Ads 2

The document discusses different types of threaded binary trees, including single and double threaded binary trees. Single threaded trees use the right null pointer to point to the inorder successor, while double threaded trees use both the left and right null pointers to point to the inorder predecessor and successor respectively. Example code representations of the node structure are also provided.

Uploaded by

Aditya Wattamwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views118 pages

Ads 2

The document discusses different types of threaded binary trees, including single and double threaded binary trees. Single threaded trees use the right null pointer to point to the inorder successor, while double threaded trees use both the left and right null pointers to point to the inorder predecessor and successor respectively. Example code representations of the node structure are also provided.

Uploaded by

Aditya Wattamwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 118

BRACT’S, Vishwakarma Institute of Information Technology, Pune-48

(An Autonomous Institute affiliated to Savitribai Phule Pune University)


(NBA and NAAC accredited, ISO 9001:2015 certified)

Advanced Data Structures

UNIT II – ADVANCED TREES


MRS.PRANALI G.CHAVHAN
COMPUTER ENG. DEPT., VIIT, PUNE
CONTENTS

✔ Threaded binary tree


✔ In-order traversal of in-order threaded binary tree.
✔ AVL Trees
✔ Indexing and Multiway Search Trees
✔ B-Tree, B+Tree
✔ Splay Tree
✔ Red Black Tree
✔ Trie Tree

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

• A. J. perils & C. Thornton jointly proposed idea to make effective use


of these null pointers.

• 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

• Null pointers of leaf node of the binary tree is set to inorder


predecessor or inorder successor.
TBT EXAMPLE

Binary Tree and its corresponding to threaded binary tree

• 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

• To differentiate between pointer to child and pointer to inorder successor /


predecessor the node structure of the binary tree is updated

• Two extra one bit fields LBIT and RBIT are added

LBI LE DAT Data RIGH RBI


T FT A T T
• If LBIT=0 , LEFT is pointer to left child, else to inorder predecessor
• If RBIT=0, RIGHT is pointer to right child, else to inorder successsor
THREADED BINARY TREES

• In order to have no loose threads we assume a head node in


implementation of TBT.
• The tree T is left sub tree of the dummy/head node

Complete memory representation of TBT


TBT NODE

• 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

Node leftMost(Node n) void inOrder(Node n)


{ {
Node ans = n; Node cur = leftmost(n);
while (cur != null)
if (ans == null) {
return null; print(cur);
if (cur.rbit)
while (ans.left != null) cur = cur.right;
ans = ans.left; else
cur =
return ans; leftmost(cur.right);
} }
}
}
THREADED TREE MODIFICATION

• 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

Single Threaded Binary Tree Double Threaded Binary Tree


• Here only the right NULL • Here both the right as well as
pointer are made to point to the left NULL pointers are
inorder successor. made to point inorder successor
and inorder predecessor
respectively. (here the left
threads are helpful in reverse
inorder traveral of the tree )
TYPES OF TBT

// single threaded // double threaded

Class Node{ Class Node{


int data ; int data ;
Node *left ; Node *left ;
Node *right ; Node *right ;
bool rightThread ; bool leftThread ;
} bool rightThread ;
}
TYPES OF TBT

// single threaded // double threaded

Class Node{ Class Node{


int data ; int data ;
Node *left ; Node *left ;
Node *right ; Node *right ;
bool rightThread ; bool leftThread ;
} bool rightThread ;
}
CREATION OF SINGLE TBT
static Node createThreaded(Node root)
{
if (root == null) // Base cases : Tree is empty or has single node
return null;
if (root.left == null && root.right == null)
return root;
if (root.left != null) // Find predecessor if it exists
{
Node l = createThreaded(root.left); // Find predecessor of root
//(Rightmost child in left subtree)
l.right = root; // Link a thread from predecessor to root.
l.rbit = true;
}
if (root.right == null) // If current node is rightmost child
return root;
return createThreaded(root.right); // Recur for right subtree.
}
TO BE DISCUSSED

✔ Threaded binary tree


✔ In-order traversal of in-order threaded binary tree.
✔ AVL Trees
✔ Indexing and Multiway Trees
✔ Search tree
✔ B-Tree, B+Tree
✔ Splay Tree
✔ Red Black Tree
✔ Trie Tree

27
AVL TREES - INTRODUCTION

• BST all operations time is proportional to height of tree O(h)

• Height of BST = log(n) where n is number of nodes in BST

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

• AVL Trees - invented by GM Adelson - Velsky and EM Landis in 1962

28
AVL TREES - INTRODUCTION

• AVL is height balanced binary search tree in which each node is


associated with a balance factor

Balance Factor (k) = height (left(k)) - height (right(k))

• Tree is said to be balanced if balance factor of each node is in between


-1 to 1, otherwise, the tree will be unbalanced and need to be balanced.

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

• Searching and traversing do not lead to the violation in property of


AVL tree.

• However, insertion and deletion may violate AVL property hence need
ROTATION OPERATION

31
AVL TREES - ROTATIONS

• We perform rotation in AVL tree only in case if Balance Factor is


other than -1, 0, and 1.

• Suppose node A is the node whose balance Factor is other than -1, 0, 1.
There are four types of rotations:

1. L L rotation: Inserted node is in the left subtree of left subtree of A

2. R R rotation : Inserted node is in the right subtree of right subtree of A

3. L R rotation : Inserted node is in the right subtree of left subtree of A

4. R L rotation : Inserted node is in the left subtree of right subtree of A

32
RR - ROTATION

• When BST becomes unbalanced, due to a node is inserted into the


right subtree of the right subtree of A, then we perform RR rotation
• RR rotation is an anticlockwise rotation
• It is applied on the edge below a node having balance factor -2

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

• When BST becomes unbalanced, due to a node is inserted into the


right (part) subtree of the left subtree of C, then we perform LR
rotation
• LR rotation = RR rotation + LL rotation
• First RR rotation is performed on subtree and then LL 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.

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

2) As LR rotation = RR + LL rotation, hence RR (anticlockwise) on


subtree rooted at A is performed first. By doing RR rotation, node A,
3) has become the left subtree of B.

4) 3) After performing RR rotation, node C is still unbalanced, i.e.,


having balance factor 2, as inserted node A is in the left of left of C

4) Now we perform LL clockwise rotation on full tree, i.e. on node


5)
C.

5) Node C has now become the right subtree of node B, A is left


subtree of B. Balance factor of each node is now either -1, 0, or 1,
36
i.e. BST is balanced now
RL - 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)

2) As RL rotation = LL rotation + RR rotation, hence, LL


(clockwise) on subtree rooted at C is performed first. By doing RR
rotation, node C has become the right subtree of B.
3)

3) After performing LL rotation, node A is still unbalanced, i.e.


4) having balance factor -2, which is because of the right-subtree of the
right-subtree node A.
4) Now we perform RR rotation (anticlockwise rotation) on full tree,
i.e. on node A.
5)
5)Node C has now become the right subtree of node B, and node A
has become the left subtree of B. Balance factor of each node is now
either -1, 0, or 1, i.e. BST is balanced now
38
AVL TREE CONSTRUCTION

Single rotations: insert 14, 15, 16, 13, 12, 11, 10

• First insert 14 and15:

14

15

• Now insert 16.


AVL TREE ROTATIONS

Single rotations:

• Inserting 16 causes AVL violation:

14

15

16

• Need to rotate.
AVL TREE ROTATIONS

Single rotations:

• Rotation type: RR

14

15

16
AVL TREE ROTATIONS

Single rotations:

• Rotation restores AVL balance:

15

14 16
AVL TREE ROTATIONS

Single rotations:

• Now insert 13 and 12:


15

14 16

13

12

• AVL violation - need to rotate.


AVL TREE ROTATIONS

Single rotations:

• Rotation type: LL
15

14 16

13

12
AVL TREE ROTATIONS

Single rotations:

15

13 16

12 14

• Now insert 11.


AVL TREE ROTATIONS

Single rotations:
15

13 16

12 14

11

• AVL violation – need to rotate


AVL TREE ROTATIONS

Single rotations:

• Rotation type:LL

15

13 16

12 14

11
AVL TREE ROTATIONS

Single rotations:

13

12 15

16
11 14

• Now insert 10.


AVL TREE ROTATIONS

Single rotations:

13

12 15

16
11 14

10

• AVL violation – need to rotate


AVL TREE ROTATIONS

Single rotations:

• Rotation type:LL
13

12 15

16
11 14

10
AVL TREE ROTATIONS

Single rotations:
13

11 15

10 12 14 16

• AVL balance restored.


AVL TREE ROTATIONS

Double rotations: insert 1, 2, 3, 4


• First insert 1 and 2:
13

11 15

10 12 14 16
AVL TREE ROTATIONS

Double rotations:
13

11 15

16
10 12 14

AVL violation - rotate


2
AVL TREE ROTATIONS

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:

• AVL violation – rotate:


13

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:

• AVL balance restored:


13

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

• Now insert 6. • AVL balance restored.


AVL TREE ROTATIONS

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

Double rotations: • AVL balance resto


10
4 13

2 11 15
6

12 14 16
1 3 5 7

• Now insert 9 and 8.


AVL TREE ROTATIONS

Double rotations:

10
4 13

2 11 15
6

12 14 16
1 3 5 7

• AVL violation - rotate.


8
AVL TREE ROTATIONS

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

• Tree is almost perfectly balanced


AVL TREE CONSTRUCTION

Q: Construct an AVL tree having the following elements H, I, J, B, A, E,


C, F, D, G, K, L
1. Insert H, I, J

66
AVL TREE CONSTRUCTION

2. Insert B, A

67
AVL TREE CONSTRUCTION

3. Insert E

a) We first perform RR rotation on node B b) perform LL rotation on the node I


68
AVL TREE CONSTRUCTION

3. Insert E

b) perform LL rotation on the node I

69
AVL TREE CONSTRUCTION

4. Insert C, F, D

a) We first perform LL rotation on node E


The resultant tree after LL rotation is:
70
AVL TREE CONSTRUCTION

4. Insert C, F, D

b) We then perform RR rotation on node B


The resultant balanced tree after RR rotation is:
71
TO BE DISCUSSED

✔ Threaded binary tree


✔ In-order traversal of in-order threaded binary tree.
✔ AVL Trees
✔ Red Black Tree
✔ Indexing and Multiway Trees
✔ Search tree
✔ B-Tree, B+Tree
✔ Splay Tree
✔ Trie Tree

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.

• So if your application involves many frequent insertions and


deletions, then Red Black trees should be preferred.

• 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

• We try recoloring first, if recoloring doesn’t work, then we go for 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.

• Color of a NULL node is considered as BLACK.


INSERTION (CONTD..)
Let x be the newly inserted node.
1) Perform standard BST insertion and make the color of newly inserted nodes as
RED.
2) If x is root, change color of x as BLACK
3) Do following if color of x’s parent is not BLACK or x is not root.
a) If x’s uncle is RED (Grand parent must have been black )
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
INSERTION (CONTD..)
Let x be the newly inserted node.
b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent
(p) and x’s grandparent (g) (This is similar to AVL Tree)
……..i) Left Left Case (p is left child of g and x is left child of p)
……..ii) Left Right Case (p is left child of g and x is right child of p)
……..iii) Right Right Case (Mirror of case a)
……..iv) Right Left Case (Mirror of case c)
INSERTION (CONTD..)
Left Left Case (See g, p and x)
INSERTION (CONTD..)
Right Right Case (See g, p and x)
INSERTION (CONTD..)
Left Right Case (See g, p and x)
INSERTION (CONTD..)
Right Left Case (See g, p and x)
EXAMPLE OF INSERTION
EXAMPLE
Create red black tree for following data
8,18,5,15,17,25,40 ,80
INSERT 25
INSERT 40
8
EXERCISE
Insert 2, 1, 4, 5, 9, 3, 6, 7 into a Red Black Tree
SOLUTION TO EXERCISE
TO BE DISCUSSED

✔ Threaded binary tree


✔ In-order traversal of in-order threaded binary tree.
✔ AVL Trees
✔ Red Black Tree
✔ Indexing and Multiway Search Trees
✔ B-Tree, B+Tree
✔ Splay Tree
✔ Trie Tree

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

• Indexing is a table which is consist of two columns – key, block address

• Indexing is also further divided into two types


1)Dense Index
2)Sparse Index.

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

Binary Search Trees + Self Balancing Property = AVL Trees


Red Black Trees

Store more than one value


in single node
To store extremely large amount
Have more than two children of data self balancing property
per node will not be enough to prevent
huge increase in the tree height
Used for Implementing
multilevel indexing

103
B-TREES

• Store mode than one value in single node

• Have more than 2 children per node


• B-Tree of order m can have ‘m’ children and at most ‘m-1’ key values
• For each node x, the keys are stored in increasing order.
• BST property holds-
• operations on BST are applicable
• efficient on large data

104
B-TREES PROPERTIES

• For a tree to be called as valid B-Tree of order-m it should satisfy


following additional property to that of ordering

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

2. Update the allowed number of keys in the node

3. Search the appropriate node for insertion

4. If the node is full, follow the steps below


1. Insert the elements in increasing order
2. Now, there are elements greater than its limit. So, split at the median
3. Push the median key upwards and make the left keys as a left child and the right keys
as a right child

5. If the node is not full, follow the steps below


1. Insert the node in increasing order.
106
B-TREES INSERTION
VISUALIZATION
HTTPS://WWW.CS.USFCA.EDU/~GALLES/VISUALIZATION/BTREE.HTML

107
B-TREES TRAVERSAL(IN-ORDER)

1 12 23 42 55 58 60 65 67 78 80 96

108
B-TREES SEARCH OPERATION

Searching for an element in a B-tree is the generalized form of searching an


element in BST.
1. Starting from the root node, compare k with the first key of the node.
• If k = the first key of the node, return the node and the index.
2. If k < the first key of the root node, search the left child of this key
recursively.
3. If there is more than one key in the current node and k > the first key,
compare k with the next key in the node.
• If k < next key, search the left child of this key (ie. k lies in between the first and
the second keys).
• Else, search the right child of the key.
4. Repeat steps 1 to 3 until the leaf is reached.

109
B TREES

Block address of record with key

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

• 99% of database management systems use B+trees for indexing.

• 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

✔ Threaded binary tree


✔ In-order traversal of in-order threaded binary tree.
✔ AVL Trees
✔ Red Black Tree
✔ Indexing and Multiway Search Trees
✔ B-Tree, B+Tree
✔ Splay Tree
✔ Trie Tree- Self Study

113
SPLAY TREES

• Splay tree is also self-balancing BST.

• 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

3. Node has both


parent and
grandparent:
3.a) Zig-Zig and
Zag-Zag Node is left
child of parent and
parent is also left
child of grand parent
OR node is right
child of its parent and
parent is also right
child of grand parent

116
SEARCH IN SPLAY TREES

3. Node has both


parent and
grandparent:
3.b) Zig-Zag and
Zag-Zig: Node is
right child of parent
and parent is right
left of grand parent
OR node is left child
of its parent and
parent is right child
of grand parent

117
EXAMPLE

118

You might also like