solution5 - CMPT 307 — Data Structures and Algorithms...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT 307 — Data Structures and Algorithms Outline Solutions to Exercises on Red-Black Trees 1. Show that any arbitrary n-node binary search tree can be transformed into any other arbitrary n-node binary search tree using O ( n ) rotations. Since the exercise asks about binary search trees rather than the more specific red-black trees, we assume here that leaves are full-fledged nodes, and we ignore the sentinels. We start by showing that with at most n- 1 right rotations, we can convert any binary search tree into one that is just a right-going chain. The idea is simple. Let us define a right spine as the root and all descendants of the root that are reachable by following only right pointers from the root. A binary search tree is just a right-going chain has all n nodes in the right spine. As long as the tree is not just a right spine, repeatedly find some node y on the right spine that has a non-leaf left child x and perform a right rotation on y . Observe that this right rotation adds x to the right spine, and no other nodes leave the right spine. Thus, this right rotation increases the number of nodes in the right spine by n . Any binary search tree starts out with at least one node — the root — in the right spine. Moreover, if there are any nodes not on the right spine, then at least one such node has a parent on the right spine. Thus, at most n- 1 right rotations are needed to put all nodes in the right spine, so that the tree consists of a single right-going chain. If we new the sequence of right rotations that transforms an arbitrary search tree T to a single right-going chain T prime , then we could perform this sequence in reverse — turning each right rotation into its inverse left rotation — to transform T prime back into T ....
View Full Document

This note was uploaded on 11/19/2009 for the course CS CMPT 307 taught by Professor A.bulatov during the Fall '09 term at Simon Fraser.

Page1 / 3

solution5 - CMPT 307 — Data Structures and Algorithms...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online