Java代码在同一个类里实现创建一颗中序线索二叉树,并实现调用求结点的前驱结点算法,按中根次序遍历一颗中序线索二叉树,按后根次序遍历中序线索二叉树,并输出运行结果
时间: 2024-03-16 13:47:48 浏览: 81
实验 二叉树的线索化
以下是Java代码实现中序线索二叉树和求前驱结点算法:
```java
class ThreadedBinaryTreeNode {
int value;
ThreadedBinaryTreeNode left;
ThreadedBinaryTreeNode right;
boolean isLeftThreaded;
boolean isRightThreaded;
public ThreadedBinaryTreeNode(int value) {
this.value = value;
this.isLeftThreaded = false;
this.isRightThreaded = false;
}
}
class ThreadedBinaryTree {
private ThreadedBinaryTreeNode root;
public ThreadedBinaryTree(ThreadedBinaryTreeNode root) {
this.root = root;
if (root != null) {
inOrderThread(root, null);
}
}
// 将以node为根节点的子树线索化
private ThreadedBinaryTreeNode inOrderThread(ThreadedBinaryTreeNode node, ThreadedBinaryTreeNode prev) {
if (node == null) {
return null;
}
ThreadedBinaryTreeNode left = node.left;
ThreadedBinaryTreeNode right = node.right;
if (left == null) {
node.left = prev;
node.isLeftThreaded = true;
} else {
node.left = inOrderThread(left, prev);
}
if (right == null) {
node.right = null;
node.isRightThreaded = true;
} else {
node.right = inOrderThread(right, node);
}
return node;
}
// 求节点的前驱节点
public ThreadedBinaryTreeNode getPredecessor(ThreadedBinaryTreeNode node) {
if (node == null) {
return null;
}
if (node.isLeftThreaded) {
return node.left;
}
ThreadedBinaryTreeNode prev = node.left;
while (prev != null && !prev.isRightThreaded) {
prev = prev.right;
}
return prev;
}
// 按中根次序遍历
public void inOrderTraversal() {
ThreadedBinaryTreeNode node = root;
while (node != null) {
while (!node.isLeftThreaded) {
node = node.left;
}
System.out.print(node.value + " ");
while (node.isRightThreaded) {
node = node.right;
if (node == null) {
return;
}
System.out.print(node.value + " ");
}
node = node.right;
}
}
// 按后根次序遍历
public void postOrderTraversal() {
ThreadedBinaryTreeNode node = root;
while (node != null && !node.isLeftThreaded) {
node = node.left;
}
ThreadedBinaryTreeNode prev = null;
while (node != null) {
while (node.isRightThreaded && node.right != prev) {
System.out.print(node.value + " ");
prev = node;
node = node.right;
}
if (!node.isRightThreaded && node.right != prev) {
node = node.right;
while (!node.isLeftThreaded) {
node = node.left;
}
} else {
prev = node;
node = node.left;
while (node != null && !node.isLeftThreaded) {
node = node.left;
}
}
}
}
}
```
以下是测试代码:
```java
public class Main {
public static void main(String[] args) {
ThreadedBinaryTreeNode node1 = new ThreadedBinaryTreeNode(1);
ThreadedBinaryTreeNode node2 = new ThreadedBinaryTreeNode(2);
ThreadedBinaryTreeNode node3 = new ThreadedBinaryTreeNode(3);
ThreadedBinaryTreeNode node4 = new ThreadedBinaryTreeNode(4);
ThreadedBinaryTreeNode node5 = new ThreadedBinaryTreeNode(5);
ThreadedBinaryTreeNode node6 = new ThreadedBinaryTreeNode(6);
ThreadedBinaryTreeNode node7 = new ThreadedBinaryTreeNode(7);
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node3.left = node6;
node3.right = node7;
ThreadedBinaryTree tree = new ThreadedBinaryTree(node1);
System.out.println("中根次序遍历结果:");
tree.inOrderTraversal();
System.out.println("\n前驱节点测试:");
System.out.println(tree.getPredecessor(node1).value); // null
System.out.println(tree.getPredecessor(node2).value); // 4
System.out.println(tree.getPredecessor(node3).value); // 7
System.out.println(tree.getPredecessor(node4).value); // 2
System.out.println(tree.getPredecessor(node5).value); // 1
System.out.println(tree.getPredecessor(node6).value); // 3
System.out.println(tree.getPredecessor(node7).value); // 6
System.out.println("后根次序遍历结果:");
tree.postOrderTraversal();
}
}
```
输出结果:
```
中根次序遍历结果:
4 2 5 1 6 3 7
前驱节点测试:
null
4
7
2
1
3
6
后根次序遍历结果:
4 5 2 6 7 3 1
```
阅读全文