小编use*_*602的帖子

将2-3-4树转换为红黑树

我正在尝试将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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我假设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 …
Run Code Online (Sandbox Code Playgroud)

java tree red-black-tree 2-3-4-tree

5
推荐指数
1
解决办法
8134
查看次数

在lisp中的树遍历

我正试图在lisp中遍历一棵树并打印出所有父子关系.这是我的输入:(5(3(4(1))(g)(9(6)))(n(8(0))(q)(7)(b(f(c(a))) )))我试图让它打印出以下内容:

5>3

5>n

3>4

3>g

3>9

4>1

9>6

n>8

n>q

n>7

n>b

8>0

b>f

f>c

c>a
Run Code Online (Sandbox Code Playgroud)

我目前的代码如下:

(defun par-child-print (l)
    (print l)
    (cond ((not (null (caadr l))) 
    (print "start")
    (print (car l)) 
    (print ">") 
    (print (caadr l)) 
    (print "end")
    (cond ((not (atom (car l))) (when (not (eq (car l) NIL)) (par-child-print (car l)))));

    (when (not (eq (cdr l) NIL)) (par-child-print (cdr l)))

    )
(t 
)));
Run Code Online (Sandbox Code Playgroud)

问题是我的输出有时只打印父级(并且它不会通过整个树).有任何想法吗?

我也有这个通过整个树,但甚至没有试图跟踪父母:

(defun atom-print (l)
(print l)
(cond ((atom l) (print l));
(t 
(when …
Run Code Online (Sandbox Code Playgroud)

lisp recursion

1
推荐指数
1
解决办法
1070
查看次数

标签 统计

2-3-4-tree ×1

java ×1

lisp ×1

recursion ×1

red-black-tree ×1

tree ×1