Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
>
Question: can we generalize binary search trees to hold more than 1 entry per node? Answer: yes, with multiway search tree Why should we care?
New type of search tree Smaller height (but not necessarily more efficient)
Outline
A new type of search trees, Multiway Seach Tree
for ordered dictionary ADT
Multi-way search tree is not binary (more than two children are allowed) Each node can store more than one ordered entries 2 6
ys an k e r th o 8 e lt gg a bi equ or
24
12
2 6 8
2 3 4 5 6 7 9
15
10 11 13
27
14
32
18
30
16 15
5
19 17
7
Multi-Way Searching
Similar to search in a binary search tree A each internal node with children v1 v2 vd and keys k1 k2 kd1
k = ki (i = 1, , d 1): the search terminates successfully k < k1: we continue the search in child v1 ki1 < k < ki (i = 2, , d 1): we continue the search in child vi k > kd1: we continue the search in child vd
k1
2
k2
6
k3
8
v1
less t equ han o r al t o2
v2
v3
v4
an 8 th er l to g g ua bi e q or
Reaching an external node terminates the search unsuccessfully Assuming d is a constant independent of number of nodes, examining each node during search takes O(1) time, thus time to search is proportional to the multiway tree height Example: search for 30 11 24 2 6 8 15 27 30 30
6
32
between 2 and 6
between 6 and 8
(2,4) Trees
10 15 24
11 2 6 8 15
24 27 30 32 2 8
12
18
27
32
Internal node can have 2, 3, or 4 children Depending on the number of children, an internal node of a (2,4) tree is called a 2-node, 3-node or 4node
11
(2,4) Trees
A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way search with the following properties
Node-Size Property: every internal node has at most four children Depth Property: all the external nodes have the same depth
Recall that in a multiway tree, the minimum number of children for an internal node is 2. Thus an internal node can have 2, 3, or 4 children The depth property together with the requirement that every internal node has at least 2 children, will guarantee logarithmic height (in the number of entries) for the tree, as we shall see later
# of entries 1 2 2h1 0
Insertion
How to find the correct node v to insert?
i.e. inserting in v preserves the multiway tree search order
2 8
12
18
27 32 35
10 15 24 2 8 12 18
v
27 30 32 35
Insertion
Suppose we need to insert a new entry (k, o) in the (2,4)-tree Always insert at a node only with leaf children (a node at level h1, where h is the maximum depth of the tree) Suppose we know the correct node v to insert in. put (k, o) in the correct place (keys have to stay sorted at node v) since the number of entries increased, to for the tree to stay a multiway tree, we must add 1 more leaf child to v (in the correct place, after the inserted entry) v
27 32 35
Insertion
Case 2: The key k is already in the tree. Solved similar to the binary search trees
Perform search. When reach node v containing entry with key k, continue search in the subtree to the left of v (could, equivalently go in the right subtree of v). Stop when reached the node with only leaf children. Example: insert 15
10 15 24 10 15 24 2 8 12
v insert 30
27 30 32 35
18
27 32 35
v
27 32 35
v insert 40
27 32 35 40 2 8
10 15 24 10 15 24 12 12 15
18
27 32 35
Insertion
The procedure on the previous slides preserves:
Multiway search tree order Depth property (all external nodes have the same depth)
4
Insertion Example
4 6
overflow
4 6 12 4 6 12 15
overflow
12 4 6 15 12 12 15 13 4 6 15
v
27 32 35
1 4 6
10 15 24 2 8 12 18
4 12
4 12 15 1 3 6 8 15 1 3
4 12 6 8 13 15
v overflow
27 30 32 35
1 3
Analysis of Insertion
v"
35
u
15 24 32
v
12 18 27 30 32 35 12 18
v'
27 30
v1 v2 v3 v4 v5 overflow
v1 v2 v3 v4 split
v5
Algorithm insert(k, o) 1. v = search(root, k) while (! v has only external children) v = search(v.childToTheLeft(k)) 2. We add the new entry (k, o) at node v 3. while overflow(v) if isRoot(v) create a new empty root above v v split(v)
search(node,k) returns the node which has entry with key k or, if key k is not in tree, the parent of the leaf reached when searching for k v.childToTheLeft(k)) returns the child immediately to the left of key k split(v) performs the split on node v and returns the parent of node v
key k3 is inserted into the parent u of v (a new root may be created) If v was child i of u, v and v become children i and i +1 of u, respectively
Deletion: Step 1
Like in AVL trees, first ensure that entry that needs to be deleted is at the node with leaf children If an entry is an internal node with no leaf children
replace the entry with its inorder successor (or, equivalently, with its inorder predecessor) and delete the latter entry and one leaf
u
10 15 27 2 8 12 18 32 35
9 14
u w v
2 5 7
9 14
v'
10 14
2 5 7
10
21
u w
2
v
underflow
u v
2 9
u
4 9
u
4 9 8
underflow 2 9
w
2 6 8
v
2 6 8
w
9
Comments on Deletions
Either case 1 or case 2 must always hold
Let v be the node from which we deleted an entry No underflow before deletion in the tree Thus parent of v must be at least 2-node(but could be also a 3 node or a 4 node) Thus node at which deletion occurs must have at least 1 sibling which is at least a 2-node (but could be also a 3 node or a 4 node) u w v
Deletion
Algorithm delete(k) search for key k to locate the deletion node v if v is not internal node with leaf children then swap(entry at v, entry at inorder successor of (v,k)) v = position of inorder successor of (v,k) Delete the entry with key k from v while underflow(v) if isRoot(v) = true make (the only) child of v the new root else if there is a 3-4 node immediate sibling s transfer(v,s) else s = immediate sibling of v v =fusion(v,s)
O(log n)
Inorder successor(v,k) is the smallest entry in the subtree to the right of key k in node v Thus, deleting an item from a (2,4) tree takes O(log n) time 27 Assume fusion(v,s) returns the parent of v
Deletion Example
9 14 5 7 10 15 delete 9 5 7 10 14 underflow! 15
In a deletion operation
transfer 5 7 14 10 15 delete 5 7 14 underflow! 10 15
We visit O(log n) nodes to locate the node from which to delete the entry We handle an underflow with a series of O(log n) fusions, followed by at most one transfer Each fusion and transfer takes O(1) time
fusion 7 10
14 15
26
Implementing a Dictionary
Comparison of efficient dictionary implementations Search Hash Table AVL Tree (2,4) Tree 1
expected
insert 30
32 35 5 12
9 15 24
split
5 12 18 27 18 27 30 32 35
Insert 1
expected
Delete 1
expected
Notes
no ordered dictionary methods simple to implement complex to implement
split
9 15 24 32 24 9 15 5 12 18 27 30 35 5 12 18 27 30 35 32
log n
worst-case
log n
worst-case
log n
worst-case
log n
worst-case
log n
worst-case
log n
worst-case
complex to implement
29
delete 25
9 14 12
22 29 20 24
rflow unde
transfer
9
14
9 14 10 12 20 24 29 10
22
12 20 24 29
30