Esempio n. 1
0
        // Connects next nodes in O(1) Space
        public void Connect()
        {
            TreeNodeSpecial <T> startNode    = root;
            TreeNodeSpecial <T> leftMostNode = null;
            TreeNodeSpecial <T> childNode    = null;
            TreeNodeSpecial <T> currentNode  = null;

            while (startNode != null)
            {
                leftMostNode = null;
                childNode    = null;
                currentNode  = startNode;
                while (currentNode != null)                     // check till curent node exists
                {
                    if (currentNode.GetLeft() != null)          // check for left element existance
                    {
                        if (leftMostNode == null)               // assign to left if it is null
                        {
                            leftMostNode = currentNode.GetLeft();
                            childNode    = currentNode.GetLeft();
                        }
                        else                                    // Assign Child Node
                        {
                            childNode.SetNext(currentNode.GetLeft());
                            childNode = currentNode.GetLeft();
                        }
                    }
                    if (currentNode.GetRight() != null)          // check for right element existance
                    {
                        if (leftMostNode == null)                // assign to left if it isnull
                        {
                            leftMostNode = currentNode.GetRight();
                            childNode    = currentNode.GetRight();
                        }
                        else                                     // Assign Child Node
                        {
                            childNode.SetNext(currentNode.GetRight());
                            childNode = currentNode.GetRight();
                        }
                    }
                    currentNode = currentNode.GetNext();        // Update Current Node
                }
                startNode = leftMostNode;                       // Update Start Node
            }
        }
 private void PrintInOrderSpecial(TreeNodeSpecial <int> root)
 {
     if (root == null)
     {
         return;
     }
     PrintInOrder(root.GetLeft());
     Console.Write(root.GetData() + "(" +
                   (root.GetNext() != null ? root.GetNext().GetData() : 0) + ") ");
     PrintInOrder(root.GetRight());
 }
Esempio n. 3
0
 // Private PrintInOrder prints tree Recursively
 private void PrintInOrder(TreeNodeSpecial <T> root)
 {
     if (root == null)
     {
         return;
     }
     PrintInOrder(root.GetLeft());
     if (root.GetNext() != null)
     {
         Console.Write(" " + root.GetData() + "-->" + root.GetNext().GetData());
     }
     else
     {
         Console.Write(" " + root.GetData() + "--> null");
     }
     PrintInOrder(root.GetRight());
 }