public TreeWithoutRef (int data) { this.data = data; left = null; right = null; parent = null; }
private void Sort(TreeWithoutRef 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.left = null; Sort(current.parent, bb); } } else if (node.left == null && node.right != null) { bb.Add(node.data); var current = node; if (node.parent != null) { node.parent.left = node.right; } else { current = node.right; current.parent = null; Sort(current, bb); } node.right.parent = node.parent; Sort(current.parent, bb); } else { Sort(node.left, bb); } } }
public int LeafsCount(TreeWithoutRef data) { if (data.left != null && data.right != null) { return 2; } else if (data.left != null || data.right != null) { return 1; } else return 0; }
public TreeWithoutRef() { left = null; right = null; parent = null; }
private void Reverse(TreeWithoutRef 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); } } }
public int NodeSize(TreeWithoutRef node) { return Height(node); }
private int Width(TreeWithoutRef root, int width)/// Требует изменения, нужно придумать как убрать ref { 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); }
private int Height(TreeWithoutRef root) { if (root==null) { return 0; } int left = 1 + Height(root.left); int right = 1 + Height(root.right); return Math.Max(left, right); }
private TreeWithoutRef FindMin(TreeWithoutRef node) { TreeWithoutRef Current = node; while (Current.Left != null) { Current = Current.Left; } return Current; }
public TreeWithoutRef Find(int data) { TreeWithoutRef node = new TreeWithoutRef(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; count--; } // Нету детей с лева 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; count--; } // Нету детей с права 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; count--; } // есть оба дитя 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; count--; } else { // this.FindMin(this.right, ref node); var node = this.FindMin(this); this.data = node.data; node.parent.left = null; node = null; } } } }
public void Add (int data) { var node = new TreeWithoutRef(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); } } } }