// Add a node to this node's subtree.
        public void AddNode(TreeNode node, ref TreeNode linkFromParent)
        {
            // See which subtree should hold it.
            if (node.Value < this.Value)
            {
                if (LeftChild == null)
                {
                    LeftChild   = node;
                    node.Height = 1;
                }
                else
                {
                    LeftChild.AddNode(node, ref LeftChild);
                }
            }
            else
            {
                if (RightChild == null)
                {
                    RightChild  = node;
                    node.Height = 1;
                }
                else
                {
                    RightChild.AddNode(node, ref RightChild);
                }
            }

            // Update the node's height (in case subtree heights have changed).
            UpdateHeight();

            // When we return to this point, we may need to rebalance our subtree.
            RebalanceSubtree(this, ref linkFromParent);
        }
Esempio n. 2
0
 public void AddNode(T nodeValue)
 {
     if (nodeValue.CompareTo(this._Value) > 0)
     {
         if (RightChild != null)
         {
             RightChild.AddNode(nodeValue);
         }
         else
         {
             RightChild = new BinaryTree <T>(nodeValue);
         }
     }
     else
     {
         if (LeftChild != null)
         {
             LeftChild.AddNode(nodeValue);
         }
         else
         {
             LeftChild = new BinaryTree <T>(nodeValue);
         }
     }
 }
Esempio n. 3
0
 // Add a node to this node's subtree.
 public void AddNode(TreeNode node)
 {
     // See which subtree should hold it.
     if (node.Value < this.Value)
     {
         if (LeftChild == null)
         {
             LeftChild = node;
         }
         else
         {
             LeftChild.AddNode(node);
         }
     }
     else
     {
         if (RightChild == null)
         {
             RightChild = node;
         }
         else
         {
             RightChild.AddNode(node);
         }
     }
 }
Esempio n. 4
0
 // Add a node to this node's sorted subtree.
 public void AddNode(int value)
 {
     // See if this value is smaller than ours.
     if (value < Value)
     {
         // The new value is smaller. Add it to the left subtree.
         if (LeftChild == null)
         {
             LeftChild = new BinaryNode(value);
         }
         else
         {
             LeftChild.AddNode(value);
         }
     }
     else
     {
         // The new value is not smaller. Add it to the right subtree.
         if (RightChild == null)
         {
             RightChild = new BinaryNode(value);
         }
         else
         {
             RightChild.AddNode(value);
         }
     }
 }
Esempio n. 5
0
 // Add a node to this node's sorted subtree.
 public void AddNode(int value)
 {
     // See if the new value is smaller than ours.
     if (value < Value)
     {
         // The new value is smaller. Add it to the left subtree.
         if (LeftChild != null)
         {
             LeftChild.AddNode(value);
         }
         else
         {
             // Add the new child here.
             ThreadedNode child = new ThreadedNode(value);
             child.LeftThread  = LeftThread;
             child.RightThread = this;
             this.LeftChild    = child;
             this.LeftThread   = null;
         }
     }
     else
     {
         // The new value is not smaller. Add it to the right subtree.
         if (RightChild != null)
         {
             RightChild.AddNode(value);
         }
         else
         {
             // Add the new child here.
             ThreadedNode child = new ThreadedNode(value);
             child.LeftThread  = this;
             child.RightThread = this.RightThread;
             this.RightChild   = child;
             this.RightThread  = null;
         }
     }
 }