我正在尝试将一棵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 */)
对于keys.size() == 2, 1等等,请参考以下内容:
我知道理论上必须使用递归,但我难以理解。有什么想法吗?