private Treap(int? x, int? y, Treap left = null, Treap right = null, Treap parent = null) { this.x = x; this.y = y; this.Left = left; this.Right = right; this.Parent = parent; }
public static Treap Treapify(int[] x, int[] y) { Treap Root = new Treap(); for (int i = 0; i < x.Length; i++) { Root = Root.Add(x[i], y[i]); } return Root; }
public Treap Add(int x, int y) { if (this.x == null && this.y == null) { this.x = x; this.y = y; return this; } Treap l, r; Split(x, out l, out r); Treap m = new Treap(x, y); return Merge(Merge(l, m), r); }
public static Treap Merge(Treap left, Treap right) { if (left == null) return right; if (right == null) return left; if (left.y > right.y) { return new Treap(left.x, left.y, left.Left, Merge(left.Right, right)); } else { return new Treap(right.x, right.y, Merge(left, right.Left), right.Right); } }
private void AddItem(object sender, EventArgs e) { int x, y; if (!Int32.TryParse(textBox2.Text, out x)) { MessageBox.Show("Wrong number"); return; } if (!Int32.TryParse(textBox3.Text, out y)) { MessageBox.Show("Wrong number"); return; } if (treap == null) treap = new Treap(); treap = treap.Add(x, y); pictureBox1.Invalidate(); }
private void DeleteItem(object sender, EventArgs e) { int x; if (treap != null) { if (!Int32.TryParse(textBox1.Text, out x)) { MessageBox.Show("Wrong number"); return; } try { treap = treap.Remove(x); } catch (NullReferenceException) { MessageBox.Show("There is no element witn index " + x); } } pictureBox1.Invalidate(); }
void DrawTear(Treap treap, int tear, float x, float delta, PaintEventArgs e) { int tearWidth = 60; int horizontalCoef = 20; int vertCoef = 20; Font font = new Font("Times New Roman", 15); Brush brush = new SolidBrush(Color.Black); Pen pen = new Pen(Color.FloralWhite, 1); float y = tear * tearWidth; x += delta; e.Graphics.DrawString(treap.x + "; " + treap.y, font, brush, x, y); if (delta != 0) { e.Graphics.DrawLine(pen, x + horizontalCoef, y, x - delta + horizontalCoef, y - tearWidth + vertCoef); } delta = Math.Abs(delta) / 2F; if (treap.Left != null) DrawTear(treap.Left, tear + 1, x, -delta, e); if (treap.Right != null) DrawTear(treap.Right, tear + 1, x, +delta, e); }
public TreeViewer(int[] xItems, int[] yItems) : this() { treap = Treap.Treapify(xItems, yItems); }
public void Split(int key, out Treap left, out Treap right) { Treap NewTree = null; if (this.x <= key) { if (this.Right == null) right = null; else this.Right.Split(key, out NewTree, out right); if (x != null && y != null) left = new Treap(this.x, this.y, this.Left, NewTree); else left = null; } else { if (this.Left == null) left = null; else this.Left.Split(key, out left, out NewTree); if (x != null && y != null) right = new Treap(this.x, this.y, NewTree, this.Right); else right = null; } }