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());
 }
예제 #2
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());
 }
예제 #3
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 TreeNode <int> CreateTree()
        {
            int x = int.Parse(Console.ReadLine());

            if (x == -1)
            {
                // this means we should not create a tree
                return(null);
            }

            TreeNode <int> newTreeNode = new TreeNodeSpecial <int>(x);

            // Console.Write("Enter the left child of " + x + " " );
            newTreeNode.SetLeft(CreateTree());
            // Console.Write("Enter the right child of " + x + " ");
            newTreeNode.SetRight(CreateTree());
            return(newTreeNode);
        }
예제 #5
0
        // Create Tree Function
        public TreeNodeSpecial <T> CreateTree()
        {
            T      x;
            string input = Console.ReadLine();

            x = (T)Convert.ChangeType(input, typeof(T));
            if ((int)(object)x == -1)
            {
                return(null);
            }
            TreeNodeSpecial <T> newTreeNode = new TreeNodeSpecial <T>(x);

            Console.WriteLine("Enter the left child of " + x);
            newTreeNode.SetLeft(CreateTree());
            Console.WriteLine("Enter the right child of " + x);
            newTreeNode.SetRight(CreateTree());
            return(newTreeNode);
        }
 public BinaryTree()
 {
     root = CreateTree();
 }
예제 #7
0
 // Constructor to initialise root
 public BinaryTreeSpecial()
 {
     root = CreateTree();
 }