• 沒有找到結果。

6.2 Balanced Binary Search Trees

N/A
N/A
Protected

Academic year: 2022

Share "6.2 Balanced Binary Search Trees"

Copied!
3
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

Homework #6

TA in charge: Yu-Cheng Chou, Ya-Hsuan Chang and Wei-Yuan Shen RELEASE DATE: 05/25/2012

DUE DATE: 06/08/2012, 17:00

As directed below, you need to submit your code to the designated place on the course website.

Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts.

Discussions on course materials and homework solutions are encouraged. But you should write the final solutions alone and understand them fully. Books, notes, and Internet resources can be consulted, but not copied from.

Since everyone needs to write the final solutions alone, there is absolutely no need to lend your homework solutions and/or source codes to your classmates at any time. In order to maximize the level of fairness in this class, lending and borrowing homework solutions are both regarded as dishonest behaviors and will be punished according to the honesty policy.

Both English and Traditional Chinese are allowed for writing any part of your homework (if the compiler recognizes Traditional Chinese, of course). We do not accept any other languages. As for coding, either C or C++ or a mixture of them is allowed.

This homework set comes with 200 points and 10 bonus points. In general, every home- work set of ours would come with a full credit of 200 points.

6.1 Hashing, Skip List, Binary Search Tree

(1) (15%) Consider an alternative quadratic probing scheme that visits h0(key), h1(key), h2(key),

· · · , hm(key) where

hi(key) = h(key) + 16i2 mod P

When P is an odd prime, let m = P −12 and i 6= j with both i and j within {0, 1, 2, · · · , m}. Prove that hi(key) 6= hj(key) for all i, j that are between 0 and m.

(2) (15%) Do Exercise R-9.14 of the textbook.

(3) (15%) Do Exercise C-9.4 of the textbook. You need to discuss why your algorithm runs in O(log n) time.

(4) (15%) Do Exercise C-9.12 of the textbook. You need to discuss why your algorithm runs in O(log n) expected time.

(5) (15%) Do Exercise C-10.4 of the textbook.

(6) (Bonus 10%) Construct a perfect hash function that is efficiently computable for the following 32 standard keywords in C. You need to explain why the hash function is perfect and why it is efficiently computable to get the full bonus.

auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while

1 of 3

(2)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

6.2 Balanced Binary Search Trees

(1) (40%) Treap is a combination of the binary search tree and the heap (with random priority). With the help of the random priority, the binary search tree can be balanced with a high probability.

The following description of the Treap is modified from wikipedia (http://en.wikipedia.org/

wiki/Treap):

In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of insertions and deletions of keys, the shape of the tree is a random variable with the same probability distribution as a random binary tree; in particular, with high probability its height is proportional to the logarithm of the number of keys, so that each search, insertion, or deletion operation takes logarithmic time to perform.

Use any resource that you can find to study the Treap algorithm, and write a program hw6 2 1 that inserts the following 30 (additional) keywords in C++ using the order they are provided to the treap. Output the resulting Treap (including the random priority) you get.

asm dynamic cast namespace reinterpret cast try bool explicit new static cast typeid catch false operator template typename class friend private this using const cast inline public throw virtual delete mutable protected true wchar t

(2) (45%) libavl (http://www.stanford.edu/~blp/avl/) is a useful library for binary search trees.

For instance, the following short code constructs an AVL tree of 16 integers and print it out.

#include <s t d i o . h>

#include < s t d l i b . h>

#include ” a v l . h”

void p o s t o r d e r i n t e g e r a v l ( const s t r u c t a v l n o d e ∗ node ) { i f ( node == NULL)

return ;

p r i n t f ( ”%d ” , ∗ ( ( i n t ∗ ) node−>a v l d a t a ) ) ;

i f ( node−>a v l l i n k [ 0 ] != NULL | | node−>a v l l i n k [ 1 ] != NULL) { p u t c h a r ( ’ ( ’ ) ;

p o s t o r d e r i n t e g e r a v l ( node−>a v l l i n k [ 0 ] ) ; p u t c h a r ( ’ , ’ ) ;

p u t c h a r ( ’ ’ ) ;

p o s t o r d e r i n t e g e r a v l ( node−>a v l l i n k [ 1 ] ) ; p u t c h a r ( ’ ) ’ ) ;

} }

i n t i n t c o m p a r e ( const void ∗pa , const void ∗pb , void ∗param ) {

i n t a = ∗ ( const i n t ∗ ) pa ; i n t b = ∗ ( const i n t ∗ ) pb ; i f ( a < b ) return −1;

e l s e i f ( a > b ) return +1;

e l s e return 0 ; }

i n t main ( ) {

s t r u c t a v l t a b l e ∗ t r e e ;

t r e e = a v l c r e a t e ( i n t c o m p a r e , NULL, NULL ) ; i n t i ;

f o r ( i =0; i <16; i ++){

2 of 3

(3)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

i n t ∗ e l e m e n t = ( i n t ∗ ) m a l l o c ( s i z e o f ( i n t ) ) ;

∗ e l e m e n t = i ;

void ∗∗p = a v l p r o b e ( t r e e , e l e m e n t ) ; }

p o s t o r d e r i n t e g e r a v l ( t r e e −>a v l r o o t ) ; p u t s ( ” ” ) ;

return 0 ; }

Note that the manual of libavl may be difficult to read; the code above comes from modifying the avl-test.c in libavl. When the code is compiled with avl.c, it correctly outputs an AVL tree.

7 (3 (1 (0 , 2 ), 5 (4 , 6 )), 11 (9 (8 , 10 ), 13 (12 , 14 (, 15 )))) Write a program hw6 2 2 that inserts the 30 keywords of C++ in Problem 6.2(1) using the order they are provided to the height-bounded binary search tree (bst.c), AVL tree (avl.c) and Red- Black tree (rb.c). Output the resulting trees with a format similar to the output above.

(3) (40%) Write a program hw6 2 3 that randomly generates 1024 integers before they are inserted into the four different trees above. For 10000 different random rounds, record the height of the trees that you get. Then, fill in the following the table and briefly state your findings.

tree type maximum height minimum height average height Treap

height-bounded binary search tree AVL tree

Red-Black tree

You are strongly encouraged to to talk to the TAs if you encounter problem using/compiling libavl.

Submission File

Please upload your program as a single ZIP compressed file to CEIBA before the deadline in the Friday afternoon (06/08/2012). The zip file should be like b86506054.zip, where the file name should be changed to your own school ID. The ZIP file should contain the following items:

• hw6 2 1.c or hw6 2 1.cpp

• hw6 2 2.c or hw6 2 2.cpp

• hw6 2 3.c or hw6 2 3.cpp

The TAs will use the Makefile provided on the course website to test your code. Please make sure that your code can be compiled with the Makefile on CSIE R217 linux machines.

3 of 3

參考文獻

相關文件

Search the portion of List preceding TestEntry for TargetValue, and report the result of that search if (TargetValue &gt; TestEntry):. Search the portion of List following

Search the portion of List preceding TestEntry for TargetValue, and report the result of that search if (TargetValue &gt; TestEntry):. Search the portion of List following

12 Binary Search Trees 13 Red-Black Trees 14 Augmenting Data Structures IV Advanced Design and Analysis Techniques. 15 Dynamic Programming 16 Greedy Algorithms 17

Binary tree based data structure Data structure based on binary

[r]

[r]

[r]

印出Optimal Binary