【发布时间】:2015-08-14 16:38:28
【问题描述】:
生成二叉树后如何为每个节点设置索引?
(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 我需要在树完全创建后完成。
-
可以利用层序遍历。
标签: java recursion tree binary-tree