I am working on a programming assignment to create a real estate database in c.
ID: 3687624 • Letter: I
Question
I am working on a programming assignment to create a real estate database in c. We must use a BST. I have managed to create a tree, but am getting lost in some of the functions. Here is the assignment, and the code I have written follows. I have the create and insert functions working, but I am not sure how to implement the delete function, or the search function. I have only created the tree using the property listings and have not input any of the other data, but I can do that. If you post any code, it would be great to have a brief explaination of how the pointers work for the delete function, I think that is where I am getting lost. Thank you in advance!
Objective By completing this assignment, students will demonstrate a working knowledge of the following Binary Search Tree (BST) concepts: 1. Code implementation of BST 2. Traversing BST 3. Algorithm for inserting nodes into a binary search tree 4. Deleting nodes from a BST 5. Searching for values in BST
Problem: Manipulating Real Estate Database You are required to create software to manage a real estate database. Because the system must be frequently searched and updated, your team-lead has suggested that you use a BST, ordered on the listingID, to store all of the property listings.
Each property listing is comprised of the following information: Listing ID (an integer) Agent (a string no greater than 12 characters long with no spaces) Price (an integer) Size in square footage (an integer) Number of beds (an integer) Number of baths (a double) Year built (an integer) Address (a string, no longer than 30 characters long)
Your code should be able to handle the following database operations: 1) Adding a property listing 2) Deleting a listed property 3) Searching for a listed property by ID 4) Searching for properties listed up to a specified price 5) Searching for listed properties by Agent 6) Listing all of the available properties
When run, your program should read real estate information from an input file called listings.txt. It should store this information in a database implemented as a BST, built as each listing is read. Your program should then execute a given set of operations on the created database.
2
Input Specification (for listings.txt) The first line of the file will contain a single positive integer n representing the number of properties listed. This will then be followed by n lines of data; each representing a single property. Each line of data will begin with an integer representing the listingID, a string representing the agent, an integer representing the cost of the property. This is followed by: the property size (an integer) Number of beds (an integer) Number of baths (a double) Year built (an integer) Address (a string, no longer than 30 characters long)
The rest of the file is a series of lines each with a database operation to be executed. Each line begins with an integer from 1 – 6 inclusive, indicating the database operation to be executed. The numbers correspond to the list on the previous page.
If the operation is (1), to add a property to the listings, then the line will contain the all of the listing information, in the order given above.
If the operation is (2), to delete a listing, then the line will contain one other integer representing the listingID of the property to be deleted. If the operation is (3), to search for a listing by ID, then the line will contain one other integer representing the listingID of the property to be searched for.
If the operation is (4), to search for a listing by price, then the line will contain an integer representing the maximum price of the properties to be searched for.
If the operation is (5), to list all of properties under a given agent, then the line will contain the agent’s name after the operation number.
If the operation is (6), to print all of the available properties, then that will be the only number on the line
The input file ends with a 0.
Output Specification
Your output should match that in the sample output. You are to use the exact text for the functions as shown.
3
Implementation Restrictions a. #define ADD_LENGTH 30 (for address length) b. Each Listing must be implemented as a struct c. Each BST node should have a listing as its data d. Your program should be written using appropriate functions. e. Do not use global variables. Instead pass parameters to your functions. Remember that for functions that will be changing your BST, it (the BST), should be passed as a parameter by reference. f. Deletions of listings should be implemented using the successor algorithm
Deliverables Your assignment should be submitted via Webcourses by the deadline. No assignments will be accepted via email. Your submission should include a single .c file called realEstateDB.c. Remember that your program will be graded using Eustis, so be sure that your submitted code runs correctly on the server.
Remember that it is each student’s responsibility to ensure that the correct file has been uploaded. Thus, as advised at the beginning of the semester, immediately after submitting your code, download it and verify that you have submitted the intended file and that the submission has gone through. An excuse that there was an upload error is not grounds for late submission. No assignments will be accepted after the 24 hour window for late submissions via Webcourses.
Sample Input File (library.txt)
6
121 Matt 205000 3000 3 2.0 2000 35 Blueberry Lane
163 Amy 450000 5000 5 5.5 2016 8885 Winter Garden Drive
116 Grant 375000 3000 3 2.0 2015 191 Oviedo Lakes
100 Linda 355000 4000 4 2.5 2014 79 Bradmore Lane
155 Amy 495000 3500 3 3.0 2012 52 Lenox Drive
280 Grant 950000 5000 5 6.5 2016 223 Willow Pines
6
1 119 Linda 985000 2950
2 2.0 2013 1200 Park Avenue 2 116
3 132
5 Amy
4 400000
6
0
#include <stdio.h>
#include <stdlib.h>
#define ADD_LENGTH 30
typedef struct node{
int listID;
char agent[12];
int price;
int size;
int beds;
double baths;
int year;
char address[30];
struct node* left;
struct node* right;
}node;
node* create(int);
node* insert(node*, int);
void display(node*);
void deleteNode(node*);
void deleteItem(node*, int);
int main()
{
int listID, price, size, beds, year, i, numNodes;
double baths;
char agent[12];
char address[30];
FILE *ifp;
ifp = fopen("library.txt", "r");
fscanf(ifp, "%d", &numNodes);
fscanf(ifp, "%d", &listID);
node *root = create(listID);
for(i=0; i<numNodes-1; i++)
{
fscanf(ifp, "%d", &listID);
printf("%d ", listID);
insert(root, listID);
}
display(root);
//printf("%d %d %d", root->listID, root->right->listID, root->left->listID);
deleteItem(root, 116);
display(root);
return 0;
}
node* create(int listID)
{
node *newNode = (node*)malloc(sizeof(node));
if(newNode == NULL)
{
printf("Out of memory.");
exit(1);
}
newNode->listID = listID;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
node* insert(node *root, int listID)
{
if(root == NULL)
{
return create(listID);
}
if(listID<root->listID)
{
if(root->left == NULL)
{
root->left = create(listID);
}
else
{
insert(root->left, listID);
}
}
if(listID>root->listID)
{
if(root->right == NULL)
{
root->right = create(listID);
}
else
{
insert(root->right, listID);
}
}
}
void display(node *root)
{
if(root == NULL)
{
return;
}
printf("%d ", root->listID);
if(root->left != NULL)
{
printf("L: %d", root->left->listID);
}
if(root->right != NULL)
{
printf("R: %d", root->right->listID);
}
printf(" ");
display(root->left);
display(root->right);
}
void deleteNode(node *root)
{
node *temp;
temp = root;
if(root->left == NULL)
{
root = root->right;
free(temp);
}
else if(root->right == NULL)
{
root = root->left;
free(temp);
}
else
{
temp = root->left;
while(temp->right != NULL)
{
temp = temp->right;
}
root->listID = temp->listID;
deleteItem(root->left, root->right);
}
}
void deleteItem(node *root, int listID)
{
if(root == NULL)
return;
if(listID<root->listID)
{
deleteItem(root->left, listID);
}
else if(listID>root->listID)
{
deleteItem(root->right, listID);
}
else
deleteNode(root);
}
Explanation / Answer
Your code has too many logical errors. I did some changes to the code and implemented delete function plz have a look.
#include <stdio.h>
#include <stdlib.h>
#define ADD_LENGTH 30
typedef struct node{
int listID;
char agent[12];
int price;
int size;
int beds;
double baths;
int year;
char address[30];
struct node* left;
struct node* right;
}node;
node* create(int);
node* insert(node*, int);
void display(node*);
struct node* deleteNode(node*,int);
//void deleteItem(node*, int);
int main()
{
int listID, price, size, beds, year, i, numNodes;
double baths;
char agent[12];
char address[30];
FILE *ifp;
ifp = fopen("library.txt", "r");
fscanf(ifp, "%d", &numNodes);
printf("Num Nodes %d ",numNodes);
fscanf(ifp, "%d", &listID);
node *root = create(listID);
for(i=0; i<numNodes-1; i++)
{
fscanf(ifp, "%d", &listID);
insert(root, 161+i); // inserting some random values
// printf("%d ", listID);
insert(root, listID);
}
display(root);
//printf("%d %d %d", root->listID, root->right->listID, root->left->listID);
deleteNode(root, 161);
display(root);
return 0;
}
node* create(int listID)
{
//printf("List id %d",listID);
node *newNode = (node*)malloc(sizeof(node));
if(newNode == NULL)
{
printf("Out of memory.");
exit(1);
}
newNode->listID = listID;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
node* insert(node *root, int listID)
{
if(root == NULL)
{
return create(listID);
}
if(listID<root->listID)
{
if(root->left == NULL)
{
root->left = create(listID);
}
else
{
insert(root->left, listID);
}
}
if(listID>root->listID)
{
if(root->right == NULL)
{
root->right = create(listID);
}
else
{
insert(root->right, listID);
}
}
else if(listID == root->listID)
{
if(root->right == NULL)
{
root->right = create(listID);
}
else
{
insert(root->right, listID);
}
}
}
void display(node *root)
{
if(root == NULL)
{
printf("Empty tree"); return;
}
printf("%d ", root->listID);
if(root->left != NULL)
{
//printf("L: %d", root->left->listID);
printf("L: "); display(root->left);
}
if(root->right != NULL)
{
//printf("R: %d", root->right->listID);
printf("R: "); display(root->right);
}
printf(" ");
//display(root->right);
}
/*void deleteNode(node *root)
{
node *temp;
temp = root;
if(root->left == NULL)
{
root = root->right;
free(temp);
}
else if(root->right == NULL)
{
root = root->left;
free(temp);
}
else
{
temp = root->left;
while(temp->right != NULL)
{
temp = temp->right;
}
root->listID = temp->listID;
deleteItem(root->left, root->right);
}
}
void deleteItem(node *root, int listID)
{
if(root == NULL)
return;
if(listID<root->listID)
{
deleteItem(root->left, listID);
}
else if(listID>root->listID)
{
deleteItem(root->right, listID);
}
else
deleteNode(root);
}
/* return the node with minimum value */
struct node * minValueNode(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node* deleteNode(struct node* root, int listID)
{
// base case
if (root == NULL) return root;
// If the listID to be deleted is smaller than the root's listID,
// then it lies in left subtree
if (listID < root->listID)
root->left = deleteNode(root->left, listID);
// If the listID to be deleted is greater than the root's listID,
// then it lies in right subtree
else if (listID > root->listID)
root->right = deleteNode(root->right, listID);
// if listID is same as root's listID, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct node* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->listID = temp->listID;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->listID);
}
return root;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.