2

I am trying to solve this problem where a new joining peer will be given an index [0,1,2, ... n-1] based on how many peer objects already exist (e.g. 8 exist -> new peer will get index 8).

I want to add these peer objects into a binary tree based on their index. For example, peer 0 joins and it will be the root, then peer 1 & peer 2 will be peer 0's left and right children.

I only need the binary tree to follow the rule that it should have two children.

Here's an example:

    0 

   / \

  1   2

 / \ / \

3  4 5  6

My problem is that I am unsure of how to actually do this insertion to keep the 2 children rule. At first I assumed a normal BST insertion rule would work, but once I actually coded that up, I realized the problem of the pivot/key - I am inserting based on the index. Everything would just become a right child

I am really stuck on this but I think the solution should be a trivial one that I am just unable to see. Any advice?

Edit: Thank you for the help! I think I figured out something that will meet my needs so I'll leave it here. I will have an implicit binary tree structure. Peers that join up will get put into a priority queue based on their index. This will signify whether they can have children assigned to them & a peer will be removed from this queue once it has 2 children

JerryFox
  • 615
  • 2
  • 13
  • 25
  • If you want to add based on the index you've already given it then you don't want a binary tree, you want an array. – Dave Zych Aug 07 '14 at 17:00
  • @DaveZych You can index a binary tree... you can even use an array to hold a binary tree... – progrenhard Aug 07 '14 at 17:01
  • @progenhard I'm well aware of that. But he's assigning an index to each item prior to adding it to the BT, and wants to use that index when it's added to the BT. He's essentially building a linked list. Just add the items to the BT and let it build itself. – Dave Zych Aug 07 '14 at 17:04
  • @DaveZych I originally did have an array structure but potentially I'll have up to 10k peers. With deletion and addition of new peers, this would heavily affect performance – JerryFox Aug 07 '14 at 17:11
  • @JerryFox What's your goal after they're added to whatever data structure you end up with? Do you need to search them? Loop through every one and perform some action? – Dave Zych Aug 07 '14 at 17:16
  • Having a length-10000 array is totally fine. If you are assigning a user id to each user you'll be using that as the index to an array, so your lookup will be constant-time. Alternatively, if you won't be assigning user ids sequentially or want to use some other non-integer datatype as a unique identifier, then use a hash table – Ryan Chipman Aug 07 '14 at 17:17
  • I need to be able to access one in the case where one is removed. For example if 2 get deleted, 5 and 6 would be in a sense re-inserted into the tree with new index, with an updated parent – JerryFox Aug 07 '14 at 17:17
  • @DaveZych But even if you needed to search them, there's no case where you'd need to search by a uuid. Like I said in my answer, the only time a data structure like this would be useful would be if you were using some other value like a score or age as a key – Ryan Chipman Aug 07 '14 at 17:18
  • @RyanChipman the issue with using a hash table is that say peer 2 is removed, everything below it would need to be updated and "re-inserted" into the tree structure so the hash table doesnt give much – JerryFox Aug 07 '14 at 17:19
  • @JerryFox this sounds like you are looking at more of a linked-list kind of thing. Why do you need this notion of "parent"? – Ryan Chipman Aug 07 '14 at 17:19
  • @JerryFox What is it that you actually need to **do** with these users. Give us an idea of what sort of operations you plan on doing that you think you need a BST for. Because right now I don't see what you are getting from using a BST. – Ryan Chipman Aug 07 '14 at 17:21
  • @RyanChipman A parent sends information to 2 child and likewise the 2 children also send info to the parent – JerryFox Aug 07 '14 at 17:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58899/discussion-between-ryan-chipman-and-jerryfox). – Ryan Chipman Aug 07 '14 at 17:22
  • I think I figured out something that will meet my needs so I'll leave it here. I will have an implicit binary tree structure. Peers that join up with get put into a priority queue based on their index. This will signify whether they can have children assigned to them & a peer will be removed from this queue once it has 2 children. – JerryFox Aug 07 '14 at 17:24

2 Answers2

2

A few things that you want to consider:

Why do you want a BST?

BSTs are, as the name implies, primarily for searching. But if you are assigning every new user that joins a unique identifier, then you don't need to use a BST to search for them, because you can access them from an array by index.

A BST would be more useful if, say, each user played a game and earned a particular score. To organize the users in a data structure that would render them easily searchable/organizable by score, you might insert a player into the BST with their score as the key when they finished the game. But for unique identifiers like this, there is no reason fo use a BST. In fact, the data structure that you show there is not a BST. The BST would look like this:

   3 

  / \

 1   5

/ \ / \

0 2 4  6

Is another data structure more appropriate?

If you have gotten a better sense of why a BST is not a useful structure for organizing user IDs, then you should next think about what is is you were actually trying to do. If you were just trying to store all the users in a data structure, a list (array) is totally fine, where the index of the list corresponds to the user id.

If you are instead looking to add some sort of grouping to these users, consider using a hash table. For example, if you wanted to be able to look up a user's friends, you would create a hash table where a user id (key) maps to a list of friends' user ids (value).

Hopefully this has been helpful. If there is any more that I can do to help or if I have not fully understood what you are trying to accomplish, just let me know


UPDATE

So based on the comments above, it seems your confusion is on the distinction between a binary tree and a BST. A binary tree is any tree where each node has <= 2 children, whereas a Binary search tree imposes additional constraints on the values of the nodes' keys. The binary tree structure is what you want, but you don't need it for searching, nor do you want to compare those values.

Ryan Chipman
  • 393
  • 2
  • 5
  • 15
  • I actually dont want a BST but just a binary tree. I realize this is probably my issue though – JerryFox Aug 07 '14 at 17:15
  • 1
    @ColonelThirtyTwo that's not true. A BST is a binary tree, but a binary tree is not a BST. A binary tree is a structure where each node has at most 2 children. A BST is where all left children are less than the current node and all right children are greater than the current node. – Dave Zych Aug 07 '14 at 17:18
  • sorry I know the difference but I did not realize I left in one reference to BST – JerryFox Aug 07 '14 at 17:29
0

For any given index, i, the parent node would have got the index (i + 1 >> 1) - 1 and the children nodes the indices (i << 1) + 1 and i + 1 << 1. I don't know if this is of any help, as I'm unsure of the purpose of your endeavour. But this at least means that you could save all your peers in a plain array and use that plain array as a binary tree structure by accessing a node's children by just using the node's index.

Fors
  • 186
  • 6