private void Add(BTLNode data, BTLNode node, BTLNode parent) { if (node.value == data.value) { node.value = data.value; node.leftNode = data.leftNode; node.rightNode = data.rightNode; node.parentNode = parent; return; } if (node.value > data.value) { if (node.leftNode == null) { node.leftNode = new BTLNode(); } Add(data, node.leftNode, node); } else { if (node.rightNode == null) { node.rightNode = new BTLNode(); } Add(data, node.rightNode, node); } }
private void Add(int value, BTLNode node, BTLNode parent) { if (node.value == value) { node.value = value; node.parentNode = parent; return; } if (node.value > value) { if (node.leftNode == null) { node.leftNode = new BTLNode(); } Add(value, node.leftNode, node); } else { if (node.rightNode == null) { node.rightNode = new BTLNode(); } Add(value, node.rightNode, node); } }
private static List <int> ToArray(BTLNode tree) { if (tree == null) { return(new List <int>()); } var result = ToArray(tree.leftNode); result.Add(tree.value); result.AddRange(ToArray(tree.rightNode)); return(result); }
/// <summary> /// Tree counter /// </summary> /// <param name="node">Root node</param> /// <returns>count of elements in the tree</returns> private int CountElements(BTLNode node) { int count = 1; if (node.rightNode != null) { count += CountElements(node.rightNode); } if (node.leftNode != null) { count += CountElements(node.leftNode); } return(count); }
/// <summary> /// Check parental branch which contains currnt node /// </summary> /// <param name="node">current.Node node</param> /// <returns>Side</returns> private Side?MeForParent(BTLNode node) { if (node.parentNode == null) { return(null); } if (node.parentNode.leftNode == node) { return(Side.Left); } if (node.parentNode.rightNode == node) { return(Side.Right); } return(null); }
/// <summary> /// Find value in current.Node node /// </summary> /// <param name="value">Value for search</param> /// <param name="node">Node for search</param> /// <returns>Founded result</returns> public BTLNode Find(int value, BTLNode node) { if (node == null) { return(null); } if (node.value == value) { return(node); } if (node.value > value) { return(Find(value, node.leftNode)); } return(Find(value, node.rightNode)); }
private static List<int> ToArray(BTLNode tree) { if (tree == null) return new List<int>(); var result = ToArray(tree.leftNode); result.Add(tree.value); result.AddRange(ToArray(tree.rightNode)); return result; }
public BTLNode(BTLNode parent, int value) { this.parentNode = parent; this.value = value; }
/// <summary> /// Tree counter /// </summary> /// <param name="node">Root node</param> /// <returns>count of elements in the tree</returns> private int CountElements(BTLNode node) { int count = 1; if (node.rightNode != null) { count += CountElements(node.rightNode); } if (node.leftNode != null) { count += CountElements(node.leftNode); } return count; }
public BTLNode() { this.leftNode = null; this.rightNode = null; this.parentNode = null; }
public void Delete(BTLNode node) { if (node == null) { throw new NullReferenceException(); } var me = MeForParent(node); if (node.leftNode == null && node.rightNode == null) { if (me == Side.Left) { node.parentNode.leftNode = null; } else { node.parentNode.rightNode = null; } return; } if (node.leftNode == null) { if (me == Side.Left) { node.parentNode.leftNode = node.rightNode; } else { node.parentNode.rightNode = node.rightNode; } node.rightNode.parentNode = node.parentNode; return; } if (node.rightNode == null) { if (me == Side.Left) { node.parentNode.leftNode = node.leftNode; } else { node.parentNode.rightNode = node.leftNode; } node.leftNode.parentNode = node.parentNode; return; } if (me == Side.Left) { node.parentNode.leftNode = node.rightNode; } if (me == Side.Right) { node.parentNode.rightNode = node.rightNode; } if (me == null) { var bufLeft = node.leftNode; var bufRightLeft = node.rightNode.leftNode; var bufRightRight = node.rightNode.rightNode; node.value = node.rightNode.value; node.rightNode = bufRightRight; node.leftNode = bufRightLeft; Add(bufLeft, node, node); } else { node.rightNode.parentNode = node.parentNode; Add(node.leftNode, node.rightNode, node.rightNode); } }
private void Add(BTLNode data, BTLNode node, BTLNode parent) { if (node.value == data.value) { node.value = data.value; node.leftNode = data.leftNode; node.rightNode = data.rightNode; node.parentNode = parent; return; } if (node.value > data.value) { if (node.leftNode == null) node.leftNode = new BTLNode(); Add(data, node.leftNode, node); } else { if (node.rightNode == null) node.rightNode = new BTLNode(); Add(data, node.rightNode, node); } }
private void Add(int value, BTLNode node, BTLNode parent) { if (node.value == value) { node.value = value; node.parentNode = parent; return; } if (node.value > value) { if (node.leftNode == null) node.leftNode = new BTLNode(); Add(value, node.leftNode, node); } else { if (node.rightNode == null) node.rightNode = new BTLNode(); Add(value, node.rightNode, node); } }
/// <summary> /// Check parental branch which contains currnt node /// </summary> /// <param name="node">current.Node node</param> /// <returns>Side</returns> private Side? MeForParent(BTLNode node) { if (node.parentNode == null) return null; if (node.parentNode.leftNode == node) return Side.Left; if (node.parentNode.rightNode == node) return Side.Right; return null; }
/// <summary> /// Find value in current.Node node /// </summary> /// <param name="value">Value for search</param> /// <param name="node">Node for search</param> /// <returns>Founded result</returns> public BTLNode Find(int value, BTLNode node) { if (node == null) return null; if (node.value == value) return node; if (node.value > value) { return Find(value, node.leftNode); } return Find(value, node.rightNode); }
public BinaryTreeLink(BTLNode rt) { this.rootNode.Node = rt; this.size = 0; }
public Link() { this.Node = new BTLNode(); }