Answers

Advanced Data Structures
Colloquim (A)
January 18, 2017
Answers
1. (a) Node* minimum(Node* r) {
if (r == null) return r;
while (r->left!=null) r=r->left;
return r;
}
Node* successor(Node* r) {
if(r==null) return r;
if(r->right!=null) return minimum(r->right);
Node* y=x->p;
while((y!=null)&&(x==y->right)) {
x=y;
y=y->p;
}
return y;
}
(b) The tree produced by inserting a node with key 17 in T is
30
3
75
2
20
15
(c) The tree produced by removing node with key 3 from T is
30
15
75
2
20
1
2. (Red-black trees)
(a) The reasons why the depicted tree is not a balanced red-black tree are: (1) it is not
a binary search tree: the node with key 23 occurs to the right of the node with key
24; (2) the root is red; (3) it has a red leaf; (4) the red node with key 23 is a child
of the red node with key 25; (5) the is a branch with two black nodes from the root
to a leaf node, and another branch with 1 black node from the root to a leaf node.
(b) The following is an example of a balanced red-black tree with black height 3 and 3
red nodes:
40:B
20:R
15:B
8:R
7:B
60:R
25:B
17:B
23:B
50:B
33:B
45:B
66:B
55:B 65:B
67:B
9:B
The leaf nodes Nil have not been depicted; all of them are assumed to be black (B).
3. (a) A binomial tree is an ordered tree with a structure Bn from the set {Bk mod k ≥ 0}
defined recursively as follows: (1) B0 consists of a single node; and (2) when k > 0,
Bk consists of two binomial trees Bk−1 such that one of them has the other one as
left child of its root.
Also, the keys must be heap-ordered: the key of any node is greater than or equal
to the key of its parent.
(b1) The result of merging H1 with H2 is a heap H with the structure
100
H.head
5
25
24
37
27
42
18
39
(b2) The result of removing the node with key 15 from H1 is a heap H 0 with the
structure
H 0 .head
19
100
25
36
41
(b3) O(m + n).
4. A possible implementation of this method is:
2
39
double minDist(vector<Point> v) {
double d = 0;
int n = v.size();
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++) {
double dx=v[i].x-v[j].x, dy=v[i].y-v[j].y;
double dij = sqrt(dx*dx+dy*dy);
if (dij<d) d=dij;
}
return d;
}
The runtime complexity of this method is O(n2 ).
5. (a) A keyword tree is a tree K associated with a set P of strings, called patterns, which
satisfies 3 conditions:
1) every edge is labeled with exactly 1 character.
2) distinct edges which leave from a node are labeled with distinct characters.
3) every pattern Pi ∈ P gets mapped to a unique node v of K as follows: the string
of characters along the branch from root to node v is Pi , and every leaf node of
K is the mapping of a pattern from P.
It can be used to find fast all occurrences of the patterns from P in a text.
(b)
a
t
a
r
r
t
s
m
t
a
3
a
r
c
1
2
The failure links which have not been drawn go to the root of the suffix tree.
3