我正在尝试将2-3-4树转换为java中的红黑树,但我很难搞清楚它。
我写了这两个基本类如下,以使问题简单明了,但无法弄清楚从哪里开始。
public class TwoThreeFour<K> {
public List<K> keys;
public List<TwoThreeFour<K>> children;
}
public class RedBlack<K> {
public K key;
public boolean isBlack;
public RedBlack<K> left,right;
public RedBlack<K key, boolean isBlack, RedBlack<K> left, RedBlack<K> right){
this.key = key; this.isBlack = isBlack; this.left = left; this.right = right;
}
}
我假设2-3-4树是有效的,并且在调用方法时想要返回一棵红黑树。
我也试过以下代码而没有运气:
public convert(TwoThreeFour<K> tTF){
if (ttf.keys.size() == 3)
RedBlack<K> node = RedBlack<ttf.keys[1], true, RedBlack<ttf.keys[0], false, /* not sure what to put here for left */, /* not sure what to put here for right */), RedBlack<ttf.keys[2], false, /* not sure what to put here for left */, /* not sure what to put here for right */)
等。 for keys.size()== 2,1 ...
我知道它必须在理论上是递归的,但我很难搞清楚它。有什么想法吗?
答案 0 :(得分:11)