SlideShare a Scribd company logo
Module III
Introduction to Trees and Hash Tables
Syllabus
• Trees - Binary Trees, Binary Search Trees, AVL
Trees, 2-4 trees, Hash tables, tries.
• Binary Trees and Binary Search Trees -
Operations, Representation, Traversals. Hash
tables - concept of hashing, hash function,
collision and collision resolution.
Linear Data Structures
• Arrays, linked lists, stacks and queues were
examples of linear data structures in which
elements are arranged in a linear fashion (ie,
one dimensional representation).
Non-linear Data Structures
• Tree is another very useful data structure in
which elements are appearing in a non-linear
fashion, which requires a two dimensional
representation.
Tree
 Tree is a nonlinear data structure
 The elements appear in a non linear fashion, which
require two dimensional representations.
 Using tree it is easy to organize hierarchical
representation of objects.
 Tree is efficient for maintaining and manipulating
data, where the hierarchy of relationship among the
data is to be preserved.
Tree
 Definition: A tree is a finite set of one or more nodes
such that there is a specially designated node called
the root. The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of these sets is
a tree. We call T1, ..., Tn the subtrees of the root.
A
B C
D E F G
H I J K
Tree - Terminologies
 Here there are 11 nodes and 10 edges
 In any tree with N nodes there will be N-1 edges
 Node: Every individual element in a tree is called a node
 Edge/Links: Nodes are connected by using edges
A
B C
D E F G
H I J K
Tree - Terminologies
 Root: It is the origin of the tree data structure. In
any tree, there must be only one root node.
A
B C
D E F G
H I
J
K
 Here A is the root node
 In any tree the first node is called root node
Tree - Terminologies
 Leaf Node/External Node/Terminal Node:
 The node which does not have a child is
called a leaf node.
 A node without successor is called a leaf node.
A
B C
D E F G
H I J K
Tree - Terminologies
 Internal Node/Non-terminal Node:
 An internal node is a node with atleast one child.
 Every non leaf node is called Internal node
A
B C
D E F G
H I J K
Tree - Terminologies
 Parent:
 The node which has child/children.
 A node which is predecessor of any other node.
A
B C
D E F G
H I J K
 Here A is the parent of B & C. B is the parent of D, E & F.
Tree - Terminologies
 Child: Descendant of any node is called a child
node.
 Here children of A are B & C. Children of B are D, E & F.
A
B C
D E F G
H I J K
Order of
nodes
⚫Children of a node are usually ordered from left-to-
right.
⚫These are two distinct ordered
trees.
a
b c
a
c b
⚫If a and b are siblings, and a is to the left of b, then all
the descendants of a are to the left of all descendants of
b.
⚫A tree in which the order of nodes is ignored is referred
to as unordered tree.
Tree - Terminologies
 Sibling: The nodes with the same parent are
called Sibling nodes.
A
B C
D E F G
H I J K
Tree - Terminologies
 Degree of the leaf node is
0.
B C
D G
 Degree of a Node: Total number of the children
of the given node
2
A
3 1
2
J
0
K
0
0
2
E
0
F
H
0
I
0
Tree - Terminologies
in a given tree
 Here maximum degree is for node B. Its degree is 3.
 So the degree of the given tree is 3
A
B C
G
 Degree of a Tree: It is the maximum degree of
nodes
2
3 1
2
J
0
K
0
0 D
2
E
0
F
H
0
I
0
Tree - Terminologies
 Level: In a tree each step from top to bottom is called as
a Level and the Level count starts with '0' and
incremented by one at each level
A
B C
D E F G
H I J K
Level 0
Level 1
Level 2
Level 3
Tree - Terminologies
 Depth of a node: It is the total number of edges
from root to that node.
A
B C
D E F G
H
I
 Depth of node E is
J K
Tree - Terminologies
 Depth of the tree: It is the total number of edges
from root to leaf in the longest path
A
B C
D E F G
H I J K
 Depth of the tree = 3
Tree - Terminologies
 Height of a node: It is the total number of edges
from longest leaf node in its subtree to that node.
A
B C
D E F G
H I J K
 Height of node B is 2
Tree - Terminologies
 Height of the tree: It is the total number of edges
from longest path leaf node to
root
A
B C
D E F G
H I J K
 Height of the tree = 3
Tree - Terminologies
 Path: Sequence of nodes and edges between two nodes
 Length of the path: Total number of edges in the path
A
B C
D E F G
H I J K
 Path between B and H is B-E-H. Its length is 2
 There is no path between B and G
Tree - Terminologies
 The ancestors of a node are all the
nodes along the path from the root to the node.
 The immediate ancestor of a node is the “parent” node
A
B C
D E F G
H
I

J K
Tree - Terminologies
 A descendant node of a node is any node in the
path from that node to the leaf node.
 The immediate descendant of a node is the
“child”
node. A
B C
D E F G
H I
J
K
 Descendant nodes of B are D, E, F, H & I
Tree - Terminologies
 Subtrees
A
B C
D E F G
H I J K
Tree - Terminologies
 Subtrees
A
B C
D E F G
H I J K
Tree Representations
1. List Representation
2. Left Child Right Sibling Representation
3. Representation as a degree 2 tree
List Representations
 Root comes first, followed by list of sub-trees
data link 1 link 2 ... link n
Left child Right sibling Representations
 Fixed sized nodes
 Easier to work
 Two link/pointer fields per node
A
B C
D E F G
H I J K
A
B C
D E F G
H I J K
Left_child data Right_Sibling
Representations as degree 2 tree
 Also known as Left child-Right child Tree/
Degree - Two Tree/ Binary Tree
 Simply rotate the right sibling pointers in the
Left- child Right-sibling tree clockwise by 45 degrees
Representations as degree 2 tree
A
B C
D E F G
A
B C
D E F G
H I J K
Tree
H I J
K
Left Child Right Sibling Representation
Representations as degree 2 tree
Tree - Applications
 Storing naturally hierarchical data: Trees are used to
store the data in the hierarchical structure. Example:
Directory structure of a file system
 Organize data: It is used to organize data for efficient
insertion, deletion and searching.
 Used in compression algorithms
 Routing table: The tree data structure is also used to
store the data in routing tables in the routers.
 To implement Heap data structure: It is used to
implement priority queues
Binary Trees
 A Binary Tree is a finite set of nodes that is either
 Empty or
 Consists of root node and two disjoint
binary trees, called, left subtree and right
subtree
A
B C
D E F
A
B
A
Binary Trees
 Any tree can be transformed into binary tree by left
child-right sibling representation
 A tree can never be empty but binary tree can be
 In binary tree a node can have atmost 2 children,
whereas in case of a tree, a node may have any
number of children
Different Binary Trees
 Skewed Binary Tree
 Left Skewed Binary Tree
 Right Skewed Binary Tree
 Complete Binary Tree
 Full/Strictly Binary Tree
Skewed Binary Trees
Complete Binary Trees
 A complete binary tree is a binary tree in which
every level, except possibly the last, is completely
filled, and the last level has all its nodes to the left
side
A
B C
D E F
H I
G
Full/Strictly Binary Trees
 Every node other than the leaves has two children
A
B C
D E F
H I
G
Binary Tree Representations
1. Array/Sequential/Linear Representation
2. Linked Representation
Binary Tree -Array Representation
 It is a sequential representation
 Use a 1-D array to store the nodes
 Block of memory for an array is allocated
before storing the actual tree in it.
 Once the memory is allocated, the size of
tree is restricted as permitted by the memory.
 Nodes are stored level by level, starting from zero
level
 The root node stored in first memory location
Binary Tree -Array Representation
 Determine the locations of the parent, left child,
and right child of any node i in the binary tree(1≤i
≤n)
 parent(i)
 If i !=1, then parent(i)= floor(i/2)
 if i =1, i is at root and has no parent
 left_child(i)
 left_child(i) = 2i
 No left child
If 2i ≤ n
Otherwis
e
 right_child(i)
 right_child(i) = 2i+1
 No right child
