Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Every node x has the following fields: a. n[x], the number of eys !urrently stored in node x, b. the n[x] eys themselves, stored in non de!reasing order, so that key"[x] # key1[x] # $$$ # keyn[x]%1[x], !. leaf [x], a boolean value that is &'(E if x is a leaf and )A*+E if x is an internal node. ,. Ea!h internal node x also !ontains n[x]- 1 pointers c"[x], c1[x], ..., cn[x][x] to its !hildren. *eaf nodes have no !hildren, so their ci fields are undefined. .. &he eys keyi[x] separate the ranges of eys stored in ea!h subtree: if ki is any ey stored in the subtree with root ci [x], then k" # key"[x] # k1 # key1[x] #$$$ # keyn[x]%1[x] # kn[x]. /. All leaves have the same depth, whi!h is the tree0s height h. 1. &here are lower and upper bounds on the number of eys a node !an !ontain. &hese bounds !an be e2pressed in terms of a fi2ed integer t 3 , !alled the minimum degree of the 4%tree: a. Every node other than the root must have at least t % 1 eys. Every internal node other than the root thus has at least t !hildren. 5f the tree is nonempty, the root must have at least one ey. b. Every node !an !ontain at most ,t % 1 eys. &herefore, an internal node !an have at most ,t !hildren. 6e say that a node is full if it !ontains e2a!tly ,t % 1 eys.
4%&ree%+ear!h(2, )
i <- 0 while i < n[x] and k > keyi[x] do i <- i + 1 if i < n[x] and k = keyi[x] then return (x, i) if leaf[x] then return NI el!e "i!k-#ead($i[x]) return %-&ree-'ear$h($i[x], k)
4%&ree%8reate(&)
x <- (llo$ate-Node() leaf[x] <- &#)* n[x] <- 0 "i!k-+rite(x) root[&] <- x
4%&ree%+plit%8hild(2, i, y)
, <- (llo$ate-Node() leaf[,] <- leaf[y] n[,] <- t - 1 for - <- 0 to t - . do key-[,] <- key-+t[y] if not leaf[y] then for - <- 0 to t-1 do $-[,] <- $-+t[y] n[y] <- t - 1 for - <- n[x] downto i + 1 do $-+1[x] <- $-[x] $i+1 <- , for - <- n[x] downto i do key-+1[x] <- key-[x] keyi[x] <- keyt-1[y] n[x] <- n[x] + 1 "i!k-+rite(y) "i!k-+rite(,) "i!k-+rite(x)
4%&ree%5nsert(&, )
r <- root[&] if n[r] = .t - 1 then ! <- (llo$ate-Node() root[&] <- ! leaf[!] <- /( '* n[!] <- 0 $1 <- r %-&ree-'0lit-1hild(!, 1, r) %-&ree-In!ert-Nonfull(!, k) el!e %-&ree-In!ert-Nonfull(r, k)
4%&ree%5nsert%:onfull(2, )
i <- n[x]-1 if leaf[x] then while i >= 0 and k < keyi[x] do keyi+1[x] <- keyi[x] i <- i - 1 keyi+1[x] <- k n[x] <- n[x] + 1 "i!k-+rite(x) el!e while i >= 0 and k < keyi[x] do i <- i - 1 i <- i + 1 "i!k-#ead($i[x]) if n[$i[x]] = .t - 1 then %-&ree-'0lit-1hild(x, i, $i[x]) if k > keyi[x] then i <- i + 1 %-&ree-In!ert-Nonfull($i[x], k)
1. +how the results of inserting the eys: F, S, Q, K, C, L, H, T, V, W, M, R, N in order into an empty 4%tree with minimum degree ,.
Solution:
6e insert the first . letters by order: ),;,+ &hen, we need to split the root and insert < and 8: ; 8,),< +
=,<,* +,&,>
=,<,* +
>,6
&
=,<,* +
>,6
),<
&
= *,@,:
',+
>,6
,. +how all legal 4%trees of minimum degree , that represent A1, ,, ., /, 1B.
solution:
we now that every node % e2!ept the root % must have at list t%1 eys, that
and at most ,t%1 eys. Also remember that the leaves stay in the same depth:
1.
, 1 .,/,1
,.
. 1,, /,1
..
/ 1,,,. 1
/.
, 1 .
/ 1
Solution:
6e get a 4%tree nodes with no eys.
/. E2plain how to find the ma2imum ey stored in a 4%tree and how to find the prede!essor of a given ey stored in a 4%tree. Solution: )inding the ma2imum in a 4%tree is ?uite similar to finding a ma2imum in a binary sear!h tree E we need to find the right most leaf for the given root, and return the last ey.
)ind%@a2imum(2)
if leaf[2] retrun ( eyn[2]%1[2]) else return )ind%@a2imum(8n[2] [2])
8omple2ity: in the height of tree 9(logtn). )inding the prede!essor of eyi[2] (given 2 and i ) is a!!ording to the following rools: 1. if the 2 is not a leaf return the ma2imum of 8i[2] (every ey apply keyi%1[x] # k # keyi[x] ,if keyi%1[x] e2ist) ,. otherwise (it is a leaf) if iG" we return eyi%1[2]. . if the above !onditions are not satisfied, we loo for the first prede!essor node y, that its pointer 8i[y] is also the prede!essor of 2 and iG" we return eyi%1[y], if su!h node y is not e2ist, nil is returned. :ote: we add another field p for ea!h node that points to it0s father. in this subtree
Hrede!essor(2,i )
FF we want the prede!essor to of eyi[2] if not leaf[2] return )ind%@a2imum(8i[2]) else if iG" return eyi%1[2] else found <- false while (not found) if p[2] C null return null FF &here e2ist no prede!essor else y <- 0[x] i <- " while 8i[y] I 2 i <- i+1 if iG" return eyi%1[y] else 2 <- y
8omple2ity: we might sear!h within a node only on!e (when iI" and we return the ey) therefore the time !omple2ity is 9(t-logtn).