CS202_Assignment2 - CS 202 Fundamental Structures of...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: CS 202 Fundamental Structures of Computer Sciences II Assignment 2 – Binary Search Trees Due Date: March 28, 2011 (Monday) Question 1 (10 Points) – Building a binary search tree Build a binary tree for the first 8 unique letters of the concatenation of your name and surname. If your full name is shorter than 8 letters or it has not enough unique letters, append sufficient number of letters to the end of it, starting from the first letter of the alphabet (as an example, suppose that your name is CHUN LI, then build your tree with {C, H, U, N, L, I, A, B}; as another example suppose that your name is ALI CAN, then build your tree with {A, L, I, C, N, B, D, E}). Beginning with an empty binary search tree, insert the letters into the tree starting from the first letter. Indicate insertion order of each node with a number on the top of it. After drawing the tree, show the preorder, inorder, postorder, and level ­ order 1 traversals of your binary tree. Example: 7 1 C A H B 8 6 L 2 U 4 5 N 3 Pre ­order: C – A – B – H – U – N – L – I In ­order: A – B – C – H – I – L – N – U Post ­order: B – A – I – L – N – U – H – C Level ­order: C – A – H – B – U – N – L – I I Question 2 (10 Points) – Insertion and deletion in binary search trees a) Form a binary search tree by inserting the following numbers into the tree in the given order. Indicate insertion order of each node with a number on the top of it. 15, 21, 2, 8, 7, 12, 42, 35, 18, 56, 4, 6, 14 b) Delete the following values from your tree in the given order and draw your tree after each deletion. 2, 42, 21 Question 3 (10 Points) – Proof Prove by induction that a binary tree with n nodes has exactly n + 1 empty subtrees (or, in C++ terms, n + 1 NULL pointers). 1: “Trees can also be traversed in level ­order, where we visit every node on a level before going to a lower level. This is called breadth ­first traversal.” –Wikipedia, the free encyclopedia Question 4 (70 Points) – Programming Assignment In this question, you will implement a simple Real Estate Information System. This system will keep the available real estates in particular districts and allow the user to make queries on the real estates. For that, it should consist of the following functionalities; the details of these functionalities are given below. • Load districts and the available real estates in these districts from an input file • Add a new real estate to an existing district • Delete an existing district with its real estates • List the k most expensive real estates in a given district (top – k queries). • List the k cheapest real estates in a given district (bottom – k queries). • List real estates whose prices are between x and y (range queries). In this question, you should implement a menu driven C++ program that reads a command from the user and executes the job described by the command. Each command is explained with its functionality in the table below. Please note that you are welcome to add new commands to the system. If you do so, please display a list of your extra commands just at the beginning of your program. You may earn extra credits for these commands. Load districts and real estates: Your program should load districts and the available real estates in these districts from a formatted text file. In this file, district names are preceded by “ ­D”. Then, the real estates are given in the following lines. Each real estate has an address and a price value. You may assume that district names and addresses do not consist of any white space characters and the price is a valid positive integer. An example of the contents of such a file is given below: -D Bilkent 14.cad,no:23 15.cad,no:12/14 125.cad,no:15/2 34.cad,no:35 Bilkent4,No:5 -D Cayyolu 122.Sokak,12 SaricamCad.12/14 MavicamCad.15 -D Kizilay -D Cankaya HosdereCad,1234 1600 300 600 1200 800 300 300 1000 500 In your implementation, you MUST use binary search trees to keep the districts and the real estates. For this purpose, you should use one binary search tree to keep the districts by name (e.g., for the example file given above, you will have a binary search tree to keep the districts of Bilkent, Cayyolu, Kizilay, and Cankaya). Note that “ ­D” is just for understanding that the line corresponds to a district name; but it is not a part of the district name. Additionally, you may assume that district names are unique. Then, to keep the real estates of each district, you should create a binary search tree in which the real estates are kept by their prices (e.g., for the example given above, you will have four more binary search trees: one for the real estates of the Bilkent district, one for the real estates of the Cayyolu district, one for the real estates of the Kizilay district [for this example you will have an empty binary search tree for the Kizilay district since it does not have any real estate in the file], and one for the real estates of the Cankaya district). Here, you CANNOT assume that prices are unique. To deal with same price values, you may define your binary search tree such that for a node, smaller valued items are kept in the left subtree of the node and greater or equal valued items are kept in its right subtree. Your program MUST build these binary search trees at the time of reading the district and real estate information from the file. In the execution of a single program, you will have a single object to keep the district and real estate information. However, a program could consist of more than one load command. At each load command, you are supposed to free the memory space that has been allocated for the previous load command. Of course, for the first load command, there is no need to make deallocation. Add a new real estate: The system will allow adding a new real estate to an existing district. For that, in the command line (please see the table below for the exact formatting), you should take the name of the district as well as the address and the price of the real estate. Likewise, you may assume that there are no white space characters in the district name and the address and the price is a valid positive integer. Your program should check whether or not the given district exists in the system. If it does, it should add the real estate to the corresponding binary search tree (i.e., the binary search tree that is defined for keeping the real estates of the given district). Otherwise, it should give a warning message (for the format of this message, please check the sample output). Delete a district: Your program will allow deleting an existing district with all of its real estates from the system. For that, it will take a district name from the command line (again see the table for the format of the command) and delete the given district from the binary search tree in which districts are kept. Your program should check whether or not the given district exists in the system. If it does, it should perform the delete operation. Otherwise, it should give a warning message (for the format of this message, please check the sample output). In this part, please do not forget to destroy the binary search tree that is defined for keeping the real estates of the given district. List the k most expensive real estates in a given district: Your program will take the name of a district and find the k most expensive real estates in this district. It will display the information of these real estates on the screen (please check the example output for the required format). Similarly, in this part, you should check whether or not the given district exists in the system. If it does, it should perform the operation. Otherwise, it should give a warning message. Your system should display the real estates in a descending order. If the number of the real estates in the given district is smaller than k, then just display the information of the available real estates. In selecting the k ­most expensive real estates, do not forget that these real estates are kept in a binary search tree according to their prices. So you SHOULD make use of this information in searching the k ­most expensive real estates. List the k cheapest real estates in a given district: Your program will take the name of a district and find the k cheapest real estates in this district. It will display the information of these real estates on the screen (please check the example output for the required format). Similarly, in this part, you should check whether or not the given district exists in the system. If it does, it should perform the operation. Otherwise, it should give a warning message. Your system should display the real estates in an ascending order. If the number of the real estates in the given district is smaller than k, then just display the information of the available real estates. In selecting the k ­cheapest real estates, do not forget that these real estates are kept in a binary search tree according to their prices. So you SHOULD make use of this information in searching the k ­cheapest real estates. List real estates in a given district whose prices are in between x and y: Your program will take the name of a district and two price values and list the information of the real estates in the district whose prices are in between (inclusively) these given price values. In this part, you should also check whether or not the given district exists in the system. If it does, it should perform the operation. Otherwise, it should give a warning message. Your system should display the real estates in either an ascending or descending order. If there is no real estate in the specified price range, then your system should give a warning message too. Similarly, in selecting the real estates, do not forget that these real estates are kept in a binary search tree according to their prices. So you SHOULD make use of this information in searching the real estates. In general, in all the aforementioned functionalities, you may assume the following (these assumptions have already been mentioned in some of the commands): • • • • • • For all commands, you may assume that the district names do not contain any white space characters. For all commands, you may assume that the real estate addresses do not contain any white space characters. You may also assume that the prices are positive integers and that the commands do not specify invalid integer values. The district names are unique. You may assume that the names are case sensitive (for example, bilkent and Bilkent can be considered as different district names). The real estate prices may not be unique. Please use the following table to see the format of how your program SHOULD take the command. In this table, boldface words are parts of the commands and italic words provide the information about districts and real estates, with which the command is executed, and thus, those italic words could change from one command to another. Please note that you may get zero points if your program uses another format for the commands. Thus, please be careful about the use of the commands in a given format. Please check the sample output to see the format of the output of the commands. command {parameters} load filename It reads the district and real estate information from a file called filename and constructs the necessary binary search trees. It adds a new real estate to a district with a name of district. The address information is given by address and the price information is given by price. It deletes a district whose name is given by district and all of its real estates. It lists the k most expensive real estates in a district whose name is given by district. It lists the k cheapest real estates in a district whose name is given by district. It lists real estates in a district (with a name of district) whose prices are in between price1 and price2. It exits the program add district address price delete district list district –t k list district –b k list district –bw price1 price2 exit  ­t = top,  ­b = bottom,  ­bw = between Notes and hints about using the commands • • • In the command table, parameters starting with “–“ are abbreviations which were defined to decrease typing errors while writing commands. When the program is running, your main function should always wait for a command, execute it, and then return to initial state waiting for another command (like any command prompt). It is suggested that when something is written to console you should first analyze it and decide whether it is a command or not. To do so, parse the given string and than check each token with known commands. If a valid command is entered than check the parameters. If everything is okay than display what command says and then return to the initial state. If parameters are wrong, warn the user for correction. If string is not a command throw an error message and wait for a correct command. Sample Output The following output is prepared according to the example input file whose contents are given above. Please note that the usage of commands is not limited by this sample output. Therefore, please try other usages according to the aforementioned descriptions. CS202 Assignment-2 Tiny Real Estate Information System (TREIS) by Salim Arslan, Sec-0 ?> load estate.txt 9 real estates are loaded from estate.txt... Program is ready to run other commands... ?> list Bilkent –bw 15.cad,no:12/14 125.cad,no:15/2 Bilkent4,No:5 34.cad,no:35 14.cad,no:23 0 100000 price(300) price(600) price(800) price(1200) price(1600) ?> add Bilkent 117.cad,no:3 1500 7.cad,no:3 (with price 1500) has been added to Bilkent. ?> list Bilkent –bw 15.cad,no:12/14 125.cad,no:15/2 Bilkent4,No:5 34.cad,no:35 117.cad,no:3 14.cad,no:23 0 100000 price(300) price(600) price(800) price(1200) price(1500) price(1600) ?> list Bilkent –t 3 14.cad,no:23 price(1600) 117.cad,no:3 price(1500) 34.cad,no:35 price(1200) ?> list Bilkent –b 2 15.cad,no:12/14 price(300) 125.cad,no:15/2 price(600) ?> list Bilkent –bw 125.cad,no:15/2 Bilkent4,No:5 34.cad,no:35 600 1400 price(600) price(800) price(1200) ?> list Bilkent –bw 900 1100 No real estates are found in that range at Bilkent. ?> delete Bilkent District Bilkent and all of its real estates have been deleted... ?> list Bilkent –bw 600 1400 District Bilkent does not exist ... ?> list Cayyolu –b 1 122.Sokak,12 price(300) ?> list Cayyolu –b 2 122.Sokak,12 price(300) SaricamCad.12/14 price(300) ?> list Cayyolu –t 7 MavicamCad.15 price(1000) SaricamCad.12/14 price(300) 122.Sokak,12 price(300) ?> list Kizilay –t 2 No real estates are found at Kizilay. ?> add Bornova DenizSokak,no:3 1500 District Bornova does not exist. ?> delete Karsiyaka District Karsiyaka does not exist. ?> list Bornova –t 3 District Bornova does not exist. ?> list Bornova –b 2 District Bornova does not exist. ?> list Bornova –bw 500 2000 District Bornova does not exist. ?> find Bilkent -b 2 Unknown command... ?> list Bilkent –tyz 1000 Wrong usage; please check your command... ?> exit I will miss you so much. See you... Code Format and Notifications To increase the efficiency of the grading process as well as the readability of your code, you have to follow the following instructions about the format and general layout of your program. • Don’t forget to write down your id, name, section, assignment number or any other information relevant to your program in the beginning of your main file. Example: /* * Title: A Real Estate Program by a Real Programmer * Author: Salim Arslan * ID: 2100000000 * Section: 0 * Assignment: 2 * Description: This program is a tiny real estate information software * for keeping tracks of estates in Ankara, and running some basic * queries. */ • Since your codes will be checked without your observation, you should report everything about your implementation. Well comment your classes, functions, declarations etc. Make sure that you explain each function in the beginning of your function structure. Example: DistrictTree readFile(char* fileName) // -------------------------------------------------------------------// Summary: Reads items in given file // Precondition: Filename defines a valid file. // Postcondition: The file specified by filename is read and a bst is // constructed. // Calls: calledFunction1, calledFunction2 ... // -------------------------------------------------------------------{ // body of the function } // end readFile • Indentation, indentation, indentation... • Pay attention to these instructions, otherwise you may lose some points even though your code has no error. ...
View Full Document

This note was uploaded on 10/23/2011 for the course ENGINEERIN 102 taught by Professor Pablo during the Spring '11 term at Bilkent University.

Ask a homework question - tutors are online