AVL Tree element index

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: AVL Tree element index

1. John Guest

## AVL Tree element index

I'm having trouble trying to find the index of a certain element in an AVL
tree. Can someone give me a hint on how I could return the index of an element
or search for an element at a certain index?

For example, say I had a tree

26
/ \
/ \
6 48
/ \ / \
2 9 35 50
/ \ / \ \
1 15 29 37 56

Element 1 would be the number 1, element 3 would be the number 6. Likewise,
element 12 would be 56 and element 9 would be 37.
Imagine these numbers as if they were in a sorted array, and find the index's
would be the same that way.

Each node has only a right and left pointer.
Thanks.

2. Ako Guest

## Re: AVL Tree element index

The best reference is the java api documentations and the tutorials on sun's
website, it is very likely that you will find an answer to your question
there but if you couldnt you can always define your own representation of
the tree structure you whant to implement.

Good luck

Ako

"John" <ieceo17@aol.com> wrote:
>
>I'm having trouble trying to find the index of a certain element in an AVL
>tree. Can someone give me a hint on how I could return the index of an element
>or search for an element at a certain index?
>
>For example, say I had a tree
>
> 26
> / \
> / \
> 6 48
> / \ / \
> 2 9 35 50
> / \ / \ \
>1 15 29 37 56
>
>Element 1 would be the number 1, element 3 would be the number 6. Likewise,
>element 12 would be 56 and element 9 would be 37.
>Imagine these numbers as if they were in a sorted array, and find the index's
>would be the same that way.
>
>Each node has only a right and left pointer.
>Thanks.

3. Paul Clapham Guest

## Re: AVL Tree element index

You are ordering your elements according to what's called a depth-first
traversal of the tree. If you use DefaultMutableTreeNode (from package
javax.swing.tree) to arrange your elements in a tree structure, you will
find that it actually has a method called depthFirstEnumeration.

PC2

"John" <ieceo17@aol.com> wrote in message news:3aeb2a8a\$1@news.devx.com...
>
> I'm having trouble trying to find the index of a certain element in an AVL
> tree. Can someone give me a hint on how I could return the index of an

element
> or search for an element at a certain index?
>
> For example, say I had a tree
>
> 26
> / \
> / \
> 6 48
> / \ / \
> 2 9 35 50
> / \ / \ \
> 1 15 29 37 56
>
> Element 1 would be the number 1, element 3 would be the number 6.

Likewise,
> element 12 would be 56 and element 9 would be 37.
> Imagine these numbers as if they were in a sorted array, and find the

index's
> would be the same that way.
>
> Each node has only a right and left pointer.
> Thanks.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise