如何在生成二叉树后为每个节点设置索引?二叉树的唯一编号节点
(a) (1)
(x) (r) => (2) (3)
(o)(t)(t)(x) (4)(5)(6)(7)
因此,我可以在特定节点上使用诸如getIndex()
之类的调用来返回其索引。
我的树类:
public class BT<E>{
E value;
BT<E> left, right;
int Index;
public BT(E value)
{
this.value=value;
}
public BT (E value, BT left, BT right)
{
this.value = value;
this.left = left;
this.right = right;
}
如果在完全创建树之后再次遍历树,或者在创建树时第一遍期间尝试初始化此索引,那么您还可以吗? – NoseKnowsAll
简单地遍历你的树一层一层。 – talex
@NoseKnows所有我需要它完成树完全创建后完成。 – RK2015