树和图的建议递归

时间:2013-11-07 20:47:17

标签: java recursion

有些人可以提供在高级数据结构上使用递归的示例。我今天参加考试。我希望这个例子比斐波纳契或因子例子更难,但比河内问题的塔更容易。有关如何使用递归操作数据结构的任何建议都表示赞赏。

3 个答案:

答案 0 :(得分:1)

树的递归示例:

void action(Node root){
    if(root.left!=null)action(root.left);
    if(root.right!=null)action(root.right);
    //current node action code here
}

有迭代函数,它们具有更多代码,但对您的系统更友好。根据系统的大小,选择其中一些。如果你正在使用大型结构制作某些东西并且取决于速度,那么迭代会好得多。

查看以下内容:link1Iterative approach

与图形相同,除了你有更多的输出分支,这使得迭代方法更好。

答案 1 :(得分:1)

这些是使用链接结构实现的BinaryTrees的示例访问。

public static <E> void printInorder(LinkedBTree<E> t) throws EmptyTreeException{
    if(t != null && !t.isEmpty())
        printInorder((BTPosition<E>)(t.root()));
    System.out.print("\n");
}

private static <E> void printInorder(BTPosition<E> v){
    if(v.getLeft() != null) printInorder(v.getLeft());
    System.out.print(v.element()+" ");
    if(v.getRight() != null) printInorder(v.getRight());
}

public static <E> void printPreorder(LinkedBTree<E> t) throws EmptyTreeException{
    if(t != null && !t.isEmpty())
        printPreorder((BTPosition<E>)(t.root()));
    System.out.print("\n");
}

private static <E> void printPreorder(BTPosition<E> v){
    System.out.print(v.element()+" ");
    if(v.getLeft() != null) printPreorder(v.getLeft());
    if(v.getRight() != null) printPreorder(v.getRight());
}

public static <E> void printPostorder(LinkedBTree<E> t) throws EmptyTreeException{
    if(t != null && !t.isEmpty())
        printPostorder((BTPosition<E>)(t.root()));
    System.out.print("\n");
}

private static <E> void printPostorder(BTPosition<E> v){
    if(v.getLeft() != null) printPostorder(v.getLeft());
    if(v.getRight() != null) printPostorder(v.getRight());
    System.out.print(v.element()+" ");
}

答案 2 :(得分:1)

深度优先遍历:

public void preOrder(Node<T> root)
{
     if (root != null)
     {
         System.out.println(root);
         preOrder(root.leftChild);
         preOrder(root.rightChild);
     }
}


public void inOrder(Node<T> root)
{
     if (root != null)
     {
         inOrder(root.leftChild);
         System.out.println(root);
         inOrder(root.rightChild);
     }
}


public void postOrder(Node<T> root)
{
     if (root != null)
     {
         postOrder(root.leftChild);
         postOrder(root.rightChild);
         System.out.println(root);
     }
}

这是检查平衡表达式的简单方法:

private String open  = "([<{";
private String close = ")]>}";

private boolean isOpen(char ch)
{
    return open.indexOf(ch) == -1 ? false : true;
}

private boolean isClosed(char ch)
{
    return close.indexOf(ch) == -1 ? false : true;
}

private boolean balanced(String sequence)
{
    if (sequence != null && sequence.length() > 0)
        return isBalanced(sequence, "");
    else 
        return true;
}

private boolean matches(char fromSeq, char fromStack)
{
    return open.indexOf(fromStack) == close.indexOf(fromSeq);
}


private boolean isBalanced(String seq, String stack)
{
    if (seq.length() == 0 )
    {
        return stack.length() == 0;
    }
    else
    {
        char first = seq.charAt(0);

        if (isOpen(first))
        {
            return isBalanced(seq.substring(1), first + stack);
        }
        else if (isClosed(first))
        {
            return stack.length() != 0 && matches(first, stack.charAt(0)) && isBalanced(seq.substring(1), stack.substring(1));
        }
        else
        {
            return isBalanced(seq.substring(1), stack);
        }
    }

这是获取某些域的所有排列的简单方法,即您将调用:
permute(3,"01", "");收益:

  

000 001 010 011 100 101 110 111

public void permute(int length, String domain, String result)
    {
        if (length == 0)
        {
            System.out.println(result);
        }
        else
        {
            for (int i = 0; i < domain.length(); i++)
                permute(length-1, domain, result + domain.charAt(i));
        }
    }

最后,一个简单的python示例用于计算base exp (虽然它在python中,它应该很容易理解):

def powrec(base, exp):
    if exp < 0 and base != 0:
        return powrec(1.0/base, -exp)

    if exp == 0:
        return 1

    if exp == 1:
        return base

    if exp % 2 == 0:
        return powrec(base*base, exp/2)

    else:
        return base*powrec(base*base, exp/2)