3

I have a simple program to serialize a binary tree. Code:

public static <T> void serialize(TBST<T> tree, String fileName) throws FileNotFoundException, IOException {
        /*
         * using only 1 file will create a lot of confusion in coding.
         */
        try (ObjectOutputStream oosNodeData = new ObjectOutputStream(new FileOutputStream(fileName))) {
                preOrderSerialization(tree.getRoot(), oosNodeData);
        }
    }


    private static <T> void preOrderSerialization(TBSTNode<T> node, ObjectOutputStream oosNodeData) throws IOException {
        if (node == null) { 
            return;                                
        }

        oosNodeData.writeObject(node.element);

        preOrderSerialization(node.left, oosNodeData); 
        preOrderSerialization(node.right, oosNodeData);  
    }

As we can see, the program itself does not use extra space. It however does what its told - serialize. Whats the space aux complexity ? O(n) or O(1) ? please ignore the stack space

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132

1 Answers1

2

This is essentially a recursive tree traversal and as such it would be O(n). See the following for a good overview: Big-Oh for Recursive Functions

Dean
  • 939
  • 10
  • 30