If 2i+1 ≤ n
Otherwise
Binary Tree -Array Representations
A
B C
D E F
G
Binary Tree -Array Representations
B C
D E F
1
A
2 3
4 5 7
10
G
A
B C
D E F
G H
Binary Tree -Array Representations
A
B
C
D
E
F
G
B C
D E F
1
2
1
A
2 3
4 5 7
G
10
3
4
5
6
7
8
9
10
Binary Tree -Array Representations
A
B C
D E G
F
Binary Tree -Array Representations
A
B
C
D
E
F
G
B C
D E F
1
2
3
4
5
6
7
1
A
2 3
4 7
F
5 6
Binary Tree -Array Representations
A
B
C
D
Binary Tree -Array Representations
A
B
C
D
A
B
C
D
1
2
3
4
5
6
7
8
1
2
4
8
Binary Tree -Array Representations
 Advantages
 Any node can be accessed from any other node by
calculating the index.
 Data are stored without any pointer
 Programming languages, where dynamic memory
allocation is not possible(like BASIC, FROTRAN),
array representation is only possible
 Good for Complete Binary tree.
 Searching is fast
Binary Tree -Array Representations
 Disadvantages
 Good for Complete Binary tree. If it is not a
complete binary tree then, a lot of memory is
wasted
 Dynamic memory allocation is not possible
Binary Tree -Linked Representations
data
Left_chil
d
Right_child
Left_child data Right_child
Binary Tree -Linked Representations
A
B C
D E F
G
A
B C
F
D E
G
NULL NULL
NULL NULL
NULL
NULL NULL NULL
Binary Tree –Linked Representations
A
B
C
D
A
B
C
D
NULL
NULL
NULL
NULL
NULL
Binary Tree -Linked Representations
 Advantages
 Dynamic memory allocation is possible
Binary Tree -Properties
 The maximum number of nodes on level i
of a binary tree is 2i, i>=0
A
B C
D E G
F
H I J K L M N O
Level
0
No. of Nodes
1
1 2
2 4
3 8
i
2i
Binary Tree -Properties
 The maximum number of nodes in a binary
tree of height h is 2h+1-1, h≥0
A
Height
0
No. of Nodes
1
Binary Tree -Properties
 The maximum number of nodes in a binary
tree of height h is 2h+1-1, h≥0
A
B C
Height
0
No. of Nodes
1
1 3
Binary Tree -Properties
 The maximum number of nodes in a binary
tree of height h is 2h+1-1, h≥0
A
B C
D E G
F
Height
0
No. of Nodes
1
1 3
2 7
Binary Tree -Properties
 The maximum number of nodes in a binary
tree of height h is 2h+1-1, h≥0
A
B C
D E G
F
H I J K L M N O
Height
0
No. of Nodes
1
1 3
2 7
3 15
Binary Tree -Properties
 The maximum number of nodes in a binary
tree of height h is 2h+1-1, h≥0
A
B C
D E G
F
H I J K L M N O
Height
0
No. of Nodes
1
1 3
2 7
3 15
h
2h+1-1
Binary Tree -Properties
 For any nonempty binary tree, T, if n0 is the
number of leaf nodes and n2 the number of nodes
of degree 2, then n0 =n2 +1
A
B C
D E G
F
J
M
 Number of nodes with degree 2 = n2 = 3
Binary Tree -Properties
 Minimum number of nodes possible in a
binary tree ofheight h is h+1
 Consider this skewed binary tree
 Here, height of the tree = 3
 Minimum number of nodes possible
A
B
C
D
Binary Tree -Properties
 For any non empty binary tree, if n is the number
of nodes and e is the number of edges then
n=e+1
 Number of nodes = n = 7
 Number of edges = e = 6
A
B C
D E F
G
Binary Tree Traversals
 Traversal means visit each node in the
binary tree exactly once
 There are three commonly used tree traversals
 Inoder Traversal
 Preorder Traversal
 Postorder Traversal
Inorder Traversal
inorder
A
B C
D E F G
D B E A F C
G
Preorder Traversal
preorder
A
B C
D E F G
A B D E C F
G
Postorder Traversal
postorder
A
B C
D E F G
D E B F G C
A
Inorder Traversal
Left  Root  Right
Algorithm Inorder(tree)
1. If tree!= NULL then
1. call Inorder(treelChild)
2. Print treedata
3. call Inorder(treerChild)
Inorder Traversal
A
B C
D E F
G
Inorder Traversal:
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D B
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D B G
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D B G E
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D B G E A
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D B G E A C
Inorder Traversal
A
B C
D E F
G
Inorder Traversal: D B G E A C F
Inorder Traversal
struct node
{
int data;
struct node *lchild, *rchild;
}
void Inorder ( struct node * root)
{
if (root != NULL)
{
Inorder(root->lchild);
printf(“%d”,root->data);
Inorder(root->rchild);
}
}
b c
d e f g
d
a
b c
d e f g
a
d b
b c
d e f g
a
d b e
b c
d e f g
a
d b e a
b c
d e f g
a
d b e a f
b c
d e f g
a
d b e a f c
b c
d e f g
a
d b e a f c g
⚫H, D, I, B, E, A, J, F, C, K, G
Preorder Traversal
Root  Left  Right
Algorithm Preorder(tree)
1. If tree!= NULL then
1. Print treedata
2. call Preorder(treelChild)
3. call Preorder(treerChild)
Preorder Traversal
A
B C
D E F
G
Preorder Traversal:
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A B
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A B D
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A B D E
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A B D E G
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A B D E G C
Preorder Traversal
A
B C
D E F
G
Preorder Traversal: A B D E G C F
Preorder Traversal
struct node
{
int data;
struct node *lchild, *rchild;
}
void Preorder ( struct node * root)
{
if (root != NULL)
{
printf(“%d”, root ->data);
Preorder(root ->lchild);
Preorder(root ->rchild);
}
}
a
b c
d e f g
a
a
b c
d e f g
a b
a
b c
d e f g
a b d
a
b c
d e f g
a b d e
a
b c
d e f g
a b d e c
a
b c
d e f g
a b d e c f
a
b c
d e f g
a b d e c f g
⚫A, B, D, H, I, E, C, F, J, G, K
Postorder Traversal
Left  Right  Root
Algorithm Postorder(tree)
1. If tree!= NULL then
1. call Postorder(treelChild)
2. call Postorder(treerChild)
3. Print treedata
Postorder Traversal
A
B C
D E F
G
Postorder Traversal:
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D G
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D G E
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D G E B
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D G E B F
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D G E B F C
Postorder Traversal
A
B C
D E F
G
Postorder Traversal: D G E B F C A
Postorder Traversal
struct node
{
int data;
struct node *lchild, *rchild;
}
void Postorder ( struct node * root)
{
if (root != NULL)
{
Postorder(root->lchild);
Postorder(root->rchild);
printf(“%d”,root->data);
}
}
b c
d e f g
d
a
b c
d e f g
d e
a
b c
d e f g
d e b
a
b c
d e f g
a
d e b f
b c
d e f g
a
d e b f g
b c
d e f g
a
d e b f g c
b c
d e f g
a
d e b f g c a
⚫H, I, D, E, B, J, F, K, G, C, A
Binary Search
Tree
Binary Search Trees
 Binary search tree(BST) is a binary tree that is empty
or each node satisfies the following properties:
 Every element has a key, and no two elements have
the same key
 The keys in a nonempty left subtree must be
smaller than the key in the root of the subtree. The
keys in a nonempty right subtree must be larger
than the key in the root of the subtree
 The left and right subtrees are also BST
Binary Search Trees
K
Keys <
K
Keys> K
Binary Search Trees
5
3 6
1 4 8
7 9
5
3 6
1 2 9
7 8
Binary Search Tree Not a Binary Search Tree
Binary Search Trees
5
3 6
1 4 8
7 9
Inorder Traversal: 1 3 4 5 6 7 8 9
Inorder traversal of BST generate a sorted list
Operations on Binary Search Trees
 Four operations
 Searching
 Insertion
 Deletion
 Traversal
 The time complexity of searching, insertion
and deletion = O(h)
where h is the height of the tree.
Binary Search Trees: Searching
 Let item is the value to be searched
 Search begins at root
 If root is NULL search unsuccessful
 Else compare item with root
 If item is less than root then only the left sub tree is to be
