Binary Tree Array
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Binary Tree Array

1. Elite Member
Join Date
May 2009
Posts
2,413

## Binary Tree Array

I've suggested a data structure I call a Binary Tree Array:

http://www.codeguru.com/forum/showthread.php?t=517046

I think it's novel but if anyone recognises it I would be interested in knowing.

Also I vaguely recognize the 'sweet problem' in the form of a 'Viking problem' or maybe a 'Roman soldier decimation problem' or something. The difference is that instead of passing a sweet a death sentence is passed and the question is where should you position yourself in the original lineup to be the last man standing walking away unharmed.

So maybe there's a known optimal solution?
Last edited by nuzzle; October 8th, 2011 at 07:39 PM.

2. Elite Member
Join Date
May 2009
Posts
2,413

## Re: Binary Tree Array

Further more to the BTA data structure in my previous post. An example. Say you have an array with 8 elements. Initially the binary tree would look like this,

8
4 4
2 2 2 2
1 1 1 1 1 1 1 1

The numbers reflect the number of array elements below each node of the tree. To remove an array element, say the fifth, you set the corresponding 1 in the leaf row to 0 and adjust all parent nodes accordingly, like

7
4 3
2 2 1 2
1 1 1 1 0 1 1 1

This makes array element removal an O(log N) operation.

To move M elements forward in the array you walk upwards in the tree from the current element until you find a node where the right child has M elements or more. Then from there you follow the path downwards that results in skipping M elements. You end up M elements away from the current element. Moving M elements backward is analogical. So moving M elements away from the current position is an O(log N) operation.

The BTA data structure allows the 'sweet problem' to be solved with O(N * log N) complexity. A solution may look like this,

1
1 0
0 1 0 0
0 0 0 1 0 0 0 0

The root node indicates there's just one element left in the array. Following the path of ones down from the root to the leaf level reveals the fourth child is the winner.
Last edited by nuzzle; October 20th, 2011 at 03:53 AM.

#### Posting Permissions

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

Click Here to Expand Forum to Full Width

This a Codeguru.com survey!

On-Demand Webinars (sponsored)