示例#1
0
 private void PreOrder(BinaryNode CurrentNode, StringBuilder NodeValues)
 {
     NodeValues.Append(CurrentNode.Value.ToString());
     NodeValues.AppendLine();
     if (CurrentNode.LeftNode != null)
     {
         PreOrder(CurrentNode.LeftNode,NodeValues);
     }
     if (CurrentNode.RightNode != null)
     {
         PreOrder(CurrentNode.RightNode,NodeValues);
     }
 }
示例#2
0
 private int InOrder(BinaryNode CurrentNode, int[] NodeValues, int count)
 {
     if (CurrentNode.LeftNode != null)
     {
         count = InOrder(CurrentNode.LeftNode, NodeValues, count);
     }
     Console.WriteLine(NodeValues.Length + "   " + count.ToString());
     NodeValues[count++] = CurrentNode.Value;
     if (CurrentNode.RightNode != null)
     {
         count = InOrder(CurrentNode.RightNode, NodeValues, count);
     }
     return count;
 }
示例#3
0
 private int PreOrder(BinaryNode CurrentNode, int[] NodeValues, int count)
 {
     NodeValues[count++] = CurrentNode.Value;
     if (CurrentNode.LeftNode != null)
     {
         count = PreOrder(CurrentNode.LeftNode, NodeValues, count);
     }
     if (CurrentNode.RightNode != null)
     {
         count = PreOrder(CurrentNode.RightNode, NodeValues, count);
     }
     return count;
 }
示例#4
0
 private BinaryNode FindParentOf(BinaryNode CurrentNode, int ValueToFind)
 {
     if ( (IsEmpty) || (CurrentNode.Value == ValueToFind) || (CurrentNode == null) ) // These are error conditions
     {
         return null;
     }
     else
     {
         if ( ValueToFind < CurrentNode.Value )
         {
             if (CurrentNode.LeftNode == null) // Tree doesn't contain ValueToFind
             {
                 return null;
             }
             else // Left branch of current may contain ValueToFind
             {
                 if (CurrentNode.LeftNode.Value == ValueToFind) // CurrentNode is parent
                 {
                     return CurrentNode;
                 }
                 else // CurrentNode isn't parent but left branch might contain parent.
                 {
                     return FindParentOf(CurrentNode.LeftNode, ValueToFind);
                 }
             }
         }
         else
         {
             if (CurrentNode.RightNode == null) // Tree doesn't contain ValueToFind
             {
                 return null;
             }
             else // Right branch of current may contain ValueToFind
             {
                 if (CurrentNode.RightNode.Value == ValueToFind) // CurrentNode is parent
                 {
                     return CurrentNode;
                 }
                 else // CurrentNode isn't parent but right branch might contain parent.
                 {
                     return FindParentOf(CurrentNode.RightNode, ValueToFind);
                 }
             }
         }
     }
 }
示例#5
0
        private bool DeleteNode(BinaryNode ParentNode, int ValueToDelete)
        {
            // Figure out which node of the parent is the child to be deleted
            // and determine if the child node has any grandchildren.
            BinaryNode ChildNode = null;
            BinaryNode LeftGrandchild = null;
            BinaryNode RightGrandchild = null;

            if ( ParentNode == null ) // Node to delete is the root.
            {
                ChildNode = root;
            }
            else
            {
                if ( ValueToDelete < ParentNode.Value ) // Child is LeftNode of parent
                {
                    ChildNode = ParentNode.LeftNode;
                }
                else // Child is RightNode of parent
                {
                    ChildNode = ParentNode.RightNode;
                }
            }

            // Figure out how whether the Child has any grandchildren
            if (ChildNode.LeftNode != null)
            {
                LeftGrandchild = ChildNode.LeftNode;
            }
            if (ChildNode.RightNode != null)
            {
                RightGrandchild = ChildNode.RightNode;
            }

            if ( (ChildNode.IsLeaf) )
            {
                if ( ParentNode == null)
                {
                    Console.WriteLine("Delete Pattern A");
                    root = null;
                }
                else
                {
                    if (ChildNode.Value < ParentNode.Value)
                    {
                        Console.WriteLine("Delete Pattern B");
                        ParentNode.LeftNode = null;
                    }
                    else
                    {
                        Console.WriteLine("Delete Pattern C");
                        ParentNode.RightNode = null;
                    }
                }
            }
            else
            {
                if ( (LeftGrandchild != null) && (RightGrandchild == null) )
                {
                    if (ParentNode == null)
                    {
                        Console.WriteLine("Delete Pattern D");
                        root = LeftGrandchild;
                    }
                    else
                    {
                        if ( ChildNode.Value < ParentNode.Value)
                        {
                            Console.WriteLine("Delete Pattern E");
                            ParentNode.LeftNode = LeftGrandchild;
                        }
                        else
                        {
                            Console.WriteLine("Delete Pattern F");
                            ParentNode.RightNode = LeftGrandchild;
                        }
                    }
                }
                else if ( (LeftGrandchild == null) && (RightGrandchild != null) )
                {
                    if (ParentNode == null)
                    {
                        Console.WriteLine("Delete Pattern G");
                        root = RightGrandchild;
                    }
                    else
                    {
                        if ( ChildNode.Value < ParentNode.Value)
                        {
                            Console.WriteLine("Delete Pattern H");
                            ParentNode.LeftNode = RightGrandchild;
                        }
                        else
                        {
                            Console.WriteLine("Delete Pattern I");
                            ParentNode.RightNode = RightGrandchild;
                        }
                    }
                }
                else
                {
                    if (ParentNode == null)
                    {
                            BinaryNode TempNode = LeftGrandchild;
                            while (TempNode.RightNode != null)
                            {
                                TempNode = TempNode.RightNode;
                            }
                            TempNode.RightNode = RightGrandchild;
                            Console.WriteLine("Delete Pattern J");
                            root = LeftGrandchild;
                    }
                    else
                    {
                        if ( ChildNode.Value < ParentNode.Value )
                        {
                            BinaryNode TempNode = RightGrandchild;
                            while (TempNode.LeftNode != null)
                            {
                                TempNode = TempNode.LeftNode;
                            }
                            TempNode.LeftNode = LeftGrandchild;
                            Console.WriteLine("Delete Pattern K");
                            ParentNode.LeftNode = RightGrandchild;
                        }
                        else
                        {
                            BinaryNode TempNode = LeftGrandchild;
                            while (TempNode.RightNode != null)
                            {
                                TempNode = TempNode.RightNode;
                            }
                            TempNode.RightNode = RightGrandchild;
                            Console.WriteLine("Delete Pattern L");
                            ParentNode.RightNode = LeftGrandchild;
                        }
                    }
                }
            }
            return true;
        }
示例#6
0
 private bool AddNode(BinaryNode CurrentNode, int ValueToAdd)
 {
     BinaryNode NewNode = null;
     if (IsEmpty)
     {
         if (CurrentNode == null)
         {
             NewNode = new BinaryNode(ValueToAdd);
             root = NewNode;
             return true;
         }
         else
         {
             return false;
         }
     }
     else
     {
         if ( ( CurrentNode == null ) || ( ValueToAdd == CurrentNode.Value ) )
         {
             return false;
         }
         else
         {
             if ( ValueToAdd < CurrentNode.Value)
             {
                 if ( CurrentNode.LeftNode != null )
                 {
                     return AddNode(CurrentNode.LeftNode, ValueToAdd);
                 }
                 else
                 {
                     NewNode = new BinaryNode(ValueToAdd);
                     CurrentNode.LeftNode = NewNode;
                     return true;
                 }
             }
             else
             {
                 if ( CurrentNode.RightNode != null )
                 {
                     return AddNode(CurrentNode.RightNode, ValueToAdd);
                 }
                 else
                 {
                     NewNode = new BinaryNode(ValueToAdd);
                     CurrentNode.RightNode = NewNode;
                     return true;
                 }
             }
         }
     }
 }
示例#7
0
 public void Clear()
 {
     root = null;
 }
示例#8
0
 public BinaryTree()
 {
     root = null;
 }