searched. The sub tree may be searched recursively
• If item is greater than root then only the right sub tree is
to be searched. The sub tree may be searched recursively
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 68
66 69
Search Data Found
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 25
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 25
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 25
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 25
66 69
Binary Search Trees: Searching
30
20
10
65
80
24 50
45 70 90
68
Search 25
66 69
Search Data Not Found
Algorithm BST_Search(item)
1. ptr=root
2. flag=0
3. While ptr!=NULL and flag=0 do
1. If ptrdata=item then
1. flag=1
2. Else if ptrdata<item then
1. ptr=ptrrChild
3. Else
1. ptr=ptrlChild
4. If flag=1 then
1. Print “Search data found”
5. Else
1. Print “Search data not found”
Binary Search Trees: Insertion
 Suppose „item‟ is to be inserted in the BST
 Search for item
 If item is found then do nothing
 Else the item is inserted as left or right child where
the search halts
Algorithm BST_Insertion(item)
1. If ROOT=NULL then
1. Create a node new and insert item in to it. Set it as ROOT
node
2. Else
1. Search item in the tree
2. If search data found, insertion is not possible
3. Else
1. Find the parent node in which the item is to be inserted
2. Create a node new and insert item in to it.
3. If item<parentdata then
1. Insert new as the left child of parent
4. Else
1. Insert new as the right child of parent
Binary Search Trees: Insertion
30
5
2
40
25 80
Insert 20
Binary Search Trees: Insertion
30
5
2
40
25 80
Insert 20
Binary Search Trees: Insertion
30
5
2
40
25 80
Insert 20
Binary Search Trees: Insertion
30
5
2
40
25 80
Insert 20
Binary Search Trees: Insertion
30
5
2
40
25 80
Insert 20
30
5
2
40
25 80
20
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Algorithm BST_Insertion(item)
1. If ROOT=NULL then
1. Create a node new
2. newdata=item
3. newlChild=newrChild=NULL
4. ROOT=new
2. Else
1. ptr=ROOT
2. flag=0
3. While ptr!=NULL and flag=0 do
1. If item = ptrdata then
1. flag=1
2. Print “item al ready exist”
3. Exit
2. Else If item < ptrdata then
1. parent=ptr
2. ptr=ptrlChild
3. Else if item > ptrdata
1. parent=ptr
2. ptr=ptrrChild
4. If ptr=NULL then
1. Create a node new
2. newdata=item
3. newlChild=newrChild=NULL
4. If parentdata < item then
1. parentrChild=new
5. Else
1. parentlChild=new
Binary Search Trees: Deletion
Binary Search Trees: Deletion
Algorithm BST_Deletion()
1. Let x be a node to be deleted
2. If x is a leaf node then
1. Delete x
3. Else if x is a node with only one child then
1. Replace x with the child node of x
2. Delete x
4. Else if x having two children then
1. Find the inorder successor/predecessor of
x, say y
2. Find the right/left child of y, say z
3. Copy the data from y to x
Binary Search Trees: Deletion
Case 1: x is a leaf node
• Delete x
30
5
2
40
80
30
5
2
40
80
15
x
Delete 15
Case 1
Binary Search Trees: Deletion
Case 2: x is a node with only one
child
• Replace x with the child node of x
• Delete x
30
5
2
40
80
30
5
2
80
x Delete 40
Case 2
70 90
70 90
Case 3: x having two children
• Find the inorder successor, say y
• Find the right, say z
• Copy the data from y to x
• Replace y by z
30
10 80
20 60
24 50
45 70 90
65
68
Delete 60
66 69
10 80
Delete 60
Case 3: x having two children
• Find the inorder successor, say y
• Find the right, say z
• Copy the data from y to x
• Replace y by z
30
x
20 60
24 50
45 70 90
65
68
y
66 69
z
10 80
Case 3: x having two children
• Find the inorder successor, say y
• Find the right, say z
• Copy the data from y to x
• Replace y by z
30
x
20 65
24 50
45 70 90
65
68
y
66 69
z
Delete 60
10 80
Case 3: x having two children
• Find the inorder successor, say y
• Find the right, say z
• Copy the data from y to x
• Replace y by z
30
x
20 65
24 50
45 70 90
66 69
68 z
Delete 60
Algorithm BST_Deletion(item)
1. ptr=ROOT, flag=0
2. While ptr!=NULL and flag=0 do
1. If item < ptrdata then
1. parent=ptr
2. ptr=ptrlChild
2. Else if item > ptrdata then
1. parent=ptr
2. ptr=ptrrChild
3. Else
1. flag=1
3. If (flag==0)
1. Print “ITEM does not exist”
2. Exit
4. Else
1. If ptrrChild=NULL and ptrlChild=NULL then
1. If parentlChild=ptr then
1. parentlChild=NULL
2. Else
1. parentrChild=NULL
2. Else if ptrlChild!=NULL and ptrrChild!=NULL then
1. y is inorder successor of ptr
2. p is the parent of y
3. z is the right child of y
4. Copy the data from y to ptr
5. parentlChild=z
6. Dispose(y)
3. Else
1. If parentlChild=ptr then
1. If ptrlChild!=NULL then
1. parentlChild=ptrlChild
2. Else
1. parentlChild=ptrrChild
2. Else
1. If ptrlChild!=NULL then
1. parentrChild=ptrlChild
2. Else
1. parentrChild=ptrrChild
• A BST is constructed by inserting the following
numbers in the order:
60,25,72,15,30,68,101,13,18,47,70,34. The
number of nodes in the left subtree is
________.
Binary Search Trees: Applications
 Remove duplicate values
 Consider a collection of n data items A1,A2, …, AN.
Suppose we want to find and delete all duplicates
in the collection. Then, we can represent them as
BST and remove the duplicate values, while it
occurs.
 Find the smallest data- Traverse the left subtree of BST
 Find the largest data- Traverse the right subtree of BST
 Find a particular data- Traverse the whole BST
Height-balanced Binary
Search Trees
⚫A self-balancing (or height-balanced) binary search
tree is any binary search tree that automatically keeps
its height (number of levels below the root) small after
arbitrary item insertions and deletions.
⚫A balanced tree is a BST whose every node above the
last level has non-empty left and right subtree.
⚫Number of nodes in a complete binary tree of height h is
2h+1 – 1. Hence, a binary tree of n elements is balanced
if:
2h – 1 < n <= 2h+1 - 1
AVL
Trees
⚫Named after Russian Mathematicians: G.M. Adelson-
Velskii and E.M. Landis who discovered them in 1962.
⚫An AVL tree is a binary search tree which has the
following properties:
⚫The sub-trees of every node differ in height by at most
one.
⚫Every sub-tree is an AVL tree.
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
⚫Implementations of AVL tree insertion rely on adding
an extra attribute, the balance factor to each node.
⚫ Balance factor (bf) = HL - HR
⚫An empty binary tree is an AVL tree.
⚫A non-empty binary tree T is an AVL tree iff :
⚫ | HL - HR | <= 1
⚫For an AVL tree, the balance factor, HL - HR , of a node can
be either 0, 1 or -1.
⚫The balance factor indicates whether the tree is:
⚫left-heavy (the height of the left sub-tree is 1 greater than
the right sub-tree),
⚫balanced (both sub-trees are of the same height) or
⚫right-heavy (the height of the right sub-tree is 1 greater
than the left sub-tree).
+1
0
0
0
0
0
+1
0
0
-1
2
-1
+1
0
0
AVL Tree
AVL Tree
0 Not an AVL
Tree
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
⚫Insertion is similar to that of a BST.
⚫After inserting a node, it is necessary to check each of
the node's ancestors for consistency with the rules of
AVL.
⚫If after inserting an element, the balance of any tree is
destroyed then a rotation is performed to restore the
balance.
AVL Tree
Insertion
⚫For each node checked, if the balance factor remains
−1, 0, or +1 then no rotations are necessary.
⚫However, if balance factor becomes less than -1 or
greater than +1, the subtree rooted at this node is
unbalanced.
⚫Theorem: When an AVL tree becomes unbalanced after
an insertion, exactly one single or double rotation is
required to balance the tree.
⚫Let A be the root of the unbalanced subtree.
⚫There are four cases which need to be considered.
⚫Left-Left (LL) rotation
⚫Right-Right (RR) rotation
⚫Left-Right (LR) rotation
⚫Right-Left (RL) rotation
⚫Assume that A is the node which is unbalanced after
inserting a new node.
⚫LL Rotation: Inserted node is in the left subtree of
left subtree of node A.
⚫RR Rotation: Inserted node is in the right subtree of
right subtree of node A.
⚫LR Rotation: Inserted node is in the right subtree of
left subtree of node A.
⚫RL Rotation: Inserted node is in the left subtree of
right subtree of node A.
⚫LL Rotation: New element 2 is inserted in the left subtree
of left subtree of A , whose bf becomes +2 after insertion.
⚫To rebalance the tree, it is rotated so as to allow B to be
the root with BL and A to be its left subtree and right child
respectively, and BR and AR to be the left and right subtrees of
A.
6
4
A
B
0
+1
AR
BL
6
4
B
+2
A
AR
BR 2
+1
BL
BR
0
6
4
B
AR
BR
BL 2
0
0 A
0
A.Leftchild = B.rightchild
B.Rightchild = A
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
6
8
A
B
0
-1
AL
BR
BL
6
⚫RR Rotation: New element 10 is inserted in the right subtree
of right subtree of A , whose bf becomes -2 after insertion.
8
B
-1
AL
10
BL
⚫To rebalance the tree, it is rotated so as to allow B to be
the root with A as its left child and BR as its right subtree, and
AL and BL as the left and right subtrees of A respectively.
BR
-2 A
10
B
AL
A 6
0
8
0
0
A.Rightchild = B.leftchild
B.leftchild = A
BL
BR
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
• Create an AVL tree by inserting the elements
in the following order:
• 10,5,15,1,8,20,25,30,40,50
⚫Left-Left case and Left-Right case:
⚫If the balance factor of A is 2, then the left subtree
outweighs the right subtree of the given node, and the
balance factor of the left child L must be checked. The right
rotation with A as the root is necessary.
⚫If the balance factor of L is +1, a single right rotation (with A
as the root) is needed (Left-Left case).
⚫If the balance factor of L is -1, two different rotations are
needed. The first rotation is a left rotation with L as the root.
The second is a right rotation with A as the root (Left-Right
case).
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
⚫Right-Right case and Right-Left case:
⚫If the balance factor of A is -2 then the right subtree
outweighs the left subtree of the given node, and the balance
factor of the right child (R) must be checked. The left
rotation with A as the root is necessary.
⚫If the balance factor of R is -1, a single left rotation (with A
as the root) is needed (Right-Right case).
⚫If the balance factor of R is +1, two different rotations are
needed. The first rotation is a right rotation with R as the
root. The second is a left rotation with A as the root (Right-
Left case).
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
B.F
(A)
B.F.
(L)
B.F.
(R)
Type of
Rotation
Number
of
Rotations
Rotation
(Root for
Rotation)
+2 +1 LL Single Right (A)
+2 -1 LR Double Left (L) &
Right (A)
-2 -1 RR Single Left (A)
-2 +1 RL Double Right (R) &
Left (A)
• Construct AVL tree for the following data
21,26,30,9,4,14,28,18,15,10,2,3,7
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
AVL Tree
Deletion
⚫If the node is a leaf or has only one child, remove it.
⚫Otherwise, replace it with either the largest in its left subtree
(inorder predecessor) or the smallest in its right subtree
(inorder successor), and remove that node.
⚫The node that was found as a replacement has at most one
subtree.
⚫After deletion, retrace the path back up the tree (parent of the
replacement) to the root, adjusting the balance factors as
needed.
⚫The retracing can stop if the balance factor becomes −1
or
+1 indicating that the height of that subtree has
remained unchanged.
⚫If the balance factor becomes 0 then the height of the
subtree has decreased by one and the retracing needs to
continue.
⚫If the balance factor becomes −2 or +2 then the subtree
is unbalanced and needs to be rotated to fix it.
Reference
⚫Sanjay Pahuja, ‘A Practical Approach to Data
Structures and Algorithms’, First Ed. 2007.
⚫For Height Balanced Trees: AVL, B-Trees, refer:
⚫ Pg. 292 – 296, 301 – 315
https://www.cse.iitd.ac.in/~mausam/courses/col106/autumn2017/lectures/10-234trees.pdf
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
2-3-4 Trees or 2-4 Trees
https://www.cs.ucf.edu/courses/cop3530/sum2005/twofour.pdf
https://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13a.pdf
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
2-4 Trees
https://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13a.pdf
https://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13a.pdf
https://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13a.pdf
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx
• Construct a B-Tree (2-4 tree) with the
following keys inserted in order.
• 30,99,70,60,40,66,50,53,45,42,88,75,80,85,90
• Assume that in case of a split 2nd
key is send to
parent, 1st
goes to left child, 3rd
, and 4th
keys go
to right child.
• Delete 42,75,80,45,90.
After insertion
After deletion
Hash Tables
• A Hash table is defined as a data structure
used to insert, look up, and remove key-value
pairs quickly.
• It operates on the hashing, where each key is
translated by a hash function into a distinct
index in an array.
• The index functions as a storage location for
the matching value.
• There are majorly three components of hashing:
• Key: input to the hash function to determine an
index or location for storage of an item in a data
structure.
• Hash Function: function that maps the key to a
location in an array called a hash table. The index
is known as the hash index.
Most commonly used function:
hash (key) = (key%size)
• Hash Table: Hash table is a data structure that
maps keys to values using a special function called
a hash function. Hash stores the data in an
associative manner in an array where each data
value has its own unique index.
Collision Resolution
• Collision occurs when two or more keys map to the
same hash value.
• There are mainly two methods to handle collision:
• Separate Chaining - Separate linked list for each key
• Open Addressing - All values are in hash table itself.
– Linear Probing
– Quadratic Probing
– Double Hashing
THANK YOU
Ad

Recommended

Tree Data Structure Tree Data Structure Details
Tree Data Structure Tree Data Structure Details
ssusera8c91a
 
Data Structures -Non Linear DS-Basics ofTrees
Data Structures -Non Linear DS-Basics ofTrees
sailaja156145
 
Data structure using c module 2
Data structure using c module 2
smruti sarangi
 
Unit 3.ppt
Unit 3.ppt
JITTAYASHWANTHREDDY
 
7.tree
7.tree
Chandan Singh
 
Introduction to tree ds
Introduction to tree ds
Viji B
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
Aakash deep Singhal
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
HamzaUsman48
 
Tree 11.ppt
Tree 11.ppt
DEEPAK948083
 
non linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 
Lecture 5 trees
Lecture 5 trees
Victor Palmar
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
ssuser5c874e
 
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
SanketDawbhat
 
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
HaroldOmega1
 
Tree.pptx
Tree.pptx
worldchannel
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
UNIT-4 TREES.ppt
UNIT-4 TREES.ppt
SIVAKUMARM603675
 
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
Lecture 2-Trees in Data Structure Complete Lecture Slide
Lecture 2-Trees in Data Structure Complete Lecture Slide
KrishnenduRarhi
 
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
unit-2-data structure and algorithms-tree-2024-1.pptx
unit-2-data structure and algorithms-tree-2024-1.pptx
pritimalkhede
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
binary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 
8.haftajava notlarıiçeriyordökumanlar.pdf
8.haftajava notlarıiçeriyordökumanlar.pdf
Smeyyeztrk10
 
Basic Tree Data Structure BST Traversals .pptx
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
jyoti_lakhani
 
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
VISWANATHAN R V
 
tree Data Structures in python Traversals.pptx
tree Data Structures in python Traversals.pptx
RupaRaj6
 
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
CST413 KTU S7 CSE Machine Learning Supervised Learning Classification Algorit...
CST413 KTU S7 CSE Machine Learning Supervised Learning Classification Algorit...
resming1
 

More Related Content

Similar to Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx (20)

Tree 11.ppt
Tree 11.ppt
DEEPAK948083
 
non linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 
Lecture 5 trees
Lecture 5 trees
Victor Palmar
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
ssuser5c874e
 
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
SanketDawbhat
 
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
HaroldOmega1
 
Tree.pptx
Tree.pptx
worldchannel
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
UNIT-4 TREES.ppt
UNIT-4 TREES.ppt
SIVAKUMARM603675
 
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
Lecture 2-Trees in Data Structure Complete Lecture Slide
Lecture 2-Trees in Data Structure Complete Lecture Slide
KrishnenduRarhi
 
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
unit-2-data structure and algorithms-tree-2024-1.pptx
unit-2-data structure and algorithms-tree-2024-1.pptx
pritimalkhede
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
binary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 
8.haftajava notlarıiçeriyordökumanlar.pdf
8.haftajava notlarıiçeriyordökumanlar.pdf
Smeyyeztrk10
 
Basic Tree Data Structure BST Traversals .pptx
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
jyoti_lakhani
 
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
VISWANATHAN R V
 
tree Data Structures in python Traversals.pptx
tree Data Structures in python Traversals.pptx
RupaRaj6
 
non linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
ssuser5c874e
 
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
SanketDawbhat
 
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
HaroldOmega1
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
Lecture 2-Trees in Data Structure Complete Lecture Slide
Lecture 2-Trees in Data Structure Complete Lecture Slide
KrishnenduRarhi
 
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
unit-2-data structure and algorithms-tree-2024-1.pptx
unit-2-data structure and algorithms-tree-2024-1.pptx
pritimalkhede
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
8.haftajava notlarıiçeriyordökumanlar.pdf
8.haftajava notlarıiçeriyordökumanlar.pdf
Smeyyeztrk10
 
Basic Tree Data Structure BST Traversals .pptx
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
jyoti_lakhani
 
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
VISWANATHAN R V
 
tree Data Structures in python Traversals.pptx
tree Data Structures in python Traversals.pptx
RupaRaj6
 

More from resming1 (10)

CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
CST413 KTU S7 CSE Machine Learning Supervised Learning Classification Algorit...
CST413 KTU S7 CSE Machine Learning Supervised Learning Classification Algorit...
resming1
 
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
CST413 KTU S7 CSE Machine Learning Classification Assessment Confusion matrix...
CST413 KTU S7 CSE Machine Learning Classification Assessment Confusion matrix...
resming1
 
CST413_Machine Learning KTU S7 CSE PracticeQuestionsForExam_Solutions.pdf
CST413_Machine Learning KTU S7 CSE PracticeQuestionsForExam_Solutions.pdf
resming1
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
CST413 KTU S7 CSE Machine Learning Supervised Learning Classification Algorit...
CST413 KTU S7 CSE Machine Learning Supervised Learning Classification Algorit...
resming1
 
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
CST413 KTU S7 CSE Machine Learning Classification Assessment Confusion matrix...
CST413 KTU S7 CSE Machine Learning Classification Assessment Confusion matrix...
resming1
 
CST413_Machine Learning KTU S7 CSE PracticeQuestionsForExam_Solutions.pdf
CST413_Machine Learning KTU S7 CSE PracticeQuestionsForExam_Solutions.pdf
resming1
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Ad

Recently uploaded (20)

Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
How to Un-Obsolete Your Legacy Keypad Design
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Ad

Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL Trees B Trees Hash Tables.pptx

  • 1. Module III Introduction to Trees and Hash Tables
  • 2. Syllabus • Trees - Binary Trees, Binary Search Trees, AVL Trees, 2-4 trees, Hash tables, tries. • Binary Trees and Binary Search Trees - Operations, Representation, Traversals. Hash tables - concept of hashing, hash function, collision and collision resolution.
  • 3. Linear Data Structures • Arrays, linked lists, stacks and queues were examples of linear data structures in which elements are arranged in a linear fashion (ie, one dimensional representation).
  • 4. Non-linear Data Structures • Tree is another very useful data structure in which elements are appearing in a non-linear fashion, which requires a two dimensional representation.
  • 5. Tree  Tree is a nonlinear data structure  The elements appear in a non linear fashion, which require two dimensional representations.  Using tree it is easy to organize hierarchical representation of objects.  Tree is efficient for maintaining and manipulating data, where the hierarchy of relationship among the data is to be preserved.
  • 6. Tree  Definition: A tree is a finite set of one or more nodes such that there is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root. A B C D E F G H I J K
  • 7. Tree - Terminologies  Here there are 11 nodes and 10 edges  In any tree with N nodes there will be N-1 edges  Node: Every individual element in a tree is called a node  Edge/Links: Nodes are connected by using edges A B C D E F G H I J K
  • 8. Tree - Terminologies  Root: It is the origin of the tree data structure. In any tree, there must be only one root node. A B C D E F G H I J K  Here A is the root node  In any tree the first node is called root node
  • 9. Tree - Terminologies  Leaf Node/External Node/Terminal Node:  The node which does not have a child is called a leaf node.  A node without successor is called a leaf node. A B C D E F G H I J K
  • 10. Tree - Terminologies  Internal Node/Non-terminal Node:  An internal node is a node with atleast one child.  Every non leaf node is called Internal node A B C D E F G H I J K
  • 11. Tree - Terminologies  Parent:  The node which has child/children.  A node which is predecessor of any other node. A B C D E F G H I J K  Here A is the parent of B & C. B is the parent of D, E & F.
  • 12. Tree - Terminologies  Child: Descendant of any node is called a child node.  Here children of A are B & C. Children of B are D, E & F. A B C D E F G H I J K
  • 13. Order of nodes ⚫Children of a node are usually ordered from left-to- right. ⚫These are two distinct ordered trees. a b c a c b
  • 14. ⚫If a and b are siblings, and a is to the left of b, then all the descendants of a are to the left of all descendants of b. ⚫A tree in which the order of nodes is ignored is referred to as unordered tree.
  • 15. Tree - Terminologies  Sibling: The nodes with the same parent are called Sibling nodes. A B C D E F G H I J K
  • 16. Tree - Terminologies  Degree of the leaf node is 0. B C D G  Degree of a Node: Total number of the children of the given node 2 A 3 1 2 J 0 K 0 0 2 E 0 F H 0 I 0
  • 17. Tree - Terminologies in a given tree  Here maximum degree is for node B. Its degree is 3.  So the degree of the given tree is 3 A B C G  Degree of a Tree: It is the maximum degree of nodes 2 3 1 2 J 0 K 0 0 D 2 E 0 F H 0 I 0
  • 18. Tree - Terminologies  Level: In a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level A B C D E F G H I J K Level 0 Level 1 Level 2 Level 3
  • 19. Tree - Terminologies  Depth of a node: It is the total number of edges from root to that node. A B C D E F G H I  Depth of node E is J K
  • 20. Tree - Terminologies  Depth of the tree: It is the total number of edges from root to leaf in the longest path A B C D E F G H I J K  Depth of the tree = 3
  • 21. Tree - Terminologies  Height of a node: It is the total number of edges from longest leaf node in its subtree to that node. A B C D E F G H I J K  Height of node B is 2
  • 22. Tree - Terminologies  Height of the tree: It is the total number of edges from longest path leaf node to root A B C D E F G H I J K  Height of the tree = 3
  • 23. Tree - Terminologies  Path: Sequence of nodes and edges between two nodes  Length of the path: Total number of edges in the path A B C D E F G H I J K  Path between B and H is B-E-H. Its length is 2  There is no path between B and G
  • 24. Tree - Terminologies  The ancestors of a node are all the nodes along the path from the root to the node.  The immediate ancestor of a node is the “parent” node A B C D E F G H I  J K
  • 25. Tree - Terminologies  A descendant node of a node is any node in the path from that node to the leaf node.  The immediate descendant of a node is the “child” node. A B C D E F G H I J K  Descendant nodes of B are D, E, F, H & I
  • 26. Tree - Terminologies  Subtrees A B C D E F G H I J K
  • 27. Tree - Terminologies  Subtrees A B C D E F G H I J K
  • 28. Tree Representations 1. List Representation 2. Left Child Right Sibling Representation 3. Representation as a degree 2 tree
  • 29. List Representations  Root comes first, followed by list of sub-trees data link 1 link 2 ... link n
  • 30. Left child Right sibling Representations  Fixed sized nodes  Easier to work  Two link/pointer fields per node A B C D E F G H I J K A B C D E F G H I J K Left_child data Right_Sibling
  • 31. Representations as degree 2 tree  Also known as Left child-Right child Tree/ Degree - Two Tree/ Binary Tree  Simply rotate the right sibling pointers in the Left- child Right-sibling tree clockwise by 45 degrees
  • 32. Representations as degree 2 tree A B C D E F G A B C D E F G H I J K Tree H I J K Left Child Right Sibling Representation
  • 34. Tree - Applications  Storing naturally hierarchical data: Trees are used to store the data in the hierarchical structure. Example: Directory structure of a file system  Organize data: It is used to organize data for efficient insertion, deletion and searching.  Used in compression algorithms  Routing table: The tree data structure is also used to store the data in routing tables in the routers.  To implement Heap data structure: It is used to implement priority queues
  • 35. Binary Trees  A Binary Tree is a finite set of nodes that is either  Empty or  Consists of root node and two disjoint binary trees, called, left subtree and right subtree A B C D E F A B A
  • 36. Binary Trees  Any tree can be transformed into binary tree by left child-right sibling representation  A tree can never be empty but binary tree can be  In binary tree a node can have atmost 2 children, whereas in case of a tree, a node may have any number of children
  • 37. Different Binary Trees  Skewed Binary Tree  Left Skewed Binary Tree  Right Skewed Binary Tree  Complete Binary Tree  Full/Strictly Binary Tree
  • 39. Complete Binary Trees  A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and the last level has all its nodes to the left side A B C D E F H I G
  • 40. Full/Strictly Binary Trees  Every node other than the leaves has two children A B C D E F H I G
  • 41. Binary Tree Representations 1. Array/Sequential/Linear Representation 2. Linked Representation
  • 42. Binary Tree -Array Representation  It is a sequential representation  Use a 1-D array to store the nodes  Block of memory for an array is allocated before storing the actual tree in it.  Once the memory is allocated, the size of tree is restricted as permitted by the memory.  Nodes are stored level by level, starting from zero level  The root node stored in first memory location
  • 43. Binary Tree -Array Representation  Determine the locations of the parent, left child, and right child of any node i in the binary tree(1≤i ≤n)  parent(i)  If i !=1, then parent(i)= floor(i/2)  if i =1, i is at root and has no parent  left_child(i)  left_child(i) = 2i  No left child If 2i ≤ n Otherwis e  right_child(i)  right_child(i) = 2i+1  No right child If 2i+1 ≤ n Otherwise
  • 44. Binary Tree -Array Representations A B C D E F G
  • 45. Binary Tree -Array Representations B C D E F 1 A 2 3 4 5 7 10 G A B C D E F G H
  • 46. Binary Tree -Array Representations A B C D E F G B C D E F 1 2 1 A 2 3 4 5 7 G 10 3 4 5 6 7 8 9 10
  • 47. Binary Tree -Array Representations A B C D E G F
  • 48. Binary Tree -Array Representations A B C D E F G B C D E F 1 2 3 4 5 6 7 1 A 2 3 4 7 F 5 6
  • 49. Binary Tree -Array Representations A B C D
  • 50. Binary Tree -Array Representations A B C D A B C D 1 2 3 4 5 6 7 8 1 2 4 8
  • 51. Binary Tree -Array Representations  Advantages  Any node can be accessed from any other node by calculating the index.  Data are stored without any pointer  Programming languages, where dynamic memory allocation is not possible(like BASIC, FROTRAN), array representation is only possible  Good for Complete Binary tree.  Searching is fast
  • 52. Binary Tree -Array Representations  Disadvantages  Good for Complete Binary tree. If it is not a complete binary tree then, a lot of memory is wasted  Dynamic memory allocation is not possible
  • 53. Binary Tree -Linked Representations data Left_chil d Right_child Left_child data Right_child
  • 54. Binary Tree -Linked Representations A B C D E F G A B C F D E G NULL NULL NULL NULL NULL NULL NULL NULL
  • 55. Binary Tree –Linked Representations A B C D A B C D NULL NULL NULL NULL NULL
  • 56. Binary Tree -Linked Representations  Advantages  Dynamic memory allocation is possible
  • 57. Binary Tree -Properties  The maximum number of nodes on level i of a binary tree is 2i, i>=0 A B C D E G F H I J K L M N O Level 0 No. of Nodes 1 1 2 2 4 3 8 i 2i
  • 58. Binary Tree -Properties  The maximum number of nodes in a binary tree of height h is 2h+1-1, h≥0 A Height 0 No. of Nodes 1
  • 59. Binary Tree -Properties  The maximum number of nodes in a binary tree of height h is 2h+1-1, h≥0 A B C Height 0 No. of Nodes 1 1 3
  • 60. Binary Tree -Properties  The maximum number of nodes in a binary tree of height h is 2h+1-1, h≥0 A B C D E G F Height 0 No. of Nodes 1 1 3 2 7
  • 61. Binary Tree -Properties  The maximum number of nodes in a binary tree of height h is 2h+1-1, h≥0 A B C D E G F H I J K L M N O Height 0 No. of Nodes 1 1 3 2 7 3 15
  • 62. Binary Tree -Properties  The maximum number of nodes in a binary tree of height h is 2h+1-1, h≥0 A B C D E G F H I J K L M N O Height 0 No. of Nodes 1 1 3 2 7 3 15 h 2h+1-1
  • 63. Binary Tree -Properties  For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0 =n2 +1 A B C D E G F J M  Number of nodes with degree 2 = n2 = 3
  • 64. Binary Tree -Properties  Minimum number of nodes possible in a binary tree ofheight h is h+1  Consider this skewed binary tree  Here, height of the tree = 3  Minimum number of nodes possible A B C D
  • 65. Binary Tree -Properties  For any non empty binary tree, if n is the number of nodes and e is the number of edges then n=e+1  Number of nodes = n = 7  Number of edges = e = 6 A B C D E F G
  • 66. Binary Tree Traversals  Traversal means visit each node in the binary tree exactly once  There are three commonly used tree traversals  Inoder Traversal  Preorder Traversal  Postorder Traversal
  • 67. Inorder Traversal inorder A B C D E F G D B E A F C G
  • 68. Preorder Traversal preorder A B C D E F G A B D E C F G
  • 70. Inorder Traversal Left  Root  Right Algorithm Inorder(tree) 1. If tree!= NULL then 1. call Inorder(treelChild) 2. Print treedata 3. call Inorder(treerChild)
  • 71. Inorder Traversal A B C D E F G Inorder Traversal:
  • 72. Inorder Traversal A B C D E F G Inorder Traversal: D
  • 73. Inorder Traversal A B C D E F G Inorder Traversal: D B
  • 74. Inorder Traversal A B C D E F G Inorder Traversal: D B G
  • 75. Inorder Traversal A B C D E F G Inorder Traversal: D B G E
  • 76. Inorder Traversal A B C D E F G Inorder Traversal: D B G E A
  • 77. Inorder Traversal A B C D E F G Inorder Traversal: D B G E A C
  • 78. Inorder Traversal A B C D E F G Inorder Traversal: D B G E A C F
  • 79. Inorder Traversal struct node { int data; struct node *lchild, *rchild; } void Inorder ( struct node * root) { if (root != NULL) { Inorder(root->lchild); printf(“%d”,root->data); Inorder(root->rchild); } }
  • 80. b c d e f g d a
  • 81. b c d e f g a d b
  • 82. b c d e f g a d b e
  • 83. b c d e f g a d b e a
  • 84. b c d e f g a d b e a f
  • 85. b c d e f g a d b e a f c
  • 86. b c d e f g a d b e a f c g
  • 87. ⚫H, D, I, B, E, A, J, F, C, K, G
  • 88. Preorder Traversal Root  Left  Right Algorithm Preorder(tree) 1. If tree!= NULL then 1. Print treedata 2. call Preorder(treelChild) 3. call Preorder(treerChild)
  • 89. Preorder Traversal A B C D E F G Preorder Traversal:
  • 90. Preorder Traversal A B C D E F G Preorder Traversal: A
  • 91. Preorder Traversal A B C D E F G Preorder Traversal: A B
  • 92. Preorder Traversal A B C D E F G Preorder Traversal: A B D
  • 93. Preorder Traversal A B C D E F G Preorder Traversal: A B D E
  • 94. Preorder Traversal A B C D E F G Preorder Traversal: A B D E G
  • 95. Preorder Traversal A B C D E F G Preorder Traversal: A B D E G C
  • 96. Preorder Traversal A B C D E F G Preorder Traversal: A B D E G C F
  • 97. Preorder Traversal struct node { int data; struct node *lchild, *rchild; } void Preorder ( struct node * root) { if (root != NULL) { printf(“%d”, root ->data); Preorder(root ->lchild); Preorder(root ->rchild); } }
  • 98. a b c d e f g a
  • 99. a b c d e f g a b
  • 100. a b c d e f g a b d
  • 101. a b c d e f g a b d e
  • 102. a b c d e f g a b d e c
  • 103. a b c d e f g a b d e c f
  • 104. a b c d e f g a b d e c f g
  • 105. ⚫A, B, D, H, I, E, C, F, J, G, K
  • 106. Postorder Traversal Left  Right  Root Algorithm Postorder(tree) 1. If tree!= NULL then 1. call Postorder(treelChild) 2. call Postorder(treerChild) 3. Print treedata
  • 107. Postorder Traversal A B C D E F G Postorder Traversal:
  • 108. Postorder Traversal A B C D E F G Postorder Traversal: D
  • 109. Postorder Traversal A B C D E F G Postorder Traversal: D G
  • 110. Postorder Traversal A B C D E F G Postorder Traversal: D G E
  • 111. Postorder Traversal A B C D E F G Postorder Traversal: D G E B
  • 112. Postorder Traversal A B C D E F G Postorder Traversal: D G E B F
  • 113. Postorder Traversal A B C D E F G Postorder Traversal: D G E B F C
  • 114. Postorder Traversal A B C D E F G Postorder Traversal: D G E B F C A
  • 115. Postorder Traversal struct node { int data; struct node *lchild, *rchild; } void Postorder ( struct node * root) { if (root != NULL) { Postorder(root->lchild); Postorder(root->rchild); printf(“%d”,root->data); } }
  • 116. b c d e f g d a
  • 117. b c d e f g d e a
  • 118. b c d e f g d e b a
  • 119. b c d e f g a d e b f
  • 120. b c d e f g a d e b f g
  • 121. b c d e f g a d e b f g c
  • 122. b c d e f g a d e b f g c a
  • 123. ⚫H, I, D, E, B, J, F, K, G, C, A
  • 125. Binary Search Trees  Binary search tree(BST) is a binary tree that is empty or each node satisfies the following properties:  Every element has a key, and no two elements have the same key  The keys in a nonempty left subtree must be smaller than the key in the root of the subtree. The keys in a nonempty right subtree must be larger than the key in the root of the subtree  The left and right subtrees are also BST
  • 127. Binary Search Trees 5 3 6 1 4 8 7 9 5 3 6 1 2 9 7 8 Binary Search Tree Not a Binary Search Tree
  • 128. Binary Search Trees 5 3 6 1 4 8 7 9 Inorder Traversal: 1 3 4 5 6 7 8 9 Inorder traversal of BST generate a sorted list
  • 129. Operations on Binary Search Trees  Four operations  Searching  Insertion  Deletion  Traversal  The time complexity of searching, insertion and deletion = O(h) where h is the height of the tree.
  • 130. Binary Search Trees: Searching  Let item is the value to be searched  Search begins at root  If root is NULL search unsuccessful  Else compare item with root  If item is less than root then only the left sub tree is to be searched. The sub tree may be searched recursively • If item is greater than root then only the right sub tree is to be searched. The sub tree may be searched recursively
  • 131. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69
  • 132. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69
  • 133. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69
  • 134. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69
  • 135. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69
  • 136. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69
  • 137. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 68 66 69 Search Data Found
  • 138. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 25 66 69
  • 139. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 25 66 69
  • 140. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 25 66 69
  • 141. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 25 66 69
  • 142. Binary Search Trees: Searching 30 20 10 65 80 24 50 45 70 90 68 Search 25 66 69 Search Data Not Found
  • 143. Algorithm BST_Search(item) 1. ptr=root 2. flag=0 3. While ptr!=NULL and flag=0 do 1. If ptrdata=item then 1. flag=1 2. Else if ptrdata<item then 1. ptr=ptrrChild 3. Else 1. ptr=ptrlChild 4. If flag=1 then 1. Print “Search data found” 5. Else 1. Print “Search data not found”
  • 144. Binary Search Trees: Insertion  Suppose „item‟ is to be inserted in the BST  Search for item  If item is found then do nothing  Else the item is inserted as left or right child where the search halts
  • 145. Algorithm BST_Insertion(item) 1. If ROOT=NULL then 1. Create a node new and insert item in to it. Set it as ROOT node 2. Else 1. Search item in the tree 2. If search data found, insertion is not possible 3. Else 1. Find the parent node in which the item is to be inserted 2. Create a node new and insert item in to it. 3. If item<parentdata then 1. Insert new as the left child of parent 4. Else 1. Insert new as the right child of parent
  • 146. Binary Search Trees: Insertion 30 5 2 40 25 80 Insert 20
  • 147. Binary Search Trees: Insertion 30 5 2 40 25 80 Insert 20
  • 148. Binary Search Trees: Insertion 30 5 2 40 25 80 Insert 20
  • 149. Binary Search Trees: Insertion 30 5 2 40 25 80 Insert 20
  • 150. Binary Search Trees: Insertion 30 5 2 40 25 80 Insert 20 30 5 2 40 25 80 20
  • 152. Algorithm BST_Insertion(item) 1. If ROOT=NULL then 1. Create a node new 2. newdata=item 3. newlChild=newrChild=NULL 4. ROOT=new 2. Else 1. ptr=ROOT 2. flag=0 3. While ptr!=NULL and flag=0 do 1. If item = ptrdata then 1. flag=1 2. Print “item al ready exist” 3. Exit
  • 153. 2. Else If item < ptrdata then 1. parent=ptr 2. ptr=ptrlChild 3. Else if item > ptrdata 1. parent=ptr 2. ptr=ptrrChild 4. If ptr=NULL then 1. Create a node new 2. newdata=item 3. newlChild=newrChild=NULL 4. If parentdata < item then 1. parentrChild=new 5. Else 1. parentlChild=new
  • 154. Binary Search Trees: Deletion
  • 155. Binary Search Trees: Deletion Algorithm BST_Deletion() 1. Let x be a node to be deleted 2. If x is a leaf node then 1. Delete x 3. Else if x is a node with only one child then 1. Replace x with the child node of x 2. Delete x 4. Else if x having two children then 1. Find the inorder successor/predecessor of x, say y 2. Find the right/left child of y, say z 3. Copy the data from y to x
  • 156. Binary Search Trees: Deletion Case 1: x is a leaf node • Delete x 30 5 2 40 80 30 5 2 40 80 15 x Delete 15 Case 1
  • 157. Binary Search Trees: Deletion Case 2: x is a node with only one child • Replace x with the child node of x • Delete x 30 5 2 40 80 30 5 2 80 x Delete 40 Case 2 70 90 70 90
  • 158. Case 3: x having two children • Find the inorder successor, say y • Find the right, say z • Copy the data from y to x • Replace y by z 30 10 80 20 60 24 50 45 70 90 65 68 Delete 60 66 69
  • 159. 10 80 Delete 60 Case 3: x having two children • Find the inorder successor, say y • Find the right, say z • Copy the data from y to x • Replace y by z 30 x 20 60 24 50 45 70 90 65 68 y 66 69 z
  • 160. 10 80 Case 3: x having two children • Find the inorder successor, say y • Find the right, say z • Copy the data from y to x • Replace y by z 30 x 20 65 24 50 45 70 90 65 68 y 66 69 z Delete 60
  • 161. 10 80 Case 3: x having two children • Find the inorder successor, say y • Find the right, say z • Copy the data from y to x • Replace y by z 30 x 20 65 24 50 45 70 90 66 69 68 z Delete 60
  • 162. Algorithm BST_Deletion(item) 1. ptr=ROOT, flag=0 2. While ptr!=NULL and flag=0 do 1. If item < ptrdata then 1. parent=ptr 2. ptr=ptrlChild 2. Else if item > ptrdata then 1. parent=ptr 2. ptr=ptrrChild 3. Else 1. flag=1 3. If (flag==0) 1. Print “ITEM does not exist” 2. Exit
  • 163. 4. Else 1. If ptrrChild=NULL and ptrlChild=NULL then 1. If parentlChild=ptr then 1. parentlChild=NULL 2. Else 1. parentrChild=NULL 2. Else if ptrlChild!=NULL and ptrrChild!=NULL then 1. y is inorder successor of ptr 2. p is the parent of y 3. z is the right child of y 4. Copy the data from y to ptr 5. parentlChild=z 6. Dispose(y)
  • 164. 3. Else 1. If parentlChild=ptr then 1. If ptrlChild!=NULL then 1. parentlChild=ptrlChild 2. Else 1. parentlChild=ptrrChild 2. Else 1. If ptrlChild!=NULL then 1. parentrChild=ptrlChild 2. Else 1. parentrChild=ptrrChild
  • 165. • A BST is constructed by inserting the following numbers in the order: 60,25,72,15,30,68,101,13,18,47,70,34. The number of nodes in the left subtree is ________.
  • 166. Binary Search Trees: Applications  Remove duplicate values  Consider a collection of n data items A1,A2, …, AN. Suppose we want to find and delete all duplicates in the collection. Then, we can represent them as BST and remove the duplicate values, while it occurs.  Find the smallest data- Traverse the left subtree of BST  Find the largest data- Traverse the right subtree of BST  Find a particular data- Traverse the whole BST
  • 167. Height-balanced Binary Search Trees ⚫A self-balancing (or height-balanced) binary search tree is any binary search tree that automatically keeps its height (number of levels below the root) small after arbitrary item insertions and deletions. ⚫A balanced tree is a BST whose every node above the last level has non-empty left and right subtree. ⚫Number of nodes in a complete binary tree of height h is 2h+1 – 1. Hence, a binary tree of n elements is balanced if: 2h – 1 < n <= 2h+1 - 1
  • 168. AVL Trees ⚫Named after Russian Mathematicians: G.M. Adelson- Velskii and E.M. Landis who discovered them in 1962. ⚫An AVL tree is a binary search tree which has the following properties: ⚫The sub-trees of every node differ in height by at most one. ⚫Every sub-tree is an AVL tree.
  • 170. ⚫Implementations of AVL tree insertion rely on adding an extra attribute, the balance factor to each node. ⚫ Balance factor (bf) = HL - HR ⚫An empty binary tree is an AVL tree. ⚫A non-empty binary tree T is an AVL tree iff : ⚫ | HL - HR | <= 1 ⚫For an AVL tree, the balance factor, HL - HR , of a node can be either 0, 1 or -1.
  • 171. ⚫The balance factor indicates whether the tree is: ⚫left-heavy (the height of the left sub-tree is 1 greater than the right sub-tree), ⚫balanced (both sub-trees are of the same height) or ⚫right-heavy (the height of the right sub-tree is 1 greater than the left sub-tree).
  • 175. ⚫Insertion is similar to that of a BST. ⚫After inserting a node, it is necessary to check each of the node's ancestors for consistency with the rules of AVL. ⚫If after inserting an element, the balance of any tree is destroyed then a rotation is performed to restore the balance. AVL Tree Insertion
  • 176. ⚫For each node checked, if the balance factor remains −1, 0, or +1 then no rotations are necessary. ⚫However, if balance factor becomes less than -1 or greater than +1, the subtree rooted at this node is unbalanced.
  • 177. ⚫Theorem: When an AVL tree becomes unbalanced after an insertion, exactly one single or double rotation is required to balance the tree. ⚫Let A be the root of the unbalanced subtree. ⚫There are four cases which need to be considered. ⚫Left-Left (LL) rotation ⚫Right-Right (RR) rotation ⚫Left-Right (LR) rotation ⚫Right-Left (RL) rotation
  • 178. ⚫Assume that A is the node which is unbalanced after inserting a new node. ⚫LL Rotation: Inserted node is in the left subtree of left subtree of node A. ⚫RR Rotation: Inserted node is in the right subtree of right subtree of node A. ⚫LR Rotation: Inserted node is in the right subtree of left subtree of node A. ⚫RL Rotation: Inserted node is in the left subtree of right subtree of node A.
  • 179. ⚫LL Rotation: New element 2 is inserted in the left subtree of left subtree of A , whose bf becomes +2 after insertion. ⚫To rebalance the tree, it is rotated so as to allow B to be the root with BL and A to be its left subtree and right child respectively, and BR and AR to be the left and right subtrees of A. 6 4 A B 0 +1 AR BL 6 4 B +2 A AR BR 2 +1 BL BR 0
  • 180. 6 4 B AR BR BL 2 0 0 A 0 A.Leftchild = B.rightchild B.Rightchild = A
  • 182. 6 8 A B 0 -1 AL BR BL 6 ⚫RR Rotation: New element 10 is inserted in the right subtree of right subtree of A , whose bf becomes -2 after insertion. 8 B -1 AL 10 BL ⚫To rebalance the tree, it is rotated so as to allow B to be the root with A as its left child and BR as its right subtree, and AL and BL as the left and right subtrees of A respectively. BR -2 A
  • 183. 10 B AL A 6 0 8 0 0 A.Rightchild = B.leftchild B.leftchild = A BL BR
  • 185. • Create an AVL tree by inserting the elements in the following order: • 10,5,15,1,8,20,25,30,40,50
  • 186. ⚫Left-Left case and Left-Right case: ⚫If the balance factor of A is 2, then the left subtree outweighs the right subtree of the given node, and the balance factor of the left child L must be checked. The right rotation with A as the root is necessary. ⚫If the balance factor of L is +1, a single right rotation (with A as the root) is needed (Left-Left case). ⚫If the balance factor of L is -1, two different rotations are needed. The first rotation is a left rotation with L as the root. The second is a right rotation with A as the root (Left-Right case).
  • 188. ⚫Right-Right case and Right-Left case: ⚫If the balance factor of A is -2 then the right subtree outweighs the left subtree of the given node, and the balance factor of the right child (R) must be checked. The left rotation with A as the root is necessary. ⚫If the balance factor of R is -1, a single left rotation (with A as the root) is needed (Right-Right case). ⚫If the balance factor of R is +1, two different rotations are needed. The first rotation is a right rotation with R as the root. The second is a left rotation with A as the root (Right- Left case).
  • 191. B.F (A) B.F. (L) B.F. (R) Type of Rotation Number of Rotations Rotation (Root for Rotation) +2 +1 LL Single Right (A) +2 -1 LR Double Left (L) & Right (A) -2 -1 RR Single Left (A) -2 +1 RL Double Right (R) & Left (A)
  • 192. • Construct AVL tree for the following data 21,26,30,9,4,14,28,18,15,10,2,3,7
  • 201. AVL Tree Deletion ⚫If the node is a leaf or has only one child, remove it. ⚫Otherwise, replace it with either the largest in its left subtree (inorder predecessor) or the smallest in its right subtree (inorder successor), and remove that node. ⚫The node that was found as a replacement has at most one subtree. ⚫After deletion, retrace the path back up the tree (parent of the replacement) to the root, adjusting the balance factors as needed.
  • 202. ⚫The retracing can stop if the balance factor becomes −1 or +1 indicating that the height of that subtree has remained unchanged. ⚫If the balance factor becomes 0 then the height of the subtree has decreased by one and the retracing needs to continue. ⚫If the balance factor becomes −2 or +2 then the subtree is unbalanced and needs to be rotated to fix it.
  • 203. Reference ⚫Sanjay Pahuja, ‘A Practical Approach to Data Structures and Algorithms’, First Ed. 2007. ⚫For Height Balanced Trees: AVL, B-Trees, refer: ⚫ Pg. 292 – 296, 301 – 315
  • 207. 2-3-4 Trees or 2-4 Trees https://www.cs.ucf.edu/courses/cop3530/sum2005/twofour.pdf
  • 221. • Construct a B-Tree (2-4 tree) with the following keys inserted in order. • 30,99,70,60,40,66,50,53,45,42,88,75,80,85,90 • Assume that in case of a split 2nd key is send to parent, 1st goes to left child, 3rd , and 4th keys go to right child. • Delete 42,75,80,45,90.
  • 224. Hash Tables • A Hash table is defined as a data structure used to insert, look up, and remove key-value pairs quickly. • It operates on the hashing, where each key is translated by a hash function into a distinct index in an array. • The index functions as a storage location for the matching value.
  • 225. • There are majorly three components of hashing: • Key: input to the hash function to determine an index or location for storage of an item in a data structure. • Hash Function: function that maps the key to a location in an array called a hash table. The index is known as the hash index. Most commonly used function: hash (key) = (key%size) • Hash Table: Hash table is a data structure that maps keys to values using a special function called a hash function. Hash stores the data in an associative manner in an array where each data value has its own unique index.
  • 226. Collision Resolution • Collision occurs when two or more keys map to the same hash value. • There are mainly two methods to handle collision: • Separate Chaining - Separate linked list for each key • Open Addressing - All values are in hash table itself. – Linear Probing – Quadratic Probing – Double Hashing