2016-08-21 10 views
0

binary treeを作成するためにこのコードを作成しましたが、このコードはunbalanced binary treeを作成しているようです。ノードは、rightサブツリーrootでのみ取得しています。左のサブツリーの子ノードにアクセスしようとするとNull pointer exceptionが出ます。ノードがleft to rightから挿入されているbalanced binary treeを作成したいと思います。私はここで何をしているのですか?Javaを使用して平衡二分木を作成する際にエラーが発生しました

public class binTree { 

    public static class TreeNode{ 
     public int val; 
     public TreeNode left; 
     public TreeNode right; 

     public TreeNode(int val){ 
      this.val = val; 
      this.left = null; 
      this.right = null; 
     } 
    } 

    public TreeNode root; 

    public binTree(){ 
     this.root = null; 

    } 

    public void insert(int data){ 
     root = insert(root,data); 
    } 
    public TreeNode insert(TreeNode node,int data){ 

     if(node == null){ 
      node = new TreeNode(data); 
      //root = node; 
     } 
     else{ 
      if(node.left == null){ 
       node.left = insert(node.left,data); 
      } 
      else{ 
       node.right = insert(node.right,data); 
      } 
     } 
     return node; 

    } 


    public static void main(String args[]){ 
     binTree obj = new binTree(); 

     obj.insert(5); 
     obj.insert(11); 
     obj.insert(13); 
     obj.insert(1); 
     obj.insert(7); 
     obj.insert(21); 
     obj.insert(35); 
     System.out.println(obj.root.right.left.val); 
     System.out.println(obj.root.left.right.val); // this throws null pointer exception 
    } 

} 

答えて

0

あなたは、このような各ツリー・ノードですべてのサブツリーのための要素の数を格納する必要があります:

public class BinTree { 

    private TreeNode root; 

    public static class TreeNode { 

     public int val; 
     public int elements = 0; 
     public TreeNode left; 
     public TreeNode right; 

     public TreeNode(int val) { 
      this.val = val; 
      this.left = null; 
      this.right = null; 
     } 
    } 

    public BinTree() { 
     this.root = null; 
    } 

    public void insert(int data) { 
     root = insert(root, data); 
    } 

    private static int height(TreeNode node) { 
     int result = 0; 
     if (node != null) { 
      result++; 
      int total = node.elements; 
      int heightElements = 2; 
      while (total > heightElements) { 
       total -= heightElements; 
       heightElements *= 2; 
       result++; 
      } 
     } 
     return result; 
    } 

    public TreeNode insert(TreeNode node, int data) { 
     if (node == null) { 
      node = new TreeNode(data); 
     } else if (height(node.left) == height(node.right)) { 
      node.left = insert(node.left, data); 
     } else { 
      node.right = insert(node.right, data); 
     } 
     node.elements++; 
     return node; 
    } 

    public static void main(String args[]) { 
     BinTree obj = new BinTree(); 
     obj.insert(5); 
     obj.insert(11); 
     obj.insert(13); 
     obj.insert(1); 
     obj.insert(7); 
     obj.insert(21); 
     obj.insert(35); 

     System.out.println(obj.root.val); 
     System.out.println(obj.root.left.val); 
     System.out.println(obj.root.right.val); 
     System.out.println(obj.root.left.left.val); 
     System.out.println(obj.root.left.right.val); 
     System.out.println(obj.root.right.left.val); 
     System.out.println(obj.root.right.right.val); 
    } 
} 
+0

あなたは '高さを()'説明することができますか?私は各サブツリーの要素の数が 'height()'でどのように計算されているのかを知っていますか? – user2916886

+0

@ user2916886 heightは、完全なサブツリーを深く計算します。 –

関連する問題