Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pls advise on AVL tree

Reply
Thread Tools

pls advise on AVL tree

 
 
Mark
Guest
Posts: n/a
 
      03-29-2012
Hello

I have a big piece of software I'm modifying now to add additional feature,
the big chunk is data structures and related API. Initially I had:

struct nhlfe_entry
{
u_int32_t nhlfe_ix;
u_int32_t xc_ix;
...
u_char nkey [1];
};

Now it's going to be (so nkey will be eliminated) :

struct nhlfe_entry
{
u_int32_t nhlfe_ix;
u_int32_t xc_ix;
..
struct list nkey_list; /* list -- is linked list library */
};

Every node of the linked list will contain object of type:

struct nhlfe_key
{
..
union
{
struct nhlfe_key_ipv4 ipv4;
struct nhlfe_key_ipv6 ipv6;
u_char val;
} u;
};

struct nhlfe_key_ipv4
{
struct pal_in4_addr nh_addr;
u_int32_t oif_ix;
u_int32_t out_label;
};

Now, objects of type 'struct nhlfe_entry' are added in AVL tree and
comparison function is quite straightforward -- based on nkey, something
like this:

int compare_fn(void *data1, void *data2)
{
int ret;
struct nhlfe_entry *nh1, *nh2;
struct nhlfe_key *key1, *key2;
...
nh1 = (struct nhlfe_entry *) data1;
nh2 = (struct nhlfe_entry *) data2;

key1 = (struct nhlfe_key *) nh1->nkey;
key2 = (struct nhlfe_key *) nh2->nkey;

ret = memcmp(&key1->u.ipv4.nh_addr, &key2->u.ipv4.nh_addr, sizeof(struct
pal_in4_addr));
...
}

But now I'm having a list of 'nhlfe_key ' and I'm thinking how the
comparison function to be modifed -- the simple idea that pops up is to
compare key of a new element to be added in the tree with the last key in
the list. Does it make sense or it may impact the correctness of the tree
behavior (specifically balance) ?

Would be very hankful for advices !

Mark


 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      03-29-2012
On Thu, 2012-03-29, Mark wrote:
> Hello
>
> I have a big piece of software I'm modifying now to add additional feature,
> the big chunk is data structures and related API. Initially I had:
>
> struct nhlfe_entry
> {
> u_int32_t nhlfe_ix;
> u_int32_t xc_ix;
> ...
> u_char nkey [1];
> };
>
> Now it's going to be (so nkey will be eliminated) :
>
> struct nhlfe_entry
> {
> u_int32_t nhlfe_ix;
> u_int32_t xc_ix;
> ..
> struct list nkey_list; /* list -- is linked list library */
> };
>
> Every node of the linked list will contain object of type:
>
> struct nhlfe_key
> {
> ..
> union
> {
> struct nhlfe_key_ipv4 ipv4;
> struct nhlfe_key_ipv6 ipv6;
> u_char val;
> } u;
> };
>
> struct nhlfe_key_ipv4
> {
> struct pal_in4_addr nh_addr;
> u_int32_t oif_ix;
> u_int32_t out_label;
> };
>
> Now, objects of type 'struct nhlfe_entry' are added in AVL tree and
> comparison function is quite straightforward -- based on nkey, something
> like this:
>
> int compare_fn(void *data1, void *data2)
> {

....
> ret = memcmp(&key1->u.ipv4.nh_addr, &key2->u.ipv4.nh_addr, sizeof(struct
> pal_in4_addr));
> ...
> }


This is already a problem. Do you really want memcmp() to order your
tree? Remember that in C there may be padding between the fields of
struct nhlfe_key, and this padding may be any random garbage data, and
will affect your memcmp() result.

> But now I'm having a list of 'nhlfe_key ' and I'm thinking how the
> comparison function to be modifed -- the simple idea that pops up is to
> compare key of a new element to be added in the tree with the last key in
> the list. Does it make sense or it may impact the correctness of the tree
> behavior (specifically balance) ?


One thing you often do is "lexicographical comparison" -- the kind
that strcmp() does. If you can compare two struct Foo, you can apply
that to compare two arrays/lists/sequences of struct Foo. In
strcmp()'s case, "struct Foo" is simply char.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about AVL tree John Java 1 04-24-2008 03:28 PM
avl tree sophia C Programming 3 04-23-2008 07:37 PM
avl tree Zunbeltz Izaola Python 5 06-01-2005 07:53 PM
sequence to FULLY test AVL tree implementation??? Nobody C++ 3 12-29-2004 07:22 PM
How do you do a REMOVAL in an AVL tree? Nobody C++ 1 12-26-2004 09:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57