private NodeBT FindElement(int value, out NodeBT parent) { var current = _head; parent = null; while (current != null) { if (current.Value > value) { parent = current; current = current.Left; } else if (current.Value < value) { parent = current; current = current.Right; } else { break; } } return(current); }
private void AddTo(NodeBT node, int value) { if (node.Value > value) { if (node.Left == null) { node.Left = new NodeBT(value); } else { AddTo(node.Left, value); } } else if (node.Value < value) { if (node.Right == null) { node.Right = new NodeBT(value); } else { AddTo(node.Right, value); } } }
public void Add(int value) { if (_head == null) { _head = new NodeBT(value); } else { AddTo(_head, value); } Size++; }
private int Width(NodeBT node) { var maxWidth = 0; var height = Height(node); for (var i = 1; i <= height; i++) { var width = GetWidth(node, i); if (width > maxWidth) { maxWidth = width; } } return(maxWidth); }
private int GetWidth(NodeBT node, int level) { if (node == null) { return(0); } if (level == 1) { return(1); } else if (level > 1) { return(GetWidth(node.Left, level - 1) + GetWidth(node.Right, level - 1)); } var count = GetWidth(node.Right, level - 1); return(count); }
private int Height(NodeBT node) { var count = 0; var leftHeight = 0; var rightHeight = 0; if (node == null) { return(0); } if (node.Left != null) { leftHeight = Height(node.Left); } if (node.Right != null) { rightHeight = Height(node.Right); } count = Math.Max(leftHeight, rightHeight) + 1; return(count); }
public void Del(int value) { NodeBT parent; var current = FindElement(value, out parent); if (current == null) { return; } Size--; if (current.Right == null) { if (parent == null) { _head = current.Left; } else { if (parent.Value > current.Value) { parent.Left = current.Left; } else if (parent.Value < current.Value) { parent.Right = current.Left; } } } else if (current.Right.Left == null) { current.Right.Left = current.Left; if (parent == null) { _head = current.Right; } else { if (parent.Value > current.Value) { parent.Left = current.Right; } else if (parent.Value < current.Value) { parent.Right = current.Right; } } } else { var leftMost = current.Left; var leftMostParent = leftMost.Left; while (leftMost.Left != null) { leftMostParent = leftMost; leftMost = leftMost.Left; } leftMostParent.Left = leftMost.Right; leftMost.Left = current.Left; leftMost.Right = current.Right; if (parent == null) { _head = leftMost; } else { if (parent.Value > current.Value) { parent.Left = leftMost; } else if (parent.Value < current.Value) { parent.Right = leftMost; } } } }
private int Width(NodeBT node) { var maxWidth = 0; var height = Height(node); for (var i = 1; i <= height; i++) { var width = GetWidth(node, i); if (width > maxWidth) { maxWidth = width; } } return maxWidth; }
private int Height(NodeBT node) { var count = 0; var leftHeight = 0; var rightHeight = 0; if (node == null) { return 0; } if (node.Left != null) { leftHeight = Height(node.Left); } if (node.Right != null) { rightHeight = Height(node.Right); } count = Math.Max(leftHeight, rightHeight) + 1; return count; }
public void Clear() { _head = null; Size = 0; }
private int GetWidth(NodeBT node, int level) { if (node == null) { return 0; } if (level == 1) { return 1; } else if (level > 1) { return GetWidth(node.Left, level - 1) + GetWidth(node.Right, level - 1); } var count = GetWidth(node.Right, level - 1); return count; }
private NodeBT FindElement(int value, out NodeBT parent) { var current = _head; parent = null; while (current != null) { if (current.Value > value) { parent = current; current = current.Left; } else if (current.Value < value) { parent = current; current = current.Right; } else { break; } } return current; }