Comp2123 Week4
Comp2123 Week4
Q1.
T0 = x.left
T1 = x.right
T2 = y.right
T3 = z.right
y.right = z
y.parent = z.parent
if z.parent.left == z:
z.parent.left = y
else:
z.parent.right = y
z.left = T2
T2.parent = z
z.parent = y
Q2.
for n=1
height(n) = 1, which is >= log2(1)=0, so the base case is true
prove true for n=k+1 (weak induction), using the inductive hypothesis
height(k+1) = height(k)+1
= log2(k)+1
> log2(k+1) because log function grows less than linear
case 2: The node does not increase the height - the tree n=k was not complete.
# internal nodes in a complete tree of height h (where keys are only in internal
nodes)
n = 2^(h-1)-1
n+1 = 2^(h-1)
h-1 = log2(n+1)
h = log2(n+1)+1
so, h > log2(n+1)+1 when tree is not complete
Therefore,
h(k) >= log2(k) for all values n>k if true for n=k
ie. since true for n=1, then also true for n=2, then true for n=3, then true for
n=4, and so on for all integers n.
----
Result to prove: height(k+1) >= log2(k+1)
Note on strong vs weak induction. When inductive hypothesis is for a single value
n=k, it's weak induction.
When assuming true for n<k, this is strong induction. (or alternatively, assume
n<=k and prove k+1 or n>k, or assume n<k and prove for k).
=====================================
Question 3.
proof #external(n) = n+1 for all n
Base case:
prove for n=1
#external for n=1, is 2.
= n+1 = 1+1, so the case of n=1 is true.
#external(k+1) = #external(k) - 1 + 2
#external(k+1) = (k+1) -1 +2
#external(k+1) = (k+2) = (k+1) + 1
which is the required result.
Therefore, true for n=k+1 if true for n=k. So, since it's true for n=1, then also
true for n=2, then n=3, and so on for all integers n.
=========================
Question 4.
21
/ \
6 30
/
5
algorithm will return true, but the BST property is violated because 5 < 21.
So the algorithm is incorrect
==========================
Question 5.
- keep going right, to get the right-most node until null is reached
def largest(u):
while u.right != None:
u = u.right
return u
===========================
Question 6.
second largest
- find largest node
- if it has a left child, return the rightmost (ie, largest node) in left child's
subtree
- otherwise, return the parent
def second_largest(T):
u = largest(T.root)
if u.left != None:
return largest(u.left)
return u.parent
============================
Question 7.
inorder traversal of BST will return the nodes in sorted order: O(n)
- do an inorder traversal and return the n/2th node encountered. - but this will
take O(n)..
-----
Augment insertion and deletion routines to keep track of subtree size at each node.
- each time a node is inserted, increase the subtree size for all ancestors by 1.
- each time a node is removed, decrease the subtree size of all ancestors by 1.
The additional time required is O(h) which does not change the running time of
insert or delete, which are already O(h).
----------------------------
Check subtree size of left and right children, and also consider nodes seen before
on left and right sides. Go towards the subtree with more nodes.
If the number of nodes in left and right side is equal, the current node is a
median (even number of nodes in total).
If the number of nodes in left side is 1 less than right side, it's also a median
(lower of two medians in even number of nodes).
def median(T):
u = T.root
left_seen = 0
right_seen = 0
while True:
left_size = 0
if u.left != None:
left_size = u.left.size
right_size = 0
if u.right != None:
right_size = u.right.size
While loop moves to either left or right child on each iteration, therefore runs
for at most h iterations. On each iteration, O(1) for comparison, addition,
attribute access. etc.
total = O(h)
==============================================
Question 8.
Adoption node: defined as the closest node outside the range from the current
position.
Any orphan nodes created from the deletion will get reattached as a child of the
adoption node, and will then become the new adoption node itself
for LHS of boundary: nodes are attached to right child of adoption node
for RHS of boundary: nodes are attached to left child of adoption node
In the case the root was in the range and was deleted, there are two halves
remaining that need to be reconnected - choose a new root which is either the
rightmost of left half, or leftmost of right half. Detach this node and make it as
the new root.