Java 树与图的递归

Java 树与图的递归,java,recursion,Java,Recursion,有人能给我举一些在高级数据结构上使用递归的例子吗。我今天有考试。我更希望这个例子比斐波那契或阶乘例子更难,但比河内塔问题更容易。对于如何使用递归操作数据结构的任何建议,我们将不胜感激。树的递归示例: void action(Node root){ if(root.left!=null)action(root.left); if(root.right!=null)action(root.right); //current node action code here } 有

有人能给我举一些在高级数据结构上使用递归的例子吗。我今天有考试。我更希望这个例子比斐波那契或阶乘例子更难,但比河内塔问题更容易。对于如何使用递归操作数据结构的任何建议,我们将不胜感激。

树的递归示例:

void action(Node root){
    if(root.left!=null)action(root.left);
    if(root.right!=null)action(root.right);
    //current node action code here
}
有一些迭代函数,它们有更多的代码,但对系统的堆栈更友好。根据系统的大小,选择其中一些。如果您正在使用大型结构并根据速度进行工作,那么迭代会更好

看看这些:还有


图形也是如此,只是您有更多的输出分支,这使得迭代方法更好。

这些是使用链接结构实现的
二进制树的访问示例

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()+" ");
}
publicstaticvoidprintinorder(LinkedBTree)抛出EmptyTreeException{
如果(t!=null&&!t.isEmpty())
printInorder((BTPosition)(t.root());
系统输出打印(“\n”);
}
专用静态无效打印顺序(BTV位置){
如果(v.getLeft()!=null)printInorder(v.getLeft());
System.out.print(v.element()+);
如果(v.getRight()!=null)printInorder(v.getRight());
}
公共静态void printproorder(LinkedBTree t)引发EmptyTreeException{
如果(t!=null&&!t.isEmpty())
printporeorder((BTPosition)(t.root());
系统输出打印(“\n”);
}
专用静态无效打印预订单(BTV位置){
System.out.print(v.element()+);
如果(v.getLeft()!=null)printPreorder(v.getLeft());
如果(v.getRight()!=null)打印预订单(v.getRight());
}
公共静态void printPostorder(LinkedBTree t)引发EmptyTreeException{
如果(t!=null&&!t.isEmpty())
printPostorder((BTPosition)(t.root());
系统输出打印(“\n”);
}
专用静态无效打印PostOrder(BTV位置){
如果(v.getLeft()!=null)printPostorder(v.getLeft());
如果(v.getRight()!=null)printPostorder(v.getRight());
System.out.print(v.element()+);
}

深度优先遍历:

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);
     }
}
最后,是一个计算baseexp的简单python示例(虽然是python,但应该很容易理解):

def POWEREC(基本,经验): 如果exp<0且基数!=0: 返回功率(1.0/base,-exp) 如果exp==0: 返回1 如果exp==1: 返回基地 如果exp%2==0: 返回功率(基本*基本,exp/2) 其他: 返回基地*powrec(基地*base,exp/2)
你必须更具体一些。如果您想在一夜之间学习如何递归地搜索/删除树/图中的节点,那么我会说祝您好运(这是可能的),但您可能会得到一本关于算法和数据结构的好书。一个选择是Allen Weiss的优秀著作《Java中的算法和数据结构》“你需要一个书呆子。你试过搜索“互联网”吗?”?
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);
        }
    }
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));
        }
    }
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)