Data Structures and Algorithms (NTU, Spring 2013) instructor: Hsuan-Tien Lin
Homework #6
TAs’ email: dsata AT csie DOT ntu DOT edu DOT tw RELEASE DATE: 05/24/2013
DUE DATE: 06/06/2013, noon
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.
Note that this is the last homework of this semester and is therefore your last chance of using the MEDALs, if you still have any!
This homework set comes with 200 points and 20 bonus points. In general, every home- work set of ours would come with a full credit of 200 points.
6.1 Skip Lists and Binary Search Trees
(1) (20%) Do Exercise C-9.15 of the textbook.
(2) (20%) Do Exercise R-10.2 of the textbook.
(3) (20%) Do Exercise R-10.6 of the textbook.
(4) (20%) Do Exercise R-10.15 of the textbook.
(5) (20%) Do Exercise C-10.20 of the textbook.
(6) (Bonus 20%) Do Exercise C-10.18 of the textbook.
6.2 Balanced Binary Search Trees
(1) (50%) libavl (http://adtinfo.org/) 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 ] ) ;
1 of 3
Data Structures and Algorithms (NTU, Spring 2013) instructor: Hsuan-Tien Lin
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 ++){
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 1 that inserts the following 30 strings (that can be compared lexicograph- ically)
C, Java, C++, Objective-C, C#, PHP, Visual Basic, Python, Perl, Ruby, JavaScript, Lisp, Pascal, Haskell, Scala, Fortran, Prolog, Assembly, Verilog, Erlang, MATLAB, Bash, SmallTalk, Caml, Scheme, Go, Ada, Cobol, Awk, Tcl/Tk, Delphi, Limbo
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.
(2) (50%) Write a program hw6 2 2 that randomly generates 4096 double numbers between [0, 1]
before they are inserted into the three different trees above. Then, remove the first 2048 keys you generated from the trees. For 10000 different random rounds, record the height of the trees that you get (a) after the insertion (b) after the removal. Then, fill in the following the table and briefly state your findings.
2 of 3
Data Structures and Algorithms (NTU, Spring 2013) instructor: Hsuan-Tien Lin
tree type maximum height minimum height average height after insertion after insertion after insertion height-bounded binary search tree
AVL tree Red-Black tree
tree type maximum height minimum height average height after removal after removal after removal 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 (Program) and Written Copy
Please upload your program as a single ZIP compressed file to CEIBA before the deadline. 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
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.
For all the problems that require illustrations, please submit a written (or printed) copy in class or to CSIE R217 before the deadline.
MEDAL USAGE: If you want to use the gold medals for this homework, please write down the number of medals that you want on the first page of your printed copy (something like “use 2 medals”). Use your medals wisely—usage cannot be retracted.
3 of 3