我正在尝试使用带有n个元素的二叉搜索树并将它们存储在arraylist中。目前,它们存储在arraylist中的基础是,树的根是元素1,(p =数组中父节点的索引)左边的子节点将在索引p * 2处,而右边的子节点将在在索引p * 2 + 1。
目前我尝试使用此代码执行此操作:
.view-table {
display: table;
}
.view-row {
display: table-row;
}
.view-row > div {
display: table-cell;
}
.view-row > div > * {
vertical-align: top;
}
img {
max-height: 100px;
}
.view-row > .with-img {
width: 1%; /* So that the cell adjusts to the width of its contents. */
}
我的逻辑出了什么问题?我怎么做这个工作? 目前如果我使用它,然后尝试打印输出形式的内容(这是arraylist的名称,它的基本大小为10,000)我得到:索引一返回为null,其余为“n”,这是我也初始化了每个元素。
谢谢!
答案 0 :(得分:0)
你总是使用固定值1来表示pos,这意味着你一次又一次地覆盖它。
考虑使用类似的东西:
public static void arraywriter(TreeNode<String> node, int pos) throws FileNotFoundException
{
outputform.set(pos, node.getElement());
pos = pos*2;
if(node.getLeft() != null) {
arraywriter(node.getLeft(), pos);
}
pos = pos+1;
if(node.getRight() != null) {
arraywriter(node.getRight(), pos);
}
}