Vous êtes sur la page 1sur 2

Massachusetts Institute of Technology

6.042J/18.062J, Fall 17: Mathematics for Computer Science October 11


Prof. Albert R Meyer, Dr. Zachary Abel revised Monday 9th October, 2017, 21:42

In-Class Problems Week 6, Wed.

Problem 1.
For T 2 BBTr, define

leaves.T / WWD fS 2 Subtrs.T / j S 2 Leavesg


internal.T / WWD fS 2 Subtrs.T / j S 2 Branchingg:

(a) Explain why it follows immediately from the definitions that if T 2 Branching,

internal.T / D fT g [ internal.left.T // [ internal.right.T //; (trnlT)


leaves.T / D leaves.left.T // [ leaves.right.T //: (lvT)

(b) Prove by structural induction on the definition of RecTr that in a recursive tree, there is always one
more leaf than there are internal subtrees:
Lemma. If T 2 RecTr, then
jleaves.T /j D 1 C jinternal.T /j: (lf-vs-in)

Problem 2.

Definition. Define the sharing binary trees SharTr recursively:


Base case: (T 2 Leaves). T 2 SharTr.
Constructor case: (T 2 Branching). If left.T /; right.T / 2 SharTr, then T is in SharTr.
(a) Prove size .T / is finite for every T 2 SharTr.

(b) Give an example of a finite T 2 BBTr that has an infinite path.

(c) Prove that for all T 2 BBTr

T 2 SharTr ! T has no infinite path:

(d) Give an example of a tree T3 2 BBTr with three branching subtrees and one leaf.

(e) Prove that


Lemma. If T 2 SharTr, then
jleaves.T /j  1 C jinternal.T /j:

Hint: Show that for every T 2 SharTr, there is a recursive tree R 2 RecTr with the same number of internal
subtrees and at least as many leaves.

2017, Albert R Meyer. This work is available under the terms of the Creative Commons Attribution-ShareAlike 3.0
license.
2 In-Class Problems Week 6, Wed.

Problem 3.
This problem will show that if T is a search tree, and U is a search tree of the same shape as T for the same
values as T after one deletion and one insertion, then T and U have no subtrees in common. So if we tried
to maintain the shape of a search tree, then after just one deletion and one insertion we might have to find
size .T / new subtrees. This contrasts dramatically with the case of AVL trees, where by allowing the shape
of U to differ somewhat from the shape of T , the search trees T and U can share all but proportional to
log2 .size .T // subtrees.

The isomorphism relation between recursive trees T; U 2 RecTr is defined recursively


Base case: (T 2 Leaves). T is isomorphic to U iff U is a leaf.
Constructor case: (T 2 Branching). T is isomorphic to U iff

U 2 Branching AND left.U / isomorphic to left.T / AND right.U / isomorphic to right.T /:

(a) Prove that if T and U are isomorphic, then size .T / D size .U /.

We now assume there is a fixed numerical tree labelling num W RecTr ! R.


(b) Show that if T and U are isomorphic search trees for the same set of values, that is, nums.T / D
nums.U /, then num.T / D num.U /.
Hint: By definition of isomorphism and search tree. No induction needed.

Let T 2 RecTr be a search tree whose numerical labels are integers in some interval

nums.T / D (k...k C size .T / ]

where
(k..n] WWD fk C 1; : : : ; n 1; ng:
A shift of T is another search tree U isomorphic to T whose labels are one larger, that is

nums.U / D (.k C 1/....k C 1/ C size .T //]:

(c) Show that if U is a shift of T , then num.U / D num.T / C 1 and left.U / is a shift of left.T /.
Hint: By definition of search tree, isomorphism and shift. No induction is needed.

Well say that R; S 2 RecTr clash when only one of them is a leaf, or else both are branching and

num.R/ num.S /; or
num.left.R// num.left.S //; or
num.right.R// num.right.S //:

If R and S clash, then of course R S.


(d) Prove that every subtree of T clashes with every subtree of a shift of T .
Hint: Suppose U is a shift of T , and R is a subtree of T and S is a subtree of U . Show that R and S clash
by cases:
Case 1: R 2 left.T / and S 2 left.U /.
Case 2: R 2 left.T / and S 2 right.U /.
Case 3: R D T and S 2 left.U /.

Vous aimerez peut-être aussi