Structuring data to represent a tree with squirrels

What I want to represent

Let’s assume I have several schemas of the following kind:

enter image description here

It is a tree on which positions of squirrels are represented by a blue dot. The number of bifurcations differ from one tree to another, the number of squirrels varies from one tree to another, the positions of the squirrels vary (and are on a continuous scale), all trees are not necessarily symmetric (unlike the picture) and the length of the branches differ (on the picture the length of the second branches are all the same, the lengths of the third branches are all the same. This is not necessarily always the case).

The problem

I am trying to describe these trees with numbers so that I can play with these numbers and easily calculate the distance between two squirrels (following the branches, not by jumping), calculate the distance between a squirrel and the ground (the bottom of the trunk) or calculate the density of squirrels per branch.

The angles between branches do not matter and the branches have no width (they are just lines). The info I need are the length of the branches (on a continuous scale), the positions of the squirrels (on a continuous scale) relative to their branch, the number of bifurcations and the positions of the branches relative to other branches (whether one branch is attached to another one).

My (very) poor solutions

One way to describe this tree is to digitalize it. I could make a big array with 0s everywhere there is nothing, 1s everywhere there is a branch and a 2 everywhere there is a squirrel (and a branch necessarily, no flying squirrel on the picture!). That would be a solution but I would very big arrays and it is really not very handy to infer things like the average distance between two squirrels. Moreover, we lose precision about the positions of the squirrels and the lengths of the branches

Another solution would be to have two sets of vectors and one set of points. The first set of vector describes the positions of each squirrel relative to the bottom-left corner, another set of vector defines the the length and direction (although we don’t directly need this info) of each branch and the set of points indicate where the starting points of the vectors describing the branches. It really doesn’t seem to be a good idea; it is too descriptive (too much info) and is not very handy for making the kind of calculations I need to perform afterward.

I feel like I focus too much on the picture and have a hard time realizing how the informations I need could be coded/stored. Can you help me with that?

1

Branches is fairly easy:

Root of tree is “1”.

First bifurcation results in branches “1×1” and “1×2” counting from the left.

So the top rightmost branch in your tree is labeled “1x2x2x2”.

This schema is quite useful for querying as you can get all the sub branches of say “1×2″ by a simple ” LIKE ‘1×2%’ ” in SQL.

Squirrel position is more problematic but I would suggest a simple distance from the beginning of the branch.

So you table would look like:

Branches:
      Branch    Length
      1         4
      1x1       3
      1x1x1     2
      1x1x1x1   1
      1x2       3
      ...................
      1x2x2     2
      1x2x2x1   1
      1x2x2x1   1

  Branch    Position   Name
  ------    --------   ----
  1         2          Fluffytail
  1         3          Nutkin
  1x1       1          Brighteyes
  1x1       2          Loopy
  1x1x1     1          Lefty
....................
  1x2x2     3          Jumper
  1x2x2x1   1          Topper
  1x2x2x2   1          Rightmost  

The classic way to store a tree in a database is to use a self-referential table. If you take James Anderson’s branch table and add an optional parentBranchID field, you are there. The only branch with a null parentBranchId will be the root node.

Similarly, read the whole table into memory using branch classes that have both a parentBranchID and a parentBranch instance pointer. The class will also need a collection of squirrels – this can be created (in Java) by an OR tool like myBatis or Hibernate.

Iterate over the list of classes, creating the parent pointer linkages, and populating a list of child branches in each branch. You now have an in-memory tree that can be traversed in both directions.

Finding the distance between 2 squirrels is likely a two-step process.

  1. Starting from one squirrel, work your way up the tree (towards the root node) until you find a node that is also a parent of the second squirrel.
  2. Write a branch member function like getDistanceTo(Squirrel squirrel). This is likely to be recursive, as if the squirrel is not on the branch, you need to try each child branch in turn. Call the function for each squirrel, and add the distances.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật