如何在前序遍历中打印 AVL 树的条目

How to print entries of AVLTree in Pre-order Traversal

我正在尝试使用先序遍历打印存储在 AVL 树中的对象,程序运行良好但它不打印前序遍历它只是按照对象插入树中的顺序打印对象。有人可以帮我吗?

private AVLNode<AnyType> printPreorder(AVLNode<AnyType> t) {
        if (t == null)
            return null;

        /* first print data of node */
        System.out.print(t.element + " ");

        /* then recur on left sutree */
        printPreorder(t.left);

        /* now recur on right subtree */
        printPreorder(t.right);
        return null;

    }


    public AVLNode<AnyType> print() {
        return printPreorder(root);
    }

不要包含最后一个 return 语句,使用此代码:

private Void printPreorder(AVLNode<AnyType> t) 
{   
    if (t != null)
    {
        System.out.print(t.element + " ");
        printPreorder(t.left);
        printPreorder(t.right);
    }
}

public Void print() 
{
        printPreorder(root);
}

请告诉我是否有帮助。