2 Replies - 1834 Views - Last Post: 17 November 2015 - 12:23 AM Rate Topic: -----

#1 tanjo3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-October 15

Date key in AVL tree

Posted 11 October 2015 - 12:03 PM

Hi all,

I'm trying to figure out how to use the date as a key in an AVL tree. In particular, I have the following:

struct date_field {
    int day;
    int month;
    int year;

struct foo {
    std::string fieldA;
    std::string fieldB;
    date_field date;

I want to insert foos into my AVL tree and keep things sorted using the date field. Two different foos can have identical date fields, but let's just assume that the combination of fieldA and fieldB will be unique. The order of two or more such foos in the AVL tree doesn't matter.

I was thinking of somehow changing date_field to a long and use that as the key and also somehow integrating a hash of fieldA and fieldB in there. I'm just stuck on how to maintain the order using the date if I do it that way.

Do you guys have any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Date key in AVL tree

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,452
  • Joined: 05-May 12

Re: Date key in AVL tree

Posted 12 October 2015 - 03:38 PM

Your AVL insert code should be calling some kind of key value comparison function. Within that function you could simply implement the following pseudo code:
if recordThis.date != recordThat.date
    return recordThis.date - recordThat.date

result = recordThis.fieldA.compare(recordThat.fieldA)
if (result == 0)
    result = recordThis.fieldB.compare(recordThat.fieldB)
return result;

Was This Post Helpful? 0
  • +
  • -

#3 pemby   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 62
  • Joined: 27-June 15

Re: Date key in AVL tree

Posted 17 November 2015 - 12:23 AM

Plugin in some values that are the same at the below link, and take notes on the operation. Then write that out into a an algorithm with respect for your class.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1