2010. 3. 24.

Data Structures

I also have written few programs using a data structures, including linked lists, hash tables, priority queues, and binary search trees. The following code is a portion of a rental car management software written in C++. Specifically, it searches a given car class object in the search tree using recursion. Class 'car' is structured as the following:


struct car{
string marque; // -- brand 
string model; // -- model
bool isRented; // -- flag of whether the car is rented
bool reCommissioned; // -- flag of whether the car is reCommissioned (ready to be rented again)
bool inService; // -- flag of whether the car is in service.
car * left; // -- points at the left child
car * right; // -- points at the right child

};




// Search for CarToBeSearched in the tree recursively.
carBST::car* carBST::Search(car* CarToBeSearched, car* node) {

// CarToBeSearched is found!
if(CarToBeSearched->model.compare(node->model) == 0) {
return node;

// CarToBeSearched is smaller than current node.
// continue searching in left subtree
} else if (CarToBeSearched->model.compare(node->model) < 0) {
if (node->left != NULL) {
return Search(CarToBeSearched, node->left);
}

// CarToBeSearched is greater than current node.
// continue searching in right subtree
} else if (CarToBeSearched->model.compare(node->model) > 0) {
if (node->right != NULL) {
return Search(CarToBeSearched, node->right);
}
}

//CarToBeSearched is not found in the tree.
return NULL;
}

댓글 없음: