实现二叉树并使其平衡

时间:2016-05-25 18:24:11

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

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

width: 50%; height: auto

public static void main(String [] args)

using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;
using System.Threading;

namespace List
{
class Program
{
    public class comp_list : IComparer<string>
      {
          public int Compare(string xx, string yy) { string x = xx.ToString(); string y = yy.ToString();  return x.CompareTo(y);  } 
      }
    public class comp_tree : IComparer
      {
          public int Compare(object xx, object yy) { string x = xx.ToString(); string y = yy.ToString();  return x.CompareTo(y); }
      }


    static void Main()
    {

        List<string> MyList = new List<string>();
        TreeView tv = new TreeView();

        int Cnt = 10000;
        string s="";
        Random R = new Random();
        for (int i=0; i<Cnt; i++) 
          {
            s=(R.Next(0,Cnt)).ToString();
            MyList.Add( s );
            tv.Nodes.Add( s );
          }

        Stopwatch t = new Stopwatch();  t.Start();             
        comp_list cmp = new comp_list(); 
        MyList.Sort( cmp );  
        t.Stop();  Console.WriteLine("SORT_LIST={0}",t.ElapsedMilliseconds);

        tv.TreeViewNodeSorter= new comp_tree();
        Stopwatch tt = new Stopwatch();  tt.Start();             
        tv.Sort();   
        tt.Stop();  Console.WriteLine("SORT_TREE={0}",tt.ElapsedMilliseconds);
    }
}
}

2 个答案:

答案 0 :(得分:0)

我假设您的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;   
    }

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

答案 1 :(得分:-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;
  }
}