Sei sulla pagina 1di 8

CS2210 Data Structures and Algorithms Lecture 12:

Multiway and (2,4) Trees


9 2 5 7 10 14

Review: Binary Search Tree


<
2 1 6 9 4 = 8

>

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)

2004 Goodrich, Tamassia

Outline
A new type of search trees, Multiway Seach Tree
for ordered dictionary ADT

Multi-Way Search Tree


11 2 6 8 15 24 27 30 32

Searching in multiway trees (2,4) trees: special case of multiway trees


Height properties Insertion Deletion

Multi-way search tree is not binary (more than two children are allowed) Each node can store more than one ordered entries 2 6

less keys t equ han o r al t o2

keys between 2 and 6

keys between 6 and 8

ys an k e r th o 8 e lt gg a bi equ or

Multi-Way Search Tree


11 2 6 8 15 24 27 30 32

Multi-Way Inorder Traversal


We can extend the notion of inorder traversal from binary trees to multi-way search trees Namely, we visit item (ki, oi) of node v between the recursive traversals of the subtrees of v rooted at children vi and vi + 1 An inorder traversal of a multi-way search tree visits the keys in increasing order 11
8

A multi-way search tree is an ordered tree such that


Each internal node has at least two children and stores d 1 key-value items (ki, oi), where d is the number of children For a node with children v1 v2 vd storing keys k1 k2 kd1
keys in the subtree of v1 are less than k1 keys in the subtree of vi are between ki1 and k i (i = 2, , d 1) keys in the subtree of vd are greater than kd1 1

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

The leaves store no items and serve as placeholders

Multi-Way Search Tree


For node with children v1 v2 vd storing keys k1 k2 kd1
keys in the subtree of v1 are less than k1 keys in the subtree of vi are between ki1 and ki (i = 2, , d 1) keys in the subtree of vd are greater than kd1

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

Multi-Way Searching: Another Example

(2,4) Trees
10 15 24

11 2 6 8 15

24 27 30 32 2 8

12

18

27

32

Search for key 7


Search terminates at a leaf child, which implies that there is no entry with key 7 in the tree

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

Height of a (2,4) Tree


Theorem: A (2,4) tree storing n entries has height O(log n) Proof:
Let h be the height of a (2,4) tree with n entries Since each internal node holds at least one entry, n number of internal nodes By depth property, no external nodes at depths i = 0, , h 1 By multiway tree property, each internal node has at least 2 children, there are at least 2i entries at depth i = 0, , h 1 and no items at depth h, we have n number of internal nodes 1 + 2 + 4 + + 2h1 = 2h 1 Thus, h log (n + 1), and therefore h is O(log n) depth 0 1 h1 h
10

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

Other Operations in a (2,4) Tree


Thus searching in a (2,4) tree with n items takes O(log n) time Now we have to show how to insert and remove entries in a (2,4) tree while preserving the node-size and the depth properties Insertion into (2,4) tree is easier than into AVL tree Removal is slightly more complicated than the insertion
13

Insertion
How to find the correct node v to insert?
i.e. inserting in v preserves the multiway tree search order

There are 2 cases:


Case 1: The key k is not in the tree. Then v is the parent of the leaf reached by searching for k Example: insert 30 10 15 24

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

But it may violate the nodes size property


Overflow occurs when a 4-node becomes 5-node (illegal in (2,4)-tree)
10 15 24 10 15 24 2 8 12 18

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

Insertion: Overflow and Split


u
15 24

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

We handle an overflow at a 5-node v with a split operation:


let v1 v5 be the children of v and k1 k4 be the keys of v node v is replaced nodes v' and v"
v' is a 3-node with keys k1 k2 and children v1 v2 v3 v" is a 2-node with key k4 and children v4 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

Let T be a (2,4) tree with n items


Tree T has O(log n) height Step 1 takes O(log n) time because we visit O(log n) nodes Step 2 takes O(1) time Step 3 takes O(log n) time because each split takes O(1) time and we may perform up to O(log n) splits

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

Thus, an insertion in a (2,4) tree takes O(log n) time


20

The overflow may propagate to the parent node u

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

Step 2: Underflow and Fusion


Case 2: all adjacent siblings of v are 2-nodes
Cannot do transfer in this case, will do fusion Fusion operation: we merge v with an adjacent sibling w and move an entry from u (the entry between w and v) to the merged node v' After a fusion, the underflow may propagate to the parent u If underflow propagates all the way to the root, we simply delete the root
in which case, height of the tree decreases by 1

Example: to delete key 24, we replace it with 27 (inorder successor)


10 15 24 2 8 12 18 27 32 35

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

Step 2: Underflow and Transfer


Now assume that entry to be deleted is at node v with leaf children Deletion from v can cause an underflow (if v becomes a 1-node) To handle an underflow at node v with parent u, we consider two cases (note that underflow node v has no entries) Case 1: an adjacent sibling w of v is a 3-node or a 4-node
Transfer operation: 1. we move a child of w to v 2. we move an entry from u (the entry between w and v) to v 3. we move an entry from w (the entry with key closest to the deleted key in u at step 2) to replace the missing entry of u After a transfer, no new underflow occurs

Another Fusion Example


9 delete 10 2 10

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) O(log n) O(1)

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

Analysis of Deletion (repeated)


Let T be a (2,4) tree with n items
Tree T has O(log n) height

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

Thus, deleting an item from a (2,4) tree takes O(log n) time


28

Implementing a Dictionary
Comparison of efficient dictionary implementations Search Hash Table AVL Tree (2,4) Tree 1
expected

Another Insertion Example


9 15 24

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

AVL vs (2,4) Trees


Advantages of (2,4) trees over AVL trees
Easier to understand In general, fewer nodes (smaller height)

Another Deletion Example


22 9 14 10 12 20 24 25 29 10

delete 25
9 14 12

22 29 20 24
rflow unde

Advantages of AVL trees over (2,4) trees


More efficient (by a constant factor, both trees have O(log n) basic operations complexity) Easier to impelement ( I think ) Only 1 type of nodes to maintain, as opposed to 2,3,4 nodes of (2,4) tree
fusion
22
underflow

transfer
9

14

9 14 10 12 20 24 29 10

22
12 20 24 29

30

Potrebbero piacerti anche