简体   繁体   中英

Why do I need to use the wrapper on my method

I've been working on interview questions and as I was finished with the problem of finding the max depth of a tree I wanted to print the tree out as well. Going over some older code I worked on, I found the printPostOrder method. I found if I remove the wrapper code I can't use the method. I don't know enough about the subject and understand that I need to do more research, but could someone explain to me why it's necessary and why it's used. Or possibly some other examples so I can solidify my understanding of what is going on internally?

// Java program to find height of tree

// A binary tree node
  class Node {
int val;

Node left, right;

Node(int item) {
    val = item;
    left = right = null;
 }
}

class BinaryTree {
Node root;

/*
 * 
 * Through recursion we get both left and right side
 * depths. Then compare left and right side, pick the bigger one and add one to
 * it to account for the current node.
 */
int maxDepth(Node node) {
    if (node == null)
        return 0;
    else {
        /* compute the depth of each subtree */
        int leftDepth = maxDepth(node.left);
        int rightDepth = maxDepth(node.right);

        int bigger = Math.max(leftDepth, rightDepth);

        return bigger + 1;
    }

}

void printPostorder(Node node) {
    if (node == null)
        return;

    // first recur on left subtree
    printPostorder(node.left);

    // then recur on right subtree
    printPostorder(node.right);

    // now deal with the node
    System.out.print(node.val + " ");
}

// why does this wrapper make the above method function and not without it
void printPostorder() {
    printPostorder(root);
}

public static void main(String[] args) {
    BinaryTree tree = new BinaryTree();

    tree.root = new Node(1);
    tree.root.left = new Node(2);
    tree.root.right = new Node(3);
    tree.root.left.left = new Node(4);
    tree.root.left.right = new Node(5);
    tree.root.left.left.left = new Node(66);
    tree.root.left.left.right = new Node(77);
    tree.root.left.left.left.left = new Node(666);

    System.out.println("Height of tree is : " + tree.maxDepth(tree.root));
    tree.printPostorder();

}
}

Strictly speaking, you don't need a separate void printPostorder() method that takes no arguments; you could, instead, force the caller to call void printPostorder(Node node) , and pass the root of the tree as an argument:

    tree.printPostorder(tree.root);

But that makes for messy code. Callers expect to be able to just write tree.printPostorder() to print the tree, rather than having to pass in a redundant argument just because of how printPostorder is implemented internally.

(Incidentally, in a more-realistic program, I'd expect void printPostorder() to be public, and Node root and void printPostorder(Node node) to both be private, so as to not expose this sort of implementation detail.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM