2015-05-04 57 views
1

如何写我的二叉树实现正确的序法?序在Java中(数组实现)在二叉树方法

这是我测试的尝试:

class Main { 
    public static void main(String[] args) { 
     BinaryTree myTree = new BinaryTree(); 
     myTree.inorder(0); 
    } 
} 

public class BinaryTree { 
    char[] tree = {'k', 'q', 'r', 'g', 'e', 'i', 'y', 'p', 'l', 'b', 'x', 'm', 'g', 't', 'u', 'v', 'z'}; 
    public void inorder(int node) { 
     if(node < tree.length) { 
      inorder((node * 2)); 
      System.out.print(tree[node] + " "); 
      inorder(((node * 2) + 1)); 
     } 
    } 
} 
+1

爱帮忙你在这里做什么 –

+0

你可以发布你的问题到底是什么吗?怎么了?预期产出是多少? – JNYRanger

回答

1

myTree.inorder(0); //参数:0

序((节点* 2)); //节点= 0,节点* 2 = 0,

因此,参数将继续成为零是一个无限循环。

public class BinaryTree { 
    char[] tree = {'k', 'q', 'r', 'g', 'e', 'i', 'y', 'p', 'l', 'b', 'x', 'm', 'g', 't', 'u', 'v', 'z'}; 
    public void inorder(int node) { 
     if(node < tree.length) { 
      inorder((node * 2) + 1); 
      System.out.print(tree[node] + " "); 
      inorder(((node * 2) + 2)); 
     } 
    } 


    public static void main(String[] args) { 
     BinaryTree tree = new BinaryTree(); 
     tree.inorder(0); 
    } 
} 
+0

哦,谢谢,我不是一个聪明的人... – Bayer