如何编写自己的数组和列表
我必须对几种不同类型的二叉树进行编程。但是,我不允许使用数组或集合等实用程序。如果需要的话,建议构建我自己的阵列。问题是,我什至不知道从哪里开始。比如说,我如何构建一个二维数组?
I have to program several different types of binary trees. However, I'm not allowed to use utils such as arrays or collections. It is suggested to build my own array, should there be a need for it. The problem is, I don't even know where to start with this. How could I build, say, a 2D array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须通过创建对象来手动创建链接列表或树,每个对象都包含指向列表或树中下一个对象的指针。这是我上学时在数据结构课上做过很多次的练习。了解如何通过插入和删除来保持列表或树完整是一个有用的练习。
You will have to manually create a linked list or tree through the creation of objects which each contain a pointer to the next object in the list or tree. This is an exercise that we did many times in our data structures class when I was in school. Understanding how to keep the list or tree intact through inserts and deletes is a useful exercise.
您不需要数组来构建树。如果您在维基百科上找不到所需的内容,有几本关于算法和数据结构的书籍:
http:// en.wikipedia.org/wiki/Binary_tree
或 http://en.wikipedia.org/wiki/List_of_data_structs
(问题的答案:对于二维数组,创建一个一维数组,并将一维数组存储在一个一维数组中。这是可能的通过创建自己的链表实现来模拟内置数组,但这既不高效,也不符合您的老师的想法。)
You won't need arrays for building a tree. There are several books about algorithms and data structures, if you will not find what you need on wikipedia:
http://en.wikipedia.org/wiki/Binary_tree
or http://en.wikipedia.org/wiki/List_of_data_structures
(answer to question: For a 2d array, create an 1d array, and store 1d arrays in a 1d array. It is possible to emulate built in arrays by creating your own linked list implementation. But it's neither effient nor what your teacher is having in mind.)
带有链表。这里的代码: http://www.roseindia.net/java/jdk6/LinkedListExample.shtml
with a linked list. here the code: http://www.roseindia.net/java/jdk6/LinkedListExample.shtml
编辑:好吧,每个人都提到了列表,但我们不要忘记,您也可以在数组上实现二叉树,就像实现堆一样。所以我们有这样的东西:
在这个结构中,如果一个节点位于位置 i,它的子节点将位于位置 2*i 和 2*i+1。
Edit: Ok, everyone mentioned lists, but let's not forget that you can implement binary trees also on arrays, like heaps are implemented. So we have something like:
In this structure, if a node is at position i, its children will be at positions 2*i and 2*i+1.
像这样的东西吗?
应该没那么难。
Something like this?
Should not be that difficult.
首先,我假设您甚至被禁止使用 Java 的原始数组类型。在这种情况下,您将不得不回到最基础的知识并开始自己管理内存。您需要首先分配适当大小的字节数组或 ByteBuffer。然后,将使用 C 语言中简单的指针数学来处理对该数据块的访问。
16 个 int 的一维数组示例:
通过反转该过程并计算存储的 int 值来读取值。
注意:使用 ByteBuffer 设置和检索值的过程更容易,因为它是获取原始 Java 类型并将其放入字节数组的方法。
I'll start by assuming that you have been forbidden from using even Java's primitive array types. In that case, you would have to go back to the very basics and start managing memory yourself. You would need to start by allocating a byte array or ByteBuffer of an appropriate size. Access into that block of data would then be handled using what in C would be straightforward pointer math.
Example of a 1D array of 16 ints:
Reading a value out would be done be reversing the process and calculating the stored int value.
Note: The process of setting and retrieving values is easier using a ByteBuffer as it methods for getting and putting the primitive Java types into the byte array.