public VTree (int data) { this.data = data; left = null; right = null; parent = null; }
public int LeafCount (VTree node) { if (node.Left != null && node.Right != null) { count = 2; } else if (node.Left != null || node.Right != null) { count = 1; } else count = 0; return count; }
public void Add(int data) { var node = new VTree(data); // Если первый пустой if (this.data == 0 && this.left == null && this.right == null && this.parent == null) { this.data = data; count++; } else { // Если уже существует первый if (node.Data < this.Data) { if (this.Left == null) { this.Left = node; left.parent = this; count++; } else { left.Add(data); } } else if (node.Data > this.Data) { if (this.Right == null) { this.Right = node; Right.parent = this; count++; } else { right.Add(data); } } } }
public VTree() { left = null; right = null; parent = null; }
private void Reverse(VTree node, List<int> bb) { if (bb.Count != count) { if (node.left == null && node.right == null) { bb.Add(node.data); var current = node; if (bb.Count != count) { node.parent.right = null; Reverse(current.parent, bb); } } else if (node.left != null && node.right == null) { bb.Add(node.data); var current = node; if (node.parent != null) { node.parent.right = node.left; } else { current = node.left; current.parent = null; Reverse(current, bb); } node.left.parent = node.parent; Reverse(current.parent, bb); } else { Reverse(node.right, bb); } } }
private int Width(VTree root, int width) { if (root == null) { return 0; } if (root.left == null && root.right == null) { width++; } int left = 1 + Width(root.left, width); int right = 1 + Width(root.right, width); return Math.Max(left, right); }
public int NodeSize(VTree node) { return Height(node); }
private int Height(VTree root) { if (root == null) { return 0; } int left = 1 + Height(root.left); int right = 1 + Height(root.right); return Math.Max(left, right); }
private VTree FindMin(VTree node) { VTree Current = node; while (Current.Left != null) { Current = Current.Left; } return Current; }
public VTree Find(int data) { VTree node = new VTree(data); if (this == null) { // вернуть эксепшен } else if (this.data == data) { return this; } else { if (data < this.Data) { if (this.Left.data == data) { node = this.Left; } else { left.Find(data); } } else if (data > this.Data) { if (this.Right.data == data) { node = this.Right; } else { right.Find(data); } } } return node; }
public void Delete(int data) { if (data < this.data) { this.left.Delete(data); } if (data > this.data) { this.right.Delete(data); } if (data == this.data) { // Нету детей if (this.right == null && this.left == null) { if (this.parent.left.data == data) this.parent.left = null; else this.parent.right = null; } // Нету детей с лева else if (this.left == null && this.right != null) { if (this.parent.left.data == data) this.parent.left = this.right; else this.parent.right = this.right; } // Нету детей с права else if (this.right == null && this.left != null) { if (this.parent.left.data == data) this.parent.left = this.left; else this.parent.right = this.left; } // есть оба дитя else { if (this.right.left == null) { this.left.parent = this.right; if (this.parent.left.data == data) this.parent.left = this.right; else this.parent.right = this.right; } else { // this.FindMin(this.right, ref node); var node = this.FindMin(this); this.data = node.data; node.parent.left = null; node = null; } } } count--; }
public void InOrder(VTree N, IVisitor V) { if (N == null) { return; } else { if (N.Left != null) { InOrder(N.Left, V); } V.Visit(N); if (N.Right != null) { InOrder(N.Right, V); } } }
public void Visit (VTree node) { LeafCount(node); }
public void Visit(VTree node) { nodes.Add(node); }