用java实现二叉树的前序、后序和内序

用java实现二叉树的前序、后序和内序,java,recursion,binary-tree,Java,Recursion,Binary Tree,我正在学习计算机科学的第二学期,在我的数据结构课上,我们看到了递归二叉树。我们必须基于以下Java代码,使用递归进行前序后序和顺序遍历: public class BinaryTree { Node root; public void addNode(int key, String name) { // Create a new Node and initialize it Node newNode = new Node(key, na

我正在学习计算机科学的第二学期,在我的数据结构课上,我们看到了递归二叉树。我们必须基于以下Java代码,使用递归进行前序后序和顺序遍历:

    public class BinaryTree {

    Node root;

    public void addNode(int key, String name) {
        // Create a new Node and initialize it
        Node newNode = new Node(key, name);
        // If there is no root this becomes root
        if (root == null) {
            root = newNode;
        } else {
            // Set root as the Node we will start
            // with as we traverse the tree
            Node focusNode = root;
            // Future parent for our new Node
            Node parent;

            while (true) {
                // root is the top parent so we start
                // there
                parent = focusNode;

                // Check if the new node should go on
                // the left side of the parent node

                if (key < focusNode.key) {

                    // Switch focus to the left child

                    focusNode = focusNode.leftChild;

                    // If the left child has no children

                    if (focusNode == null) {

                            // then place the new node on the left of it

                            parent.leftChild = newNode;
                            return; // All Done

                    }

                } else { // If we get here put the node on the right
                    focusNode = focusNode.rightChild;

                    // If the right child has no children

                    if (focusNode == null) {

                            // then place the new node on the right of it

                            parent.rightChild = newNode;
                            return; // All Done
                    }
                }
            }
        }
    }

    // Traversals recursion methods

    public void inOrderTraverseTree(Node focusNode) {

    }

    public void preorderTraverseTree(Node focusNode) {

    }

    public void postOrderTraverseTree(Node focusNode) {

    }

    //******************************************************************

    public Node findNode(int key) {
        // Start at the top of the tree
        Node focusNode = root;
        // While we haven't found the Node
        // keep looking
        while (focusNode.key != key) {
            // If we should search to the left
            if (key < focusNode.key) {
                    // Shift the focus Node to the left child
                    focusNode = focusNode.leftChild;
            } else {
                    // Shift the focus Node to the right child
                    focusNode = focusNode.rightChild;
            }
            // The node wasn't found
            if (focusNode == null)
                    return null;
        }
        return focusNode;
    }

    public static void main(String[] args) {
        BinaryTree theTree = new BinaryTree();
        theTree.addNode(50, "Boss");
        theTree.addNode(25, "Vice President");
        theTree.addNode(15, "Office Manager");
        theTree.addNode(30, "Secretary");
        theTree.addNode(75, "Sales Manager");
        theTree.addNode(85, "Salesman 1");

        // Different ways to traverse binary trees
         theTree.inOrderTraverseTree(theTree.root);
         theTree.preorderTraverseTree(theTree.root);
         theTree.postOrderTraverseTree(theTree.root);
        // Find the node with key 75
        System.out.println("\nNode with the key 75");
        System.out.println(theTree.findNode(75));
    }
}

class Node {
    int key;
    String name;
    Node leftChild;
    Node rightChild;

    Node(int key, String name) {
        this.key = key;
        this.name = name;
    }

    public String toString() {
        return name + " has the key " + key;
        /*
         * return name + " has the key " + key + "\nLeft Child: " + leftChild +
         * "\nRight Child: " + rightChild + "\n";
         */

    }
}
公共类二进制树{
节根;
public void addNode(int键,字符串名称){
//创建一个新节点并初始化它
Node newNode=新节点(键、名称);
//如果没有根,则成为根
if(root==null){
根=新节点;
}否则{
//将root设置为我们将启动的节点
//当我们穿过这棵树的时候
节点focusNode=root;
//新节点的未来父节点
节点父节点;
while(true){
//root是顶级父级,因此我们开始
//那里
父节点=焦点节点;
//检查新节点是否应继续运行
//父节点的左侧
if(键

有人能给我解释一下这些trasversals是如何工作的以及如何编码的吗?

网上有一些二叉树的可视化效果,所以你可以更好地理解它,但这里有一些我使用的图像


你到底想让我们做什么来回答这个问题,请确保问题是显而易见的。有很多美丽的资源可用,你应该搜索谷歌并从中学习
public void inOrderTraverseTree(Node focusNode) {
    if (focusNode != null) {
        // Traverse the left node
        inOrderTraverseTree(focusNode.leftChild);
        // Visit the currently focused on node
        System.out.println(focusNode);
        // Traverse the right node
        inOrderTraverseTree(focusNode.rightChild);
    }
}
public void postOrderTraverseTree(Node focusNode) {
    if (focusNode != null) {
        postOrderTraverseTree(focusNode.leftChild);
        postOrderTraverseTree(focusNode.rightChild);

        System.out.println(focusNode);
    }
}
public void preorderTraverseTree(Node focusNode) {
    if (focusNode != null) {
        System.out.println(focusNode);

        preorderTraverseTree(focusNode.leftChild);
        preorderTraverseTree(focusNode.rightChild);
    }
}