如何编写自己的数组和列表

发布于 2024-12-16 15:10:20 字数 96 浏览 0 评论 0原文

我必须对几种不同类型的二叉树进行编程。但是,我不允许使用数组或集合等实用程序。如果需要的话,建议构建我自己的阵列。问题是,我什至不知道从哪里开始。比如说,我如何构建一个二维数组?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

固执像三岁 2024-12-23 15:10:20

您必须通过创建对象来手动创建链接列表或树,每个对象都包含指向列表或树中下一个对象的指针。这是我上学时在数据结构课上做过很多次的练习。了解如何通过插入和删除来保持列表或树完整是一个有用的练习。

public class ListNode<T> {
  private T payload;
  private ListNode<T> nextNode;
}

public class TreeNode<T> {
  private T payload;
  private TreeNode<T> leftChild, rightChild;
}

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.

public class ListNode<T> {
  private T payload;
  private ListNode<T> nextNode;
}

public class TreeNode<T> {
  private T payload;
  private TreeNode<T> leftChild, rightChild;
}
小耗子 2024-12-23 15:10:20

您不需要数组来构建树。如果您在维基百科上找不到所需的内容,有几本关于算法和数据结构的书籍:

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.)

( inspiration for the actual question, that you don't know you are
asking:

Something like this is the core data structure for a binary search
tree...

class Node {
     Object value;
     Node left;
     Node right;
} )
行至春深 2024-12-23 15:10:20

编辑:好吧,每个人都提到了列表,但我们不要忘记,您也可以在数组上实现二叉树,就像实现堆一样。所以我们有这样的东西:

public class BinaryTree {
    private int[] tree; // assuming each node holds an integer.
    private int nodeCount;

    public BinaryTree (int nodes) {
         tree = new int[nodes * 2];
         nodeCount = nodes;
    }

    public int getRoot() {
         return tree[0];
    }

    private int getPositionOfNode(int value) {
         for(int i = 0; i < nodeCount; i++) {
             if(tree[i] == value) {
                 return i;
             }
         }
         return -1;
    }

    public int getLeftChildOfNode(int node) {
         int pos = getPositionOfNode(node);
         if(pos != -1) {
              return tree[pos * 2];
         }
         return pos;
    }

    public int getRightChildOfNode(int node) {
         int pos = getPositionOfNode(node);
         if(pos != -1) {
              return tree[pos * 2 + 1];
         }
         return pos;
    }

    public int getParentOfNode(int node) {
         int pos = getPositionOfNode(node);
         if(pos != -1) {
              return tree[pos / 2];
         }
         return pos;
    }
}

在这个结构中,如果一个节点位于位置 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:

public class BinaryTree {
    private int[] tree; // assuming each node holds an integer.
    private int nodeCount;

    public BinaryTree (int nodes) {
         tree = new int[nodes * 2];
         nodeCount = nodes;
    }

    public int getRoot() {
         return tree[0];
    }

    private int getPositionOfNode(int value) {
         for(int i = 0; i < nodeCount; i++) {
             if(tree[i] == value) {
                 return i;
             }
         }
         return -1;
    }

    public int getLeftChildOfNode(int node) {
         int pos = getPositionOfNode(node);
         if(pos != -1) {
              return tree[pos * 2];
         }
         return pos;
    }

    public int getRightChildOfNode(int node) {
         int pos = getPositionOfNode(node);
         if(pos != -1) {
              return tree[pos * 2 + 1];
         }
         return pos;
    }

    public int getParentOfNode(int node) {
         int pos = getPositionOfNode(node);
         if(pos != -1) {
              return tree[pos / 2];
         }
         return pos;
    }
}

In this structure, if a node is at position i, its children will be at positions 2*i and 2*i+1.

魔法少女 2024-12-23 15:10:20
public class TwoDArray {
   private Object[] values;
   public TwoDArray(int n, int m) {
       values = new Object[n * m];
   }
   public void set(int i, int j, Object x) { ... }
   public Object get(int i, int j) { ... }
}
public class BinTree {
   private BinTree left;
   private BinTree right;
   private Comparable value;
   public void insert(Comparable x) { ... }
}

像这样的东西吗?
应该没那么难。

public class TwoDArray {
   private Object[] values;
   public TwoDArray(int n, int m) {
       values = new Object[n * m];
   }
   public void set(int i, int j, Object x) { ... }
   public Object get(int i, int j) { ... }
}
public class BinTree {
   private BinTree left;
   private BinTree right;
   private Comparable value;
   public void insert(Comparable x) { ... }
}

Something like this?
Should not be that difficult.

末骤雨初歇 2024-12-23 15:10:20

首先,我假设您甚至被禁止使用 Java 的原始数组类型。在这种情况下,您将不得不回到最基础的知识并开始自己管理内存。您需要首先分配适当大小的字节数组或 ByteBuffer。然后,将使用 C 语言中简单的指针数学来处理对该数据块的访问。

16 个 int 的一维数组示例:

byte[] mem = new byte[16 * 4] ; // Ints are 4 bytes long

// Write a value to the 8th element of our "array"
index = 8 ;
int value = 31415926 ;
mem[4 * index + 0] = (byte)( value >> 24 ) ;
mem[4 * index + 1] = (byte)( ( value << 8 ) >> 24 ) ;
mem[4 * index + 2] = (byte)( ( value << 16 ) >> 24 ) ;
mem[4 * index + 3] = (byte)( ( value << 24 ) >> 24 ) ;

通过反转该过程并计算存储的 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:

byte[] mem = new byte[16 * 4] ; // Ints are 4 bytes long

// Write a value to the 8th element of our "array"
index = 8 ;
int value = 31415926 ;
mem[4 * index + 0] = (byte)( value >> 24 ) ;
mem[4 * index + 1] = (byte)( ( value << 8 ) >> 24 ) ;
mem[4 * index + 2] = (byte)( ( value << 16 ) >> 24 ) ;
mem[4 * index + 3] = (byte)( ( value << 24 ) >> 24 ) ;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文