这个问题一直困扰着我多年。我想知道如何构建大型二叉树,我知道的唯一方法是创建一个函数将一个元素推送到树上(一个名为insert();的函数)。如果我有一个3元素树并想要添加5个元素,我将不得不调用插入函数5次。这似乎是一个非常差的方法,如果我想添加50个元素怎么办?必须有一个比调用insert()函数五十次更好的方法。
答案 0 :(得分:9)
如果数据是预先排序的,您可以递归地构建它。
基本上为某些输入构建树:
第三步将递归地应用于输入的部分。
这里有一些伪代码:
FUNCTION TREE (input -> node)
IF input IS 1 ENTRY
VALUE OF node IS entry OF input
ELSE
SPLIT input IN 2
LEFT SUB-TREE OF node IS TREE(FIRST HALF OF input)
RIGHT SUB-TREE OF node IS TREE(SECOND HALF OF input)
以下是您可以试验的一些LINQPad C#代码:
// Add the following two using-directives to LINQPad:
// System.Drawing
// System.Drawing.Imaging
static Bitmap _Dummy = new Bitmap(16, 16, PixelFormat.Format24bppRgb);
static Font _Font = new Font("Arial", 12);
void Main()
{
var sorted = Enumerable.Range(1, 16).ToArray();
var tree = BuildTree(sorted);
Visualize(tree);
}
public Node<T> BuildTree<T>(T[] input)
{
return BuildTree<T>(input, 0, input.Length);
}
public Node<T> BuildTree<T>(T[] input, int left, int right)
{
if (right <= left)
return null;
if (right == left + 1)
return new Node<T> { Value = input[left] };
int middle = (left + right) / 2;
return new Node<T>
{
Left = BuildTree<T>(input, left, middle),
Right = BuildTree<T>(input, middle, right)
};
}
public class Node<T>
{
public T Value;
public Node<T> Left;
public Node<T> Right;
public Bitmap ToBitmap()
{
Size valueSize;
using (Graphics g = Graphics.FromImage(_Dummy))
{
var tempSize = g.MeasureString(Value.ToString(), _Font);
valueSize = new Size((int)tempSize.Width + 4, (int)tempSize.Height + 4);
}
Bitmap bitmap;
Color valueColor = Color.LightPink;
if (Left == null && Right == null)
{
bitmap = new Bitmap(valueSize.Width, valueSize.Height);
using (var g = Graphics.FromImage(bitmap))
g.Clear(Color.White);
valueColor = Color.LightGreen;
}
else
{
using (var leftBitmap = Left.ToBitmap())
using (var rightBitmap = Right.ToBitmap())
{
int subNodeHeight = Math.Max(leftBitmap.Height, rightBitmap.Height);
bitmap = new Bitmap(
leftBitmap.Width + rightBitmap.Width + valueSize.Width,
valueSize.Height + 32 + subNodeHeight);
using (var g = Graphics.FromImage(bitmap))
{
g.Clear(Color.White);
int baseY = valueSize.Height + 32;
int leftTop = baseY; // + (subNodeHeight - leftBitmap.Height) / 2;
g.DrawImage(leftBitmap, 0, leftTop);
int rightTop = baseY; // + (subNodeHeight - rightBitmap.Height) / 2;
g.DrawImage(rightBitmap, bitmap.Width - rightBitmap.Width, rightTop);
g.DrawLine(Pens.Black, bitmap.Width / 2 - 4, valueSize.Height, leftBitmap.Width / 2, leftTop);
g.DrawLine(Pens.Black, bitmap.Width / 2 + 4, valueSize.Height, bitmap.Width - rightBitmap.Width / 2, rightTop);
}
}
}
using (var g = Graphics.FromImage(bitmap))
{
float x = (bitmap.Width - valueSize.Width) / 2;
using (var b = new SolidBrush(valueColor))
g.FillRectangle(b, x, 0, valueSize.Width - 1, valueSize.Height - 1);
g.DrawRectangle(Pens.Black, x, 0, valueSize.Width - 1, valueSize.Height - 1);
if (Left == null && Right == null)
g.DrawString(Value.ToString(), _Font, Brushes.Black, x + 1, 2);
}
return bitmap;
}
}
void Visualize<T>(Node<T> node)
{
node.ToBitmap().Dump();
}
这是输出:
答案 1 :(得分:2)
有。 Knuth提供了一种算法,用于从 ACP 第三卷 ACP 第三卷中构建一个合理平衡的二进制文件。