This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CMPT 307 — Data Structures and Algorithms Outline Solutions to Exercises on RedBlack Trees 1. Show that any arbitrary nnode binary search tree can be transformed into any other arbitrary nnode binary search tree using O ( n ) rotations. Since the exercise asks about binary search trees rather than the more specific redblack trees, we assume here that leaves are fullfledged 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 rightgoing 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 rightgoing 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 nonleaf 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 rightgoing chain. If we new the sequence of right rotations that transforms an arbitrary search tree T to a single rightgoing 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.
 Fall '09
 A.BULATOV
 Algorithms, Binary Search, Data Structures

Click to edit the document details