简体   繁体   English

存储二进制路径的最有效方式(例如通过二叉树)

[英]Most efficient way of storing a binary path (e.g. through a binary tree)

I am trying to store a binary path as efficient as possible (in terms of using as little memory as possible/necessary).我正在尝试尽可能高效地存储二进制路径(就使用尽可能少的内存而言/必要的)。 A path that leads to a specific node in a linked binary tree (beginning at the root), for example -- An easy way to store this would be to store the nodes (or their values) themselves.例如,通向链接二叉树中特定节点的路径(从根开始)——存储它的一种简单方法是存储节点(或其值)本身。

But this is not efficient, nor is it elegant.但这并不高效,也不优雅。 If I want to apply the same path on a different BinaryTree, this does not work.如果我想在不同的 BinaryTree 上应用相同的路径,这不起作用。

So You could save just a sequence, eg a string, like "lrllrrr" where l means left branch, r means right branch.所以你可以只保存一个序列,例如一个字符串,比如"lrllrrr" ,其中l表示左分支, r表示右分支。 But storing this information in a string is still not very efficient.但是将这些信息存储在字符串中仍然不是很有效。

This can be improved by using an array of booleans.这可以通过使用布尔数组来改进。 But I don't know how those are handled in Java in the backgrund.但我不知道在后台如何在 Java 中处理这些。 Also, I would like to avoid fixed-sized Strings/Arrays.另外,我想避免固定大小的字符串/数组。

My next idea was to directly store the path in a binary integer like 0100111 (reading left to right).我的下一个想法是直接将路径存储在一个二进制整数中,如0100111 (从左到右阅读)。 You could then add a 'move left' by using << and a 'move right' by using << , followed by ++ .然后,您可以使用<<添加“向左移动”,使用<<添加“向右移动<< ,后跟++ But this approach has three weaknesses:但是这种方法有三个弱点:

  1. In Java, there are no unsigned integers.在 Java 中,没有无符号整数。 I don't know much about bitwise operators in Java, so I am wondering about how to deal with the sign bit of a primitive int or long ?我不太了解 Java 中的按位运算符,所以我想知道如何处理原始intlong的符号位?
  2. The entry count of such a representation is limited by the number of binary digits that make up an integer (eg 63 for a long ).这种表示的条目计数受组成整数的二进制数字数量的限制(例如long为 63)。 That can probably be solved by using BigInteger .这可能可以通过使用BigInteger来解决。
  3. A path that starts with at least one 'left' entry cannot be stored correctly, because '0' Digits on the very left are ignored by default when dealing with integers.以至少一个 'left' 条目开头的路径无法正确存储,因为在处理整数时,默认情况下会忽略最左边的 '0' 数字。

So, is there any way in Java to store a specific series of bits so that i can read them individually without much effort?那么,Java 中是否有任何方法可以存储特定的一系列位,以便我可以毫不费力地单独读取它们? If something like this does already exist I'd like to read more about how it is immplemented.如果这样的东西已经存在,我想阅读更多关于它是如何实现的。 If there is no such thing in Java and it is too difficult or simply impossible, how would you approach that in another environment/ in another language?如果 Java 中没有这样的东西并且它太难或根本不可能,您将如何在另一种环境/另一种语言中处理它?

Thanks in advance.提前致谢。

EDIT : '0' Digits on the very left编辑:'0'最左边的数字

You can use a single integer to signify the path.您可以使用单个整数来表示路径。 The tree increases in size 1,2,4,8 etc树的大小增加 1,2,4,8 等

Also note that when talking about "smallest", keep in mind memory block size constraints, and also be aware that your binary tree is probably going to be much larger than your path representation.另请注意,在谈论“最小”时,请记住内存块大小限制,并且还要注意您的二叉树可能会比您的路径表示大得多。

0 1 2 3 4 5 6

摘自@RealSceptic 的评论: BitSet对我来说很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM