Java TreeMap排序选项?

kyl*_*lex 5 java binary-tree red-black-tree

我被告知java类TreeMap使用RB树的实现.如果是这种情况,如何在TreeMap上进行顺序,预订和后序树步行?

或者这不可能吗?

Bil*_*ard 6

您无法使用Collections库中实现的TreeMap执行此操作.这里是Java中的红黑树的实现,你可以看一下.查看printTree()方法,了解它们如何按排序顺序遍历树.

/**
 * Print all items.
 */
public void printTree( ) {
    printTree( header.right );
}

/**
 * Internal method to print a subtree in sorted order.
 * @param t the node that roots the tree.
 */
private void printTree( RedBlackNode t ) {
    if( t != nullNode ) {
        printTree( t.left );
        System.out.println( t.element );
        printTree( t.right );
    }
}
Run Code Online (Sandbox Code Playgroud)

从那可能你可以编写自己的方法来遍历所有三个命令中的树.