Binary tree inorder java

huangapple go评论71阅读模式
英文:

Binary tree inorder java

问题

以下是您提供的代码的翻译部分:

我在这段代码中实现了两个函数第一个是将元素插入二叉树另一个是中序遍历请问如何调用中序遍历函数因为该函数要求传入一个类型为node的参数但我刚接触这个不知道应该传入什么

    import java.util.Scanner;
    
    class BinaryTree {
        
        Node root;
        
        class Node {
            
            int data;
            Node leftChild;
            Node rightChild;
    
            public Node(int data) {
                this.data = data;
                leftChild = null;
                rightChild = null;
            }
        }
        
        public Node insert(Node current, int data) {
            if (current == null) {
                return new Node(data);
            } else if (data < current.data) {
                current.leftChild = insert(current.leftChild, data);
            } else if (data > current.data) {
                current.rightChild = insert(current.rightChild, data);
            }
    
            return current;
        }
        
        public void add(int data) {
            root = insert(root, data);
        }
    
        void inorder(Node current) {
            if (current != null) {
                inorder(current.leftChild);
                System.out.println(current.data);
                inorder(current.rightChild);
            }
        }
    
        public static void main(String[] args) {
            // 在这里编写您的代码
            BinaryTree bt = new BinaryTree();
            bt.add(2);
            bt.add(1);
            bt.add(3);
            bt.add(4);
            bt.add(7);
            bt.add(8);
            bt.inorder();  // 在这里调用中序遍历函数
        }
    }
英文:

I have implemented two functions in this code first is inserting elements in a binary tree and the other is In-order traversal .Please help how to call the In-order function ,as the function is asking for a argument of type node which is needed to be given but I am new to this to got struck what to pass in it.

import java.util.Scanner;
class BinaryTree {
Node root;
class Node {
int data;
Node leftChild;
Node rightChild;
public Node(int data) {
this.data = data;
leftChild = null;
rightChild = null;
}
}
public Node insert(Node current, int data) {
if (current == null) {
return new Node(data);
} else if (data &lt; current.data) {
current.leftChild = insert(current.leftChild, data);
} else if (data &gt; current.data) {
current.rightChild = insert(current.rightChild, data);
}
return current;
}
public void add(int data) {
root = insert(root, data);
}
void inorder(Node current) {
if (current != null) {
inorder(current.leftChild);
System.out.println(current.data);
inorder(current.rightChild);
}
}
public static void main(String[] args) {
// Write your code here
BinaryTree bt = new BinaryTree();
bt.add(2);
bt.add(1);
bt.add(3);
bt.add(4);
bt.add(7);
bt.add(8);
bt.inorder();
}
}

答案1

得分: 1

你想要传入你的二叉树根节点,该节点存储在你的BinaryTree类的root成员变量中。所以你想要执行:

bt.inorder(bt.root);

在不改变其余代码的情况下,这将得到以下结果:

1
2
3
4
7
8

由于你正在调用同一类中具有所需传递数据的方法,你还可以向该类中添加一个新方法来完成相同的操作,而无需传递任何数据。你可以使用与已有方法相同的名称,或者其他名称。以下是使用相同名称的方法:

void inorder() {
inorder(root);
}

如果将此方法添加到你的BinaryTree类中,你的代码将在无需其他更改的情况下正常工作,并产生相同的结果。

英文:

You want to pass in the root of your binary tree, which is stored in the root member variable in your BinaryTree class. So you want to do:

bt.inorder(bt.root);

This, with the rest of your code unchanged, gives the following result:

1
2
3
4
7
8

Since you are calling a method in the same class that has the data you need to pass to that method, you could also add a new method to your class that does the same thing without having to pass in any data. You could either use the same name as the method you already have, or some other name. Here's the method using the same name:

void inorder() {
inorder(root);
}

If you add this method to your BinaryTree class, your code will work with no other changes, and will give the same result.

huangapple
  • 本文由 发表于 2020年10月5日 01:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64197858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定