コード例 #1
0
 public void InsertToCreateCompleteBT(T data)
 {
     if (Head == null)
     {
         Head = new BinaryTreeNodeWithParent <T>(data);
         BtQueue.Enqueue(Head);
     }
     else
     {
         BinaryTreeNodeWithParent <T> nodeToAdd     = new BinaryTreeNodeWithParent <T>(data);
         BinaryTreeNodeWithParent <T> nodeFromQueue = BtQueue.Peek();
         if (nodeFromQueue.Left == null)
         {
             // add the new node to the left if the left node is not present on the nodeFromQueue
             nodeFromQueue.Left = nodeToAdd;
             nodeToAdd.Parent   = nodeFromQueue;
         }
         else if (nodeFromQueue.Right == null)
         {
             // add the new node to the right if the right node is not present on the nodeFromQueue
             nodeFromQueue.Right = nodeToAdd;
             nodeToAdd.Parent    = nodeFromQueue;
             // Now both the left and right nodes of the nodeFromQueue is full, so we can dequeue it
             BtQueue.Dequeue();
         }
         else
         {
             // we should never hit this condition, as we are dequeueing the Binary Tree node in the condition before
             throw new Exception("The node with both left and right nodes notqual to null is present in the queue");
         }
         BtQueue.Enqueue(nodeToAdd);
     }
 }
コード例 #2
0
 public void InsertSkewed(T data)
 {
     if (Head == null)
     {
         Head = new BinaryTreeNodeWithParent <T>(data);
     }
     else
     {
         BinaryTreeNodeWithParent <T> currentNode = Head;
         while (currentNode.Left != null)
         {
             currentNode = currentNode.Left;
         }
         BinaryTreeNodeWithParent <T> nodeToAdd = new BinaryTreeNodeWithParent <T>(data);
         currentNode.Left = nodeToAdd;
         nodeToAdd.Parent = currentNode;
     }
 }
コード例 #3
0
        public BinaryTreeNodeWithParent <T> Search(T data)
        {
            Queue <BinaryTreeNodeWithParent <T> > queueForTreeNodes = new Queue <BinaryTreeNodeWithParent <T> >();

            queueForTreeNodes.Enqueue(Head);
            while (queueForTreeNodes.Count != 0)
            {
                BinaryTreeNodeWithParent <T> currentNode = queueForTreeNodes.Dequeue();
                if (currentNode.Data.CompareTo(data) == 0)
                {
                    // Found the node
                    return(currentNode);
                }
                if (currentNode.Left != null)
                {
                    queueForTreeNodes.Enqueue(currentNode.Left);
                }
                if (currentNode.Right != null)
                {
                    queueForTreeNodes.Enqueue(currentNode.Right);
                }
            }
            return(null);
        }