因此,我需要构建一个不平衡的二进制搜索树,创建一种平衡它的方法,这是通过基于级别顺序遍历(或至少尝试过)构建一个排序的数组来完成的,然后打印出平衡的二进制搜索树。 这是我的尝试,但仅输出内存位置。 救命 !

    public class BalanceTree {
      public static BST balanceTree(int[]list){
       if(list.length==0)//if length is zero, then empty list, cannot balance
         return null;

       return balanceTree(list,0,list.length-1);}//recursively call balance 

     public static BST balanceTree(int[] list, int begin, int end){//take sorted array from unbalanced tree
       if (begin>end) 
         return null;
       int mid = (begin+end)/2;//find midpoint, assign as root node
       BST chld = new BST(list[mid]);
       chld.left = balanceTree(list,begin,mid-1);//assign as left child
       chld.right = balanceTree(list, mid+1,end);//assign as right child
       return chld;
     }


    public static void levelOrderPrint(Node node){
            int h = height(node);
            for(int i=1; i<=h;i++){
               printLevel(node,i); //print every level

            }
    }
     public static int height (Node t)//find height of tree
      {
        if (t.left == null && t.right == null) //leaf check
          return 0;
        else if (t.left == null)
          return 1 + height(t.right);
        else if (t.right == null)
          return 1 + height(t.left);
        else
          return 1 + Math.max(height(t.left),height(t.right));
      }

     public static void printLevel(Node node, int level){
            if(node ==null){
                return;
            }
            if(level == 1){
                System.out.println(node);
            }
            else if (level > 1){
                printLevel(node.left,level-1);
                printLevel(node.right,level-1);
            }
        }


     public static void main(String[] args) { 
      Node node = new Node(); 
      node.root=26; 
      int [] sortedArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};


      System.out.println("Now Building Unbalanced Tree. Root Node: " + node.root); 
      node.insertNode(node, 25); 
      node.insertNode(node, 24);  
      node.insertNode(node, 23);  
      node.insertNode(node, 22);  
      node.insertNode(node, 21);  
      node.insertNode(node, 20);  
      node.insertNode(node, 19);  
      node.insertNode(node, 18);  
      node.insertNode(node, 17);  
      node.insertNode(node, 16);  
      node.insertNode(node, 15);  
      node.insertNode(node, 14);  
      node.insertNode(node, 13);  
      node.insertNode(node, 12);  
      node.insertNode(node, 11);  
      node.insertNode(node, 10);  
      node.insertNode(node, 9); 
      node.insertNode(node, 8);  
      node.insertNode(node, 7);  
      node.insertNode(node, 6);  
      node.insertNode(node, 5);  
      node.insertNode(node, 4);  
      node.insertNode(node, 3);  
      node.insertNode(node, 2);  
      node.insertNode(node, 1); 
      System.out.println("*********************************************");
      System.out.println("Now building balanced binary search tree: ");

      //int[] arr = 
      levelOrderPrint(node);// I know this should be the array " arr" which is passed into the balance tree method
    balanceTree(sortedArray,0,25);// I know this must use the array from the "levelorderPrint" method,
    //but i could not get it to compile, so i substituted in an already sorted array.



     } 
     }

      public class BST{//constructing bst node
       int val;
       BST left;
       BST right;
       BST(int x){
         val = x;}
     }
    public class Node {
 Node node;
 int root; 
 Node left; 
 Node right; 

 public void insertNode(Node node, int num) { //building unbalanced tree
  if(num < node.root) // if current node is less than root node, insert to the left
  { 
   while(node.left != null) 
   { 
    node = node.left; 
    if( num > node.root){ //if current node is greater than root node, insert to the right

     Node temp = new Node(); 
     temp.root = num; 
     node.right = temp; 
     System.out.println("Inserting "+ num + "" + "at right of" + node.root); 
    } 
   }
   Node temp = new Node(); 
   temp.root = num; 
   node.left = temp; 
   System.out.println("Inserting " +num + "" + "at left of" + node.root); 
  } 
 }

 public String toString(){

   return root+ "";

 }


}

===============>>#1 票数:0

Java中的每个对象都实现toString()方法。 默认情况下(如在Object类中编写的那样),对象将打印其“内存位置”。

为了能够用Java打印对象,您将需要覆盖toString()方法。

===============>>#2 票数:0

在您的行System.out.println(node); 您告诉Java调用node.toString() 因为您没有为自定义Node类创建自己的toString()方法,所以Java自动使用Object.toString()方法,该方法返回内存地址。 考虑在代码中添加以下内容:

public class Node {
    ...
    public String toString() {
        return root + "";
    }
    ...
}

  ask by WhisperingRhino translate from so

未解决问题?本站智能推荐: