예제 #1
0
 /// <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;
 }
예제 #2
0
 public bool Find(int value, ref Leaf leaf)
 {
     if (RootNode == null)
     {
         return false;
     }
     else
     {
         return findValue(RootNode, value, ref leaf);
     }
 }
예제 #3
0
 /// <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);
     }
 }
예제 #4
0
        private void printInOrder(Leaf node)
        {
            if (node.Left != null)
            {
                printInOrder(node.Left);
            }

            Console.WriteLine("{0}", node.Value);

            if (node.Right != null)
            {
                printInOrder(node.Right);
            }
        }
예제 #5
0
        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;
        }
예제 #6
0
 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);
         }
     }
 }