hw6wsol - Homework #6 CS 2134 Polytechnic University Fall...

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

View Full Document Right Arrow Icon
Homework #6 CS 2134 Polytechnic University Fall 2009 Written Part 1. Consider the following binary search tree: 4 / \ 2 8 / \ 1 9 Color the nodes of the above tree so that it becomes a red-black tree. Is your coloring the only possible one? SOLUTION: 4 B / \ B 2 8 B / \ R 1 9 R This is the only possible coloring, since coloring 2 and 8 red, and 1 and 9 black, would violate the black height property. 2. Consider the following red-black tree. The letters r and b indicate whether the node is colored red or black. 30 b / \ / \ b 28 47 r / / \ / / \ r 22 35 b 54 b / \ r 31 37 r Suppose we insert 32 into this red-black tree. Show the tree after the insertion. (Do all necessary rotations and recoloring.) SOLUTION: (all you needed to show was the final tree) Insert as in a standard BST and color it red. 30 b / \ / \
Background image of page 1

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

View Full DocumentRight Arrow Icon
47 r / / \ / / \ r 22 35 b 54 b / \ r 31 37 r \ r 32 Have red-red violation. Sibling of parent is red. Recolor. 30 b / \ / \ b 28 47 r / / \ / / \ r 22 35 r 54 b / \ b 31 37 b \ r 32 Have red-red violation. Sibling of parent is black. Lower of two red nodes is an inside grandchild. Rotate right to make it an outside grandchild. 30 b / \ / \ b 28 35 r / / \ / / \ r 22 b31 47 r \ / \ r32 37b 54 b Rotate grandparent (30) left and re-color. 35 b / \ / \ r 30 47 r /\ / \ / \ / \ b28 b31 37b 54 b / \ r 22 r32 Done. 3. Consider a hash table of size 11 with hash function h(k) = (5*k) % 11 using linear probing to resolve collisions. a. Show the table after the following elements are inserted in the given
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2011 for the course CS 2134 taught by Professor Hellerstein during the Fall '07 term at NYU Poly.

Page1 / 5

hw6wsol - Homework #6 CS 2134 Polytechnic University Fall...

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

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