Ejemplo n.º 1
0
        private void PrintTraversalRecursively(MyBinaryTreeNode <T> root)
        {
            if (root == null)
            {
                return;
            }

            PrintTraversalRecursively(root.LeftNode);
            Console.WriteLine(root.Data);
            PrintTraversalRecursively(root.RightNode);
        }
Ejemplo n.º 2
0
        private MyBinaryTreeNode <T> ConstructNodeFromInAndPre(T[] inOrder, T[] preOrder, int inStart, int inEnd)
        {
            // Done because inStart and inIndex - 1 || inIndex + 1 and inEnd might lead to this issue when inIndex and inStart || inIndex and inEnd are same.
            if (inStart > inEnd)
            {
                return(null);
            }

            var node = new MyBinaryTreeNode <T>(preOrder[preIndex]);

            preIndex++;

            if (inStart == inEnd)
            {
                return(node);
            }

            var inIndex = FindNodeInInOrderSubArray(inOrder, inStart, inEnd, node.Data);

            node.LeftNode  = ConstructNodeFromInAndPre(inOrder, preOrder, inStart, inIndex - 1);
            node.RightNode = ConstructNodeFromInAndPre(inOrder, preOrder, inIndex + 1, inEnd);

            return(node);
        }