英文:
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 < 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) {
// 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论