1、手动建立一个小型二叉树,包括树的结点和结点间的关系。2、设计程序实现通过输入先序、中序遍历序列建立二叉树的操作。要洗澡写出完整的辅助代码。3、设计程序实现通过输入中序、后序遍历序列建立二叉树的操作。要写出完整的代码。4、通过实例测试程序正确性。
时间: 2024-10-26 22:16:34 浏览: 15
二叉树的指定结点路径
1. **手动建立小型二叉树**:
创建一个简单的二叉树结构,我们可以使用递归的方式定义节点。每个节点包含一个值(`val`)和两个子节点(`left` 和 `right`)。以下是一个基本的二叉树节点类的定义:
```java
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
left = null;
right = null;
}
}
```
2. **通过先序和中序遍历来构建二叉树**:
先序遍历(根-左-右)和中序遍历(左-根-右)可以用来确定一棵二叉树的结构。这里我们提供一个辅助方法来找到根节点,然后递归地构建左右子树。
```java
public TreeNode buildTreeFromPreAndIn(String[] preOrder, String[] inOrder) {
if (preOrder == null || preOrder.length == 0 || inOrder == null || inOrder.length == 0) {
return null;
}
int rootValIndex = findRoot(preOrder, inOrder);
TreeNode root = new TreeNode(Integer.parseInt(preOrder[rootValIndex]));
// 左子树
int leftInOrderStart = 0;
int leftInOrderEnd = inOrderIndex(inOrder, root.val);
// 右子树
int rightInOrderStart = leftInOrderEnd + 1;
int rightInOrderEnd = inOrder.length - 1;
root.left = buildTreeFromPreAndIn(Arrays.copyOfRange(preOrder, 0, rootValIndex),
Arrays.copyOfRange(inOrder, leftInOrderStart, leftInOrderEnd));
root.right = buildTreeFromPreAndIn(Arrays.copyOfRange(preOrder, rootValIndex + 1, preOrder.length),
Arrays.copyOfRange(inOrder, rightInOrderStart, rightInOrderEnd));
return root;
}
private int findRoot(String[] preOrder, String[] inOrder) {
for (int i = 0; i < preOrder.length; i++) {
if (!inOrder.contains(preOrder[i])) {
return i;
}
}
throw new IllegalArgumentException("Invalid input: unable to find root node");
}
private int inOrderIndex(String[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(value)) {
return i;
}
}
return -1;
}
```
3. **通过中序和后序遍历来构建二叉树**:
后序遍历(左-右-根)同样有其特点,可以通过找到最后一个出现的前驱元素来定位根节点。这里的代码会稍微复杂些:
```java
public TreeNode buildTreeFromInAndPost(String[] inOrder, String[] postOrder) {
if (inOrder == null || inOrder.length == 0 || postOrder == null || postOrder.length == 0) {
return null;
}
TreeNode lastNode = findLastNode(postOrder, inOrder);
TreeNode root = new TreeNode(lastNode.val);
int index = inOrder.indexOf(lastNode.val);
root.left = buildTreeFromInAndPost(Arrays.copyOfRange(inOrder, 0, index),
Arrays.copyOfRange(postOrder, 0, index));
root.right = buildTreeFromInAndPost(Arrays.copyOfRange(inOrder, index + 1, inOrder.length),
Arrays.copyOfRange(postOrder, index + 1, postOrder.length));
return root;
}
private TreeNode findLastNode(String[] array, String[] inOrder) {
for (int i = 0; i < array.length; i++) {
if (Arrays.binarySearch(inOrder, array[i]) < 0) {
return new TreeNode(array[i]);
}
}
throw new IllegalArgumentException("Invalid input: unable to find last node");
}
```
4. **实例测试**:
你可以创建一些随机的先序、中序或后序遍历序列来测试这些方法是否能正确地重建出对应的二叉树。例如:
```java
public static void main(String[] args) {
String[] preOrder = {"1", "2", "3", "4", "5"};
String[] inOrder = {"4", "2", "5", "1", "3"};
// 或者
String[] inOrder = {"4", "2", "5", "3", "1"};
String[] postOrder = {"4", "5", "2", "3", "1"};
TreeNode tree = buildTreeFromPreAndIn(preOrder, inOrder);
System.out.println("Constructed tree from pre-order and in-order:");
printTree(tree);
tree = buildTreeFromInAndPost(inOrder, postOrder);
System.out.println("\nConstructed tree from in-order and post-order:");
printTree(tree);
}
```
别忘了添加`printTree(TreeNode)`方法来打印二叉树。
阅读全文