Hi.
I've a BST created for an address book application for my term project.
When inserting into the BST, I insert items sorted by their first names in ascending order.
The thing is that the user should be able to display the BST in the ascending form of last names & birthdays as well.
My solution to that is inserting items from the BST into a new BST in the sorted form of last names in ascending order. Besides that I've no idea about how to sort by birthdays.
So I've coded something for sorting by last name. It uses my insert into the BST code with the change of sorting by last names.
//
// sort_by_last_name.h
// Project
//
// Created by Can Sürmeli on 5/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
// PURPOSE: Sorts according to last names
struct tree_node *sort_by_last_name(struct tree_node *node, struct tree_node *last_name_node) {
// if there is no root
if( last_name_node == NULL )
last_name_node = create_node(NULL, NULL, node->data);
// if there is a root & the new entry should be placed before the root
else if( strcmp(last_name_node->data.last_name, node->data.last_name) < 0 )
last_name_node->left = sort_by_last_name(node, last_name_node->left);
// if there is a root & the new entry should be places after the root
else if ( strcmp(last_name_node->data.last_name, node->data.last_name) > 0 )
last_name_node->right = sort_by_last_name(node, last_name_node->right);
// if there is a root & the last names are identical
else {
// if it should be placed before the root
if ( strcmp(last_name_node->data.phone, node->data.phone) < 0 )
last_name_node->left = sort_by_last_name(node, last_name_node->left);
// if it should be places after the root
else if( strcmp(last_name_node->data.phone, node->data.phone) > 0 )
last_name_node->right = sort_by_last_name(node, last_name_node->right);
// if the entries are the same
else
return last_name_node;
}
return last_name_node;
}
//
// create_node.h
// Proje
//
// Created by Can Sürmeli on 4/29/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
// PURPOSE: Creates a node
struct tree_node *create_node(struct tree_node *a, struct tree_node *b, ENTRY contact) {
struct tree_node *newNode;
newNode = (struct tree_node *)(malloc(sizeof(struct tree_node)));
newNode->data = contact;
newNode->left = a;
newNode->right = b;
return newNode;
}
Also here are my structures:
//
// structure.h
// Proje
//
// Created by Can Sürmeli on 4/25/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
// PURPOSE: Structures
// The structure of a contact(single entry) in the address book
typedef struct entry{
char first_name[15];
char last_name[10];
char date_of_birth[11];
char e_mail[25];
char phone[15];
char address[50];
char city[15];
char zipcode[6];
struct contact *next;
}ENTRY;
// The structure of a single node in the tree structure
struct tree_node {
ENTRY data;
struct tree_node *left;
struct tree_node *right;
};
But it's not working! :(( It just takes the first item and stops.
What am I doing wrong here?
Or is there a better solution than sorting again the BST?
Also what is your idea about sorting the records in the address book by birthdays? How can I determine which one is bigger or smaller?
Thanks.