#region 二叉搜索樹
public class clsTree2
{
public int key { get; set; }
public int val { get; set; }
public int lv { get; set; }
public int index { get; set; }
public clsTree2 p { get; set; }
public clsTree2 left { get; set; }
public clsTree2 rigth { get; set; }
public void get_tr(clsTree2 p, clsTree2 l, clsTree2 r)
{
this.p = p;
this.left = l;
this.rigth = r;
}
}
public static class toolTree2
{
public static string sr_for = "";
public static void tree2_foreach(clsTree2 x)
{
if (x != null)
{
if (x.p != null)
{
x.lv = x.p.lv + 1;
x.index = x == x.p.left ? x.p.index * 2 - 1 : x.p.index * 2;
}
tree2_foreach(x.left);
sr_for += string.Format("{0},", x.key);
tree2_foreach(x.rigth);
}
}
public static void tree2_foreach_pic(clsTree2 x, Graphics g, int max_lv, int pic_w)
{
int _w = pic_w - 30;
if (x != null)
{
tree2_foreach_pic(x.left, g, max_lv, pic_w);
PointF p = new PointF();
float n = (float)Math.Pow(2, x.lv - 1) + 1;
p.X = _w / n * (float)x.index;
p.Y = x.lv * 20;
get_tree2_pic_str(p, x.key, g);
if (x.p != null)
{
PointF p2 = new PointF();
n = (float)Math.Pow(2, x.p.lv - 1) + 1;
p2.X = _w / n * (float)x.p.index + 5;
p2.Y = x.p.lv * 20 + 5;
get_tree2_pic_line(new PointF(p.X + 5, p.Y + 5), p2, g);
}
tree2_foreach_pic(x.rigth, g, max_lv, pic_w);
}
}
public static void get_tree2_pic_str(PointF p, int k, Graphics g)
{
Pen pen = new Pen(Color.Black, 1);
int _w = 20;
Font MyFont1 = new Font("新細明體", 9, FontStyle.Regular);
g.DrawString(k.ToString(), MyFont1, Brushes.Black, p);
//g.DrawEllipse(pen, p.X, p.Y, _w, _w);
}
public static void get_tree2_pic_line(PointF p1, PointF p2, Graphics g)
{
Pen pen = new Pen(Color.Orange, 1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
g.DrawLine(pen, p1, p2);
}
public static clsTree2 get_tr2(int k)
{
clsTree2 t = new clsTree2();
t.key = t.val = k;
return t;
}
public static clsTree2 tree2_search(clsTree2 x, int k)
{
if (x == null || x.key == k)
{
return x;
}
if (k < x.key)
{
return tree2_search(x.left, k);
}
else
{
return tree2_search(x.rigth, k);
}
}
public static clsTree2 tree2_search_while(clsTree2 x, int k)
{
while (x != null && k != x.key)
{
if (k < x.key)
{
x = x.left;
}
else
{
x = x.rigth;
}
}
return x;
}
public static clsTree2 tree2_min(clsTree2 x)
{
while (x.left != null)
{
x = x.left;
}
return x;
}
public static clsTree2 tree2_max(clsTree2 x)
{
while (x.rigth != null)
{
x = x.rigth;
}
return x;
}
//後繼
public static clsTree2 tree2_hj(clsTree2 x)
{
if (x.rigth != null)
{
return tree2_min(x.rigth);
}
var y = x.p;
while (y != null && x == y.rigth)
{
x = y;
y = y.p;
}
return y;
}
//前驅
public static clsTree2 tree2_qq(clsTree2 x)
{
if (x.left != null)
{
return tree2_max(x.left);
}
var y = x.p;
while (y != null && x == y.left)
{
x = y;
y = y.p;
}
return y;
}
//插入
public static void tree2_insert(clsTree2 t0, clsTree2 z)
{
clsTree2 y = null;
var x = t0;
if (t0 == null)
{
frmSF.t15 = z;
frmSF.t15.index = 1;
frmSF.t15.lv = 1;
return;
}
while (x != null)
{
y = x;
if (z.key < x.key)
{
x = x.left;
}
else
{
x = x.rigth;
}
}
z.p = y;
if (y == null)
{
t0 = z;
}
else if (z.key < y.key)
{
y.left = z;
}
else
{
y.rigth = z;
}
}
//刪除
public static void tree2_del(clsTree2 t0, int k)
{
var z = toolTree2.tree2_search(t0, k);
if (z == t0)
{
frmSF.t15 = null;
return;
}
if (z.left == null)
{
tree2_del_part(t0, z, z.rigth);
}
else if (z.rigth == null)
{
tree2_del_part(t0, z, z.left);
}
else
{
var y = tree2_min(z.rigth);
if (y.p != z)
{
tree2_del_part(t0, y, y.rigth);
y.rigth = z.rigth;
y.rigth.p = y;
}
tree2_del_part(t0, z, y);
y.left = z.left;
y.left.p = y;
}
}
public static void tree2_del_part(clsTree2 t0, clsTree2 u, clsTree2 v)
{
if (u.p == null)
{
t0 = v;
}
else if (u == u.p.left)
{
u.p.left = v;
}
else
{
u.p.rigth = v;
}
if (v != null)
{
v.p = u.p;
}
}
}
#endregion