BST: print tree in sorted order using another key

I have next structures code in C for my binary tree:

struct student
{
    int studentID;
    char lastName[MAX_LENGTH];
    char firstName[MAX_LENGTH];
    float amount;
};

struct node
{
    struct student* record;
    struct node* left;
    struct node* right;
};

Records are inserted to the tree using studentID field as a key. When I'm printing the tree in order of studentID all is fine. But I want to print the SAME tree in order of lastName field.

I have only one idea: copy tree to array, sort array and display array.

Is there is any another solution?

Answers


You can build and maintain multiple binary search trees in parallel, optionally folding them into a single multi-indexed data structure:

struct node {
    struct student *record;
    struct node *left_by_id, *right_by_id,
                *left_by_name, *right_by_name;
};

Obviously, all the BST operations have to be modified for this to work: e.g. to insert a new struct student*, you must tie a single node into two trees.


Need Your Help

Set Webkit Keyframes Values Using Javascript Variable

javascript css variables webkit keyframe

I have a piece of JS code to generate random numbers and output them as variables to be used here in place of the rotation values

How I get menu click event

java google-glass google-mirror-api

I created a custom menu using mirror api.