/// <summary> /// Constructor /// </summary> /// <param name="value">Value</param> /// <param name="left">Left node reference</param> /// <param name="right">Right node reference</param> /// <param name="parent">Parent node reference</param> public Leaf(int value, Leaf left, Leaf right, Leaf parent) { this.Value = value; this.Left = left; this.Right = right; this.Parent = parent; }
public bool Find(int value, ref Leaf leaf) { if (RootNode == null) { return false; } else { return findValue(RootNode, value, ref leaf); } }
/// <summary> /// Adds a Value to the tree /// </summary> /// <param name="Value">The value</param> public void Add(int Value) { if (RootNode == null) { Leaf node = new Leaf(Value, null, null, null); RootNode = node; } else { addToNode(RootNode, Value); } }
private void printInOrder(Leaf node) { if (node.Left != null) { printInOrder(node.Left); } Console.WriteLine("{0}", node.Value); if (node.Right != null) { printInOrder(node.Right); } }
private bool findValue(Leaf node, int value, ref Leaf leaf) { if (node.Value == value) { leaf = node; return true; } if (node.Left != null && value < node.Value) { return findValue(node.Left, value, ref leaf); } if (node.Right != null && value > node.Value) { return findValue(node.Right, value, ref leaf); } return false; }
private void addToNode(Leaf leaf, int value) { if (value <= leaf.Value) { if (leaf.Left != null) { addToNode(leaf.Left, value); } else { leaf.Left = new Leaf(value, null, null, leaf); } } else if (value > leaf.Value) { if (leaf.Right != null) { addToNode(leaf.Right, value); } else { leaf.Right = new Leaf(value, null, null, leaf); } } }