创建二叉树,编写程序,初始从键盘输入二叉树的结点数据创建二叉树,并将该二叉树的数据以某种方式存储到文件btree.dat中,以便程序此后运行时从文件中读取数据构建该二叉树;要求能根据指定结点求出其在二叉树中所在的层数。java语言编写代码,带解析
时间: 2024-06-24 13:01:47 浏览: 175
创建二叉树并将其数据存储到文件中通常涉及以下几个步骤:定义二叉树节点、读取用户输入、创建二叉树、保存到文件以及从文件加载重建二叉树。这里是一个简单的Java代码示例,用于演示如何实现这些功能:
首先,定义一个二叉树节点类(`BinaryTreeNode`):
```java
public class BinaryTreeNode {
int data;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
```
接下来,创建一个方法来从键盘输入构建二叉树:
```java
import java.util.Scanner;
public void createTreeFromInput() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the binary tree nodes (enter 'null' to stop):");
BinaryTreeNode root = buildTree(scanner);
scanner.close();
}
private BinaryTreeNode buildTree(Scanner scanner) {
String input = scanner.nextLine();
if (input.equals("null")) return null;
int data = Integer.parseInt(input);
BinaryTreeNode node = new BinaryTreeNode(data);
node.left = buildTree(scanner); // 递归左子树
node.right = buildTree(scanner); // 递归右子树
return node;
}
```
然后,我们需要一个方法来保存二叉树到文件:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public void saveTreeToFile(BinaryTreeNode root, String filename) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
saveNode(root, writer);
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
private void saveNode(BinaryTreeNode node, BufferedWriter writer) throws IOException {
if (node == null) {
writer.write("null\n");
} else {
writer.write(Integer.toString(node.data) + "\n");
saveNode(node.left, writer);
saveNode(node.right, writer);
}
}
```
最后,从文件中重建二叉树和计算节点层数:
```java
public BinaryTreeNode loadTreeFromFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
return buildTree(reader);
} catch (IOException e) {
System.err.println("Error reading from file: " + e.getMessage());
return null;
}
}
private BinaryTreeNode buildTree(BufferedReader reader) throws IOException {
String line = reader.readLine();
if (line.equals("null")) return null;
return buildTreeFromLine(line, reader);
}
private BinaryTreeNode buildTreeFromLine(String line, BufferedReader reader) throws IOException {
int data = Integer.parseInt(line);
BinaryTreeNode node = new BinaryTreeNode(data);
node.left = buildTree(reader);
node.right = buildTree(reader);
return node;
}
// 计算节点层数
public int getLevel(BinaryTreeNode node, int level, int[][] levels) {
if (node == null) return level;
levels[level][++levels[level]] = node.data;
return getLevel(node.left, level + 1, levels);
}
```
在主函数中,你可以这样使用:
```java
public static void main(String[] args) {
// 创建二叉树
BinaryTreeNode root = new BinaryTreeNode(1);
// 输入节点
createTreeFromInput();
// 保存到文件
saveTreeToFile(root, "btree.dat");
// 重建树
BinaryTreeNode loadedRoot = loadTreeFromFile("btree.dat");
// 测试节点层数
int[][] levels = new int[10][];
for (int i = 0; i < 10; i++) {
levels[i] = new int[10];
}
int level = getLevel(loadedRoot, 0, levels);
System.out.printf("The root node is on level %d.\n", level);
// 关闭流和资源
// ...
}
```
记得在主函数的最后处理关闭流和资源,如`writer.close()`和`reader.close()`。这个代码示例应该能帮助你理解如何创建、保存和重建二叉树,并计算节点的层数。如果你需要更详细的解释或有其他问题,请告诉我。
阅读全文