0% found this document useful (0 votes)
15 views4 pages

Comp2123 Week4

The document contains solutions to various algorithmic questions related to binary search trees (BST), including proofs of properties like height and external nodes. It discusses methods for finding the largest and second largest nodes, as well as finding the median and performing range queries. Additionally, it highlights the importance of maintaining subtree sizes during insertions and deletions to optimize operations.

Uploaded by

Abhinav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Comp2123 Week4

The document contains solutions to various algorithmic questions related to binary search trees (BST), including proofs of properties like height and external nodes. It discusses methods for finding the largest and second largest nodes, as well as finding the median and performing range queries. Additionally, it highlights the importance of maintaining subtree sizes during insertions and deletions to optimize operations.

Uploaded by

Abhinav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

COMP2123

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

assume true for n=k - inductive hypothesis


height(k) >= log2(k)

prove true for n=k+1 (weak induction), using the inductive hypothesis

- when adding an additional node to the tree:


case 1. it either increases the height, because it was added to the deepest node

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

h(k+1) = h(k) because height did not change


h(k) > log2(k+1)+1
h(k+1) > log2(k+1)+1
h(k+1) >= log2(k+1) because height is an integer, which is the required result

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.

Inductive hypothesis: - assume true for n=k


#external(k) = k+1

prove true for n=k+1, using the inductive hypothesis


- the new node added to the tree, replaces an external node. Then it adds two new
external nodes of its own.

#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

if left_seen+left_size == right_seen+right_size or left_seen+left_size ==


right_seen+right_size+1:
return u

if left_seen + left_size < right_seen + right_size


left_seen += left_size + 1
u = u.right
elif left_seen + left_size > right_seen + right_size
right_seen += right_size + 1
u = u.left

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.

Do a range query as in the lecture


- find boundary nodes
- any nodes within the range are deleted - reference links removed if children or
parent are in the range

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.

if the root was not deleted, then it's fine.

You might also like