{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw6wsol

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

This preview shows pages 1–4. Sign up to view the full content.

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 / \ / \

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

View Full Document
b 28 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 order:
5, 2, 10, 24, 4, 37 (tip: you may want to write a very simple program to compute the function h(k)) SOLUTION: Hash values

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern