【问题标题】:implement binary tree and make it balanced实现二叉树并使其平衡
【发布时间】:2016-09-23 12:26:16
【问题描述】:

如何在树中插入 1 到 10000 之间的随机整数?我只是在下面的代码中使用scanner.in

 private BinaryTree insert(BinaryTree node, int value)
 {
     if (node == null)
         node = new BinaryTree(value);
     else
     {
         if (value <= node.getValue())
             node.left_child = insert(node.left_child, value);
         else
             node.right_child = insert(node.right_child, value);
     }
     return node;
 }

public static void main(String[] args)

    do    
    {
        //tree operations
        System.out.println("\nTree Operations\n");
        System.out.println("1. insert ");


        int choice = scan.nextInt();            
        switch (choice)
        {
        case 1 : 
            System.out.println("get integer element to insert");
            bst.insert( scan.nextInt() );                     
            break;                                    
        default : 
            System.out.println("Wrong Entry \n ");
            break;   
        }

【问题讨论】:

  • 到底是什么问题?你有错误吗?结果错误?
  • 我认为您需要重新考虑如何实施 BT

标签: java algorithm data-structures binary-tree insertion


【解决方案1】:

我假设您的BinaryTree 课程如下:

public BinaryTree{

private int value = null;
private BinaryTree left_child=null;
private BinaryTree right_child=null;
//Also insert getters and setters here

BinaryTree(value){
this.value = value;
}

 private BinaryTree insert(BinaryTree node, int value)
 {
     if (node == null)
         node = new BinaryTree(value);
     else
     {
         if (value <= node.getValue())
             node.left_child = insert(node.left_child, value);
         else
             node.right_child = insert(node.right_child, value);
     }
     return node;
 }
}

在你的主要方法中,你应该有:

BinaryTree node = new BinaryTree("RootValue");
do{
    //tree operations
    System.out.println("\nTree Operations\n");
    System.out.println("1. insert ");


    int choice = scan.nextInt();            
    switch (choice)
    {
    case 1 : 
        System.out.println("get integer element to insert");
        node = bst.insert( node,scan.nextInt() );                     
        break;                                    
    default : 
        System.out.println("Wrong Entry \n ");
        break;   
    }

我没有对此进行测试,但我认为您的代码的主要问题是您的插入方法需要两个参数,但您只在代码中传递了一个。让我知道这是否有效。

【讨论】:

    【解决方案2】:

    这只是用于插入,您需要另一个过程来使其平衡。我会建议, 取一个数组中的所有整数,对数组进行排序,然后一次性构造二叉树,

    private BinaryTree constructBalanced(int a[], int low, int high) {
    
      if (low > high) return null;
      else {
    
        int mid = (low + high) / 2;
        BinaryTree root = new BinaryTree(a[mid]);
        root.left = constructBalanced(int a[], low, mid - 1);
        root.right = constructBalanced(int a[], int mid + 1, high);
        return root;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多