Professional Documents
Culture Documents
COL 106
Shweta Agrawal, Amit Kumar, Dr.
Ilyas Cicekli, Naveen Garg
Multi-Way Trees
Searching 22
" Similar to binary searching
for s = 8
Searching
3 If search key s<k1 search the 5 10
for s = 12
25
leftmost child
3 If s>kd-1 , search the rightmost
child 33 44 6 8 14 23 24 27
" Properties:
12
3 Each node has at
most 4 children
3 All external nodes
5 10 15
have same depth
3 Height h of (2,4)
tree is O(log n).
3 4 6 8 11 13 14 17
" How is the last
fact useful in
searching?
13 22 32
3 8 10 18 25 35
1 2 4 5 6 9 11 12 14 15 20 24 26 28 30 33 37 39
Insertion(2)
" Nodes get split if there is
29 7 insufficient space.
13 22 32
3 8 10 18 25 35
1 2 4 5 6 9 11 12 14 15 20 21 23 24 26 28 30 33 37 39 40
Insertion(3)
7
" One key is promoted to
parent and inserted in there
13 22 32
3 8 10 18 25 35
1 2 4 5 6 9 11 12 14 15 20 21 23 24 33 37 39 40
26 28 29 30
Insertion(4)
" If parent node does not have sufficient space then it
is split.
" In this manner splits can cascade.
13 22 32
3 8 10 18 25 28 35
1 2 9 11 12 14 15 20 21 23 24 33 37 39 40
4 5 6 7 26 29 30
Insertion(5)
" Eventually we may have to create a new root.
" This increases the height of the tree
5 13 22 32
3 8 10
18 25 28 35
1 2 9 11 12 14 15 20 21 23 24 33 37 39 40
4 6 7 26 29 30
Time for Search and Insertion
" A search visits O(log N) nodes
" An insertion requires O(log N) node splits
" Each node split takes constant time
" Hence, operations Search and Insert each take time O(log
N)
Deletion
" Delete 21.
" No problem if key to be deleted is in a leaf with at least 2 keys
13
5 22
3 8 10
18 25 28
1 2 9 11 12 14 15 20 21 23 24
4 6 7 26 29 30
Deletion(2)
" If key to be deleted is in an internal node then we
swap it with its predecessor (which is in a leaf) and
then delete it.
" Delete 25 13
5 22
3 8 10
18 25 28
1 2 9 11 12 14 15 20 23 24
4 6 7 26 29 30
Deletion(3)
" If after deleting a key a node becomes empty then
we borrow a key from its sibling.
" Delete 20
13
5 22
3 8 10
18 24 28
1 2 9 11 12 14 15 20 23
4 6 7 26 29 30
Deletion(4)
" If sibling has only one key then we merge with it.
" The key in the parent node separating these two siblings
moves down into the merged node.
" Delete 23 13
5 22
3 8 10
15 24 28
1 2 9 11 12 14 18 23
4 6 7 26 29 30
Delete(5)
" Moving a key down from the parent corresponds to
deletion in the parent node.
" The procedure is the same as for a leaf node.
" Can lead to a cascade . 13
" Delete 18 5 22
3 8 10
15 28
1 2 9 11 12 14 18
4 6 7 29 30
24 26
(2,4) Conclusion
" The height of a (2,4) tree is O(log n).
" Split, transfer, and merge each take O(1).
" Search, insertion and deletion each take
O(log n) .
" Why are we doing this?
3 (2,4) trees are fun! Why else would we do it?
3 Well, there9s another reason, too.
3 They can be extended to what are called B-trees.
(a,b) Trees
" A multiway search tree.
" Each node has at least a
and at most b children. 12
13 22
3 8 18 25
1 2 4 5 9 14 15 20 24 26 28
Insertion(2)
" Nodes get split if there is insufficient
space.
29 7
" The median key is promoted to the
parent node and inserted there
13 22
3 8 18 25
1 2 4 5 9 14 15 20 23 24 26 28
Insertion(3)
5 22
3 8 10
18 24 28
1 2 4 6 7 9 11 12 14 15 20 23 26 29 30
Deletion(2)
" If sibling has only one key then we merge with it.
" The key in the parent node separating these two
siblings moves down into the merged node.
" Delete 23
13
5 22
3 8 10
18 24 28
1 2 4 6 7 9 11 12 14 15 20 23 26 29 30
Deletion(3)
" In an (a,b) tree we will merge a node with its
sibling if the node has a-2 keys and its sibling
has a-1 keys.
" Thus the merged node has 2(a-1) keys.
" This implies that 2(a-1) <= b-1 which is
equivalent to a-1 <= .
" Earlier too we argued that a-1 <=
" This implies b >= 2a-1
" For a=2, b >= 3
Conclusion
" The height of a (a,b) tree is O(log n).
" b >= 2a-1.
" For insertion and deletion we take time
proportional to the height.
Disk Based Data Structures
" Operations:
3 DiskRead(x:pointer_to_a_node)
3 DiskWrite(x:pointer_to_a_node)
3 AllocateNode():pointer_to_a_node
Binary-trees vs. B-trees
" Size of B-tree nodes is determined by the page size. One
page 3 one node.
" A B-tree of height 2 may contain > 1 billion keys!
" Heights of Binary-tree and B-tree are logarithmic
3 B-tree: logarithm of base, e.g., 1000
3 Binary-tree: logarithm of base 2
1 node
1000 1000 keys
1001
1001 nodes,
1000 1000 & 1000
1,001,000 keys
1001 1001 1001
1,002,001 nodes,
1000 1000 & 1000
1,002,001,000 keys
B-tree Definitions
" Node x has fields
3 n[x]: the number of keys of that the node
3 key1[x] ó & ó keyn[x][x]: the keys in ascending order
3 leaf[x]: true if leaf node, false if internal node
3 if internal node, then c1[x], &, cn[x]+1[x]: pointers to children
" Keys separate the ranges of keys in the sub-trees. If ki
is an arbitrary key in the subtree ci[x] then kió keyi[x]
ó ki+1
B-tree Definitions (2)
" Every leaf has the same depth
" In a B-tree of a degree t all nodes except the
root node have between t and 2t children (i.e.,
between t31 and 2t31 keys).
" The root node has between 0 and 2t children
(i.e., between 0 and 2t31 keys)
Height of a B-tree
" B-tree T of height h, containing n ó 1 keys and minimum
degree t ó 2, the following restriction on the height holds:
n û1
h ó log t depth
#of
2 nodes
1
0 1
t-1 t-1 1 2
t t
BTreeCreate(T)
01 x ü AllocateNode();
02 leaf[x] ü TRUE;
03 n[x] ü 0;
04 DiskWrite(x);
05 root[T] ü x
Splitting Nodes
x x
... N W ... ... N S W ...
y = ci[x]
y = ci[x] z = ci+1[x]
P Q R S T V W
P Q R T V W
T1 ... T8
Splitting Nodes (2)
BTreeSplitChild(x,i,y)
z ü AllocateNode()
x: parent node
leaf[z] ü leaf[y]
n[z] ü t-1
y: node to be split and child of x
for j ü 1 to t-1 i: index in x
keyj[z] ü keyj+t[y] z: new node
if not leaf[y] then
for j ü 1 to t
cj[z] ü cj+t[y]
n[y] ü t-1 x
for j ü n[x]+1 downto i+1 ... N W ...
cj+1[x] ü cj[x]
ci+1[x] ü z y = ci[x]
for j ü n[x] downto i
keyj+1[x] ü keyj[x] P Q R S T V W
keyi[x] ü keyt[y]
n[x] ü n[x]+1
DiskWrite(y)
DiskWrite(z) T1 ... T8
DiskWrite(x)
Split: Running Time
" A local operation that does not traverse the
tree
" Q(t) CPU-time, since two loops run t times
" 3 I/Os
Inserting Keys
" Done recursively, by starting from the root and
recursively traversing down the tree to the
leaf level
" Before descending to a lower level in the tree,
make sure that the node contains < 2t 3 1
keys:
3 so that if we split a node in a lower level we will
have space to include a new key
Inserting Keys (2)
" Special case: root is full (BtreeInsert)
BTreeInsert(T)
r ü root[T]
if n[r] = 2t 3 1 then
s ü AllocateNode()
root[T] ü s
leaf[s] ü FALSE
n[s] ü 0
c1[s] ü r
BTreeSplitChild(s,1,r)
BTreeInsertNonFull(s,k)
else BTreeInsertNonFull(r,k)
Splitting the Root
" Splitting the root requires the creation of a
new root
root[T]
root[T] s
r
H
A D F H L N P
r
A D F L N P
T1 ... T8
A C D E J K N O R S T U V Y Z
B inserted
G M P X
A B C D E J K N O R S T U V Y Z
Q inserted
G M P T X
A B C D E J K N O Q R S U V Y Z
Insertion: Example (2)
L inserted P
G M T X
A B C D E J K L N O Q R S U V Y Z
F inserted P
C G M T X
A B D E F J K L N O Q R S U V Y Z
Insertion: Running Time
" Disk I/O: O(h), since only O(1) disk accesses
are performed during recursive calls of
BTreeInsertNonFull
" CPU: O(th) = O(t logtn)
" At any given time there are O(1) number of
disk pages in main memory
Deleting Keys
" Done recursively, by starting from the root and
recursively traversing down the tree to the leaf level
" Before descending to a lower level in the tree, make
sure that the node contains ó t keys (cf. insertion < 2t
3 1 keys)
" BtreeDelete distinguishes three different
stages/scenarios for deletion
3 Case 1: key k found in leaf node
3 Case 2: key k found in internal node
3 Case 3: key k suspected in lower level node
Deleting Keys (2)
initial tree P
C G M T X
A B D E F J K L N O Q R S U V Y Z
F deleted: P
case 1
C G M T X
A B D E J K L N O Q R S U V Y Z
" Case 1: Ifxthe key k is in node x, and x is a leaf, delete
k from x
Deleting Keys (3)
" Case 2: If the key k is in node x, and x is not a leaf,
delete k from x
3 a) If the child y that precedes k in node x has at least t
keys, then find the predecessor k9 of k in the sub-tree
rooted at y. Recursively delete k9, and replace k with k9 in x.
3 b) Symmetrically for successor node z
M deleted: P
case 2a
C G L x T X
A B D E J K N O Q R S U V Y Z
y
Deleting Keys (4)
" If both y and z have only t 31 keys, merge k with the
contents of z into y, so that x loses both k and the
pointers to z, and y now contains 2t 3 1 keys. Free z
and recursively delete k from y.
G deleted: P
case 2c
C L x-k T X
A B D E J K N O Q R S U V Y Z
y = y+k + z - k
Deleting Keys - Distribution
" Descending down the tree: if k not found in current
node x, find the sub-tree ci[x] that has to contain k.
" If ci[x] has only t 3 1 keys take action to ensure that
we descent to a node of size at least t.
" We can encounter two cases.
3 If ci[x] has only t-1 keys, but a sibling with at least t keys,
give ci[x] an extra key by moving a key from x to ci[x],
moving a key from ci[x]9s immediate left and right sibling up
into x, and moving the appropriate child from the sibling
into ci[x] - distribution
Deleting Keys 3 Distribution(2)
x ... k ... x ... k9 ...
A B A B
C L P T X
delete B
ci[x] A B E J K N O Q R S U V Y Z
sibling
B deleted: E L P T X
A C J K N O Q R S U V Y Z
Deleting Keys - Merging
" If ci[x] and both of ci[x]9s siblings have t 3 1
keys, merge ci with one sibling, which involves
moving a key from x down into the new
merged node to become the median key for
that node
A B A B
Deleting Keys 3 Merging (2)
P
A B D E J K N O Q R S U V Y Z
D deleted:
C L P T X
A B E J K N O Q R S U V Y Z
COL 106
Shweta Agrawal, Amit Kumar
0 5 10 25 35 45 55 0 5 25 35 45 55
10
B-tree 4-way tree
B-tree
1. It is perfectly balanced: every leaf node is at the same depth.
2. Every node, except maybe the root, is at least half-full
t-1f #keys f2t-1
3. Every internal node with k keys has k+1 non-null children
B-tree Height
Claim: any B-tree with n keys, height h and minimum degree t
satisfies:
n û1
h ó log t
2
Proof:
" The minimum number of KEYS for a tree with height h is
obtained when:
3 The root contains one key
3 All other nodes contain t-1 keys
B-Tree: Insert X
J
x & 56 98 &. x & 56 68 98 &.
split
y 60 65 68 83 86 90
y 60 65 z 83 86 90
Insert example
20 40 60 80 M ý 6; t ý 3
0 5 10 15 25 35 45 55 62 66 70 74 78 87 98
Insert 3:
20 40 60 80
0 3 5 10 15 25 35 45 55 62 66 70 74 78 87 98
20 40 60 80 M ý 6; t ý 3
0 3 5 10 15 25 35 45 55 62 66 70 74 78 87 98
Insert 61: 20 40 60 80
OVERFLOW
0 3 5 10 15 25 35 45 55 61 62 66 70 74 78 87 98
SPLIT IT
20 40 60 70 80
0 3 5 10 15 25 35 45 55 61 62 66 74 78 87 98
M ý 6; t ý 3
20 40 60 70 80
Insert 38:
0 3 5 10 15 25 35 45 55 61 62 66 74 78 87 98
20 40 60 70 80
0 3 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98
Insert 4: M ý 6; t ý 3
20 40 60 70 80
0 3 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98
20 40 60 70 80
OVERFLOW
0 3 4 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98
SPLIT IT
OVERFLOW
5 20 40 60 70 80
SPLIT IT
0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
M ý 6; t ý 3
OVERFLOW
5 20 40 60 70 80
SPLIT IT
0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
60
5 20 40 70 80
0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
Complexity Insert
" Inserting a key into a B-tree of height h is done in a
single pass down the tree and a single pass up the
tree
Recall: The root should have at least 1 value in it, and all other nodes should
have at least t-1 values in them
M ý 6; t ý 3
Underflow Example
Delete 87: 60
5 20 40 70 80
0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
60 B-tree
UNDERFLOW
5 20 40 70 80
0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 98
B-Tree: Delete X,k
" Delete as in M-way tree
" A problem:
3 might cause underflow: the number of keys remain in a
node < t-1
" Solution:
3 make sure a node that is visited has at least t instead of t-1
keys.
3 If it doesn9t have k
" (1) either take from sibling via a rotate, or
" (2) merge with the parent
3 If it does have k
" See next slides
Recall: The root should have at least 1 value in it, and all other nodes should
have at least t-1 (at most 2t-1) values in them
B-Tree-Delete(x,k)
1st case: k is in x and x is a leaf ð delete k
k=66
x 62 66 70 74 x 62 70 74
5 6 7 5 6 7
y 35 40 45 y 35 40 45
Example t=3
2nd case cont.:
c. Both a and b are not satisfied: y and z have t-1
keys
3 Merge the two children, y and z
3 Recursively delete k from the merged cell
x 30 70 90
x 30 50 70 90
y
35 40 55 60 z y 35 40 50 55 65
1 2 3 4 5 6 1 2 3 4 5 6
Example t=3
Questions
" When does the height of the tree shrink?
" Why do we need the number of keys to be at least t
and not t-1 when we proceed down in the tree?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Delete Complexity
" Basically downward pass:
3 Most of the keys are in the leaves 3 one
downward pass
3 When deleting a key in internal node 3 may
have to go one step up to replace the key with
its predecessor or successor
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Why B-Tree?
COL 106
Shweta Agrawal and Amit Kumar
Revisiting FindMin
" Application: Find the smallest ( or highest
priority) item quickly
3 Operating system needs to schedule jobs
according to priority instead of FIFO
3 Event simulation (bank customers arriving and
departing, ordered according to when the event
happened)
3 Find student with highest grade, employee with
highest salary etc.
2
Priority Queue ADT
" Priority Queue can efficiently do:
3 FindMin (and DeleteMin)
3 Insert
" What if we use&
3 Lists: If sorted, what is the run time for Insert and
FindMin? Unsorted?
3 Binary Search Trees: What is the run time for Insert and
FindMin?
3 Hash Tables: What is the run time for Insert and FindMin?
3
Less flexibility ø More speed
" Lists
3 If sorted: FindMin is O(1) but Insert is O(N)
3 If not sorted: Insert is O(1) but FindMin is O(N)
" Balanced Binary Search Trees (BSTs)
3 Insert is O(log N) and FindMin is O(log N)
" Hash Tables
3 Insert O(1) but no hope for FindMin
" BSTs look good but&
3 BSTs are efficient for all Finds, not just FindMin
3 We only need FindMin
4
Better than a speeding BST
5
Binary Heaps
" A binary heap is a binary tree (NOT a BST) that is:
3 Complete: the tree is completely filled except possibly the
bottom level, which is filled from left to right
3 Satisfies the heap order property
" every node is less than or equal to its children
" or every node is greater than or equal to its children
" The root node is always the smallest node
3 or the largest, depending on the heap order
6
Heap order property
" A heap provides limited ordering information
" Each path is sorted, but the subtrees are not sorted
relative to each other
3 A binary heap is NOT a binary search tree
-1
2 1
0 1
4 6 2 6
0
7 5 8 4 7
These are all valid binary heaps (minimum)
7
Binary Heap vs Binary Search Tree
Binary Heap Binary Search Tree
min value
5 94
10 94 10 97
min
value
97 24 5 24
9
Examples
6 2
4 2 4 6
complete tree,
heap order is "max"
5
not complete
2 2
4 6 5 6
7 5 7 4
complete tree, complete tree, but min
heap order is "min" heap order is broken
10
Array Implementation of Heaps
(Implicit Pointers)
" Root node = A[1]
" Children of A[i] = A[2i], A[2i + 1]
" Parent of A[j] = A[j/2]
" Keep track of current size N (number of nodes)
1
2
2 3
value - 2 4 6 7 5 4 6
index 0 1 2 3 4 5 6 7 7 5
4 5
N=5
11
FindMin and DeleteMin
3 Run time = ? 4 3
7 5 8 10
" DeleteMin: 11 9 6 14
12
DeleteMin
11 9 6 14
13
Maintain the Structure Property
11 9 6 14
14
Maintain the Heap Property
15
DeleteMin: Percolate Down
? 14
14 3
3 ?
4 3 4 4 8
7 5 8 10 7 5 8 10 7 5 14 10
11 9 6 11 9 6 11 9 6
PercDown(i:integer, x: integer): {
// N is the number elements, i is the hole,
x is the value to insert
Case{
no children
2i > N : A[i] := x; //at bottom//
2i = N : if A[2i] < x then
one child
at the end
A[i] := A[2i]; A[2i] := x;
else A[i] := x;
2i < N : if A[2i] < A[2i+1] then j := 2i;
2 children else j := 2i+1;
if A[j] < x then
A[i] := A[j]; PercDown(j,x);
else A[i] := x;
}}
18
Insert
11 9 6
19
Maintain the Structure Property
3
4 8
7 5 14 10
11 9 6
2
21
Insert: Percolate Up
2 ?
3 3 3
4 8 4 8 8
7 5 14 10 7 14 10 7 4 14 10
? ?
11 9 6 11 9 6 5 11 9 6 5
2 2
" Start at last node and keep comparing with parent A[i/2]
" If parent larger, copy parent down and go up one level
" Done if parent f item or reached top node A[1]
22
Insert: Done
2
3 8
7 4 14 10
11 9 6 5
23
Binary Heap Analysis
1
N=11
11 11
2 3
5 10 5 10
4 5 6 7
9 4 8 12 9 3 8 12
8
2 7 6 3 11 2 7 6 4
9 10
11 11
5 10 5 8
2 3 8 9 2 3 10 12
9 7 6 4 9 7 6 4
11 2
2 8 3 8
5 3 10 12 5 4 10 12
9 7 6 4 9 7 6 11
Total Time
= O(n)
" Runtime?
Best-case ___ Worst-case ___ Average-case ___
" Stable? _____
" In-place? _____
CSE373: Data Structures &
36
Algorithms
Heap Sort
" Idea: buildHeap then call deleteMin n times
E[] input = buildHeap(...);
E[] output = new E[n];
for (int i = 0; i < n; i++) {
output[i] = deleteMin(input);
}
" Runtime?
Best-case, Worst-case, and Average-case: O(n log(n))
" Stable? No
" In-place? No. But it could be, with a slight trick...
CSE373: Data Structures &
37
Algorithms
But this reverse sorts 3
In-place Heap Sort how would you fix that?
4 7 5 9 8 6 10 3 2 1
5 7 6 9 8 10 4 3 2 1
arr[n-i]=
deleteMin() heap part sorted part
CSE373: Data Structures &
38
Algorithms
<AVL sort=? <Hash sort=?
AVL Tree: sure, we can also use an AVL tree to:
3 insert each element: total time O(n log n)
3 Repeatedly deleteMin: total time O(n log n)
" Better: in-order traversal O(n), but still O(n log n)
overall
3 But this cannot be done in-place and has worse
constant factors than heap sort
3
Nearest Neighbor Search
Problem deonition
" Given: a set P of n points in Rd
Over some metric
" ond the nearest neighbor p of q in P
Q?
Distance metric
Nearest Neighbor Query in High
Dimensions
" Very important and practical problem!
3 Image retrieval
find N closest
matches (i.e., N
nearest neighbors)
(f1,f2, .., fk)
5
Nearest Neighbor Query in High
Dimensions
3 Face recognition
6
Nearest Neighbor(s) Query
3 What is the closest restaurant to my hotel?
7
Other Applications
" Classiocation " Copyright violation
" Clustering detection
" Indexing
Weight
q?
color
We will see three solutions
(or as many as time permits)&
" Quad Trees
" K-D Trees
" Locality Sensitive Hashing
Interpreting Queries
Geometrically
" Multi-dimensional keys can be thought
as <points= in high dimensional spaces.
10
Example 1- Range Search in
2D
11
Example 2 3 Range Search in
3D
12
1D Range Search
Range: [x, x9]
13
1D Range Search
" Example: retrieve all points in [25, 90]
3 The search path for 25 is:
14
1D Range Search
3 The search for 90 is:
15
1D Range Search
" Examine the leaves in the sub-trees between
the two traversing paths from the root.
split node
j k f g l d a b
Quadtree 3 Exact Match Query
E(25,25) C
•
SW
X1,Y1
NE
NW SE
X2,Y2
Y
X
Quadtree 3 Nearest Neighbor Query
SW X1,Y1 NE
NW SE
X2,Y2
Y NW
X
Quadtree3 Nearest Neighbor Query
SW X1,Y1 NE
NW SE
X2,Y2
SW
Y NW SE NE
X
Quadtree3 Nearest Neighbor Search
Algorithm
Initialize range search with large r
Put the root on a stack
Repeat
3 Pop the next node T from the stack q
3 For each child C of T
" if C intersects with a circle (ball) of radius
r around q, add C to the stack
" if C is a leaf, examine point(s) in C and
update r
27, 28 65, 51
Pleft Pright
KD Tree (cont9d)
As we move down the tree, we divide the space along
alternating (but not always) axis-aligned hyperplanes:
x
KD Tree - Example
Split by y-coordinate: split by a horizontal line that
has half the points below or on and half above.
y y
KD Tree - Example
Split by x-coordinate: split by a vertical line that
has half the points left or on, and half right.
y y
x
x x x
KD Tree - Example
Split by y-coordinate: split by a horizontal line that
has half the points below or on and half above.
y y
x
x x x
y y
Node Structure
" A KD-tree node has 5 oelds
3 Splitting axis
3 Splitting value
3 Data
3 Left pointer
3 Right pointer
Splitting Strategies
" Divide based on order of point insertion
3 Assumes that points are given one at a time.
72
Dimensionality Reduction
Idea: Find a mapping T to reduce the
dimensionality of the data.
Drawback: May not be able to ond all similar
objects (i.e., distance relationships might not be
preserved)
73
Locality Sensitive Hashing
" Hash the high dimensional points down to a
smaller space
0 101
1 Input vector
0
1
1
Fergus et al 0
How to search from hash table?
N
Hash function
h r1&rk
Search the hash table
Xi for a small set of images
<< N
Hash table
11010 Q
1
h r &r
1 k Q
11011
1
11110
1
New query
Lecture 26
Keerti Choudhary
Department of Computer Science, IIT Delhi
TodayÕs Lecture
Some Resources:(
(
234 Trees: https://people.ksp.sk/~kuko/gnarley-trees/234tree.html (
(
Trees: https://people.ksp.sk/~kuko/gnarley-trees/Intro.html(
234 Trees
Property 1: Every node is of following type:
" 2-node : has one data element, and zero or two children
" 3-node : has two data elements, and zero or three children
" 4-node : has three data elements, and zero or four children
a a b a b c
N1 N2 N1 N2 N3 N1 N2 N3 N4
234 Trees
Property 1: Every node is of following type:
" 2-node : has one data element, and zero or two children
" 3-node : has two data elements, and zero or three children
" 4-node : has three data elements, and zero or four children
a a b a b c
N1 N2 N1 N2 N3 N1 N2 N3 N4
30 50
5 10 15 40 55 65
4 8 11 12 20 35 45 55 60 70
Trees with a Single Overflow
DeÞnition: Contains exactly one 5-node.
30 50
5-node
5 10 15 25 40 55 65
4 8 11 12 20 28 35 45 55 60 70
Why study Overflow?
Reason : We can get a 5-Node after insertions
30 50
5 10 15 40 55 65
4 8 11 12 20 35 45 55 60 70
20 22 25 28
5-Node obtained(
after insertion of 22, 25, 28
Handling Insertion
Algorithm:(
(
Suppose a key k needs to be inserted
1. The Þrst step is to Þnd the leaf node, say x, that can accommodate key k(
without breaking the sorted ordering. This takes O(log n) time.
2. After inserting key k, the node x transitions from either 2-node to 3-node, (
or 3-node to 4-node, or 4-node to 5-node.
(
Question: How to Þx trees with a Single Overßow?
(
Handling Overflow
x b x
Tx Tx
5-node
a b c d a c d
Ta T[a,b] T[b,c] T[c,d] Td Ta T[a,b] T[b,c] T[c,d] Td
x y x b y
5-node
a b c d a c d
Ta T[a,b] T[b,c] T[c,d] Td Ta T[a,b] T[b,c] T[c,d] Td
x y z x b y z
5-node
a b c d a c d
Ta T[a,b] T[b,c] T[c,d] Td Ta T[a,b] T[b,c] T[c,d] Td
Furthermore, in the process if the overßow reaches the root then the height of the tree
increases by one.
Trees with a Single Underflow
DeÞnition: Contains exactly one Ò1-nodeÓ
30 50
5 10 15 1-node 55 65
4 8 11 12 20 40 55 60 70
Why study Underflow?
Reason : We can get a 1-Node after deletions
origÑ30
35 50
5 10 15 40 55 65
4 8 11 12 20 35 45 55 60 70
1- Node obtained
after deletion of 30
Algorithm:
1. Suppose a key k needs to be deleted. We Þrst search k. If k is not a leaf then we go to its (
successor node (say x) and swap.
b c b
a × d a c d
Ta T[a,b] T[b,c] T[c,d] Td Ta T[a,b] T[b,c] T[c,d] Td
Case 1 : All Siblings are 2-Nodes, and the parent is either a 3-Node or 4-Node.
Handling Underflow
a c b d
× b d e a c e
Ta T[a,b] T[b,c] T[c,d] T[d,e] Te Ta T[a,b] T[b,c] T[c,d] T[d,e] Te
a ×
× b a b
Furthermore, in the process if the underßow reaches the root then the height of the tree
decreases by one.