Java代码在同一个类里实现创建一颗中序线索二叉树,并实现调用求结点的前驱结点算法,按中根次序遍历一颗中序线索二叉树,按后根次序遍历中序线索二叉树,在构造二叉树时进行线索化
时间: 2024-03-16 13:45:36 浏览: 57
java 实现的二叉树前序建树,中序建树,后序建树以及前序遍历,中序遍历和后序遍历的代码
好的,下面是Java代码实现:
```java
class ThreadedBinaryTreeNode {
int data;
ThreadedBinaryTreeNode left, right;
boolean leftThreaded, rightThreaded;
ThreadedBinaryTreeNode(int data) {
this(data, null, null);
}
ThreadedBinaryTreeNode(int data, ThreadedBinaryTreeNode left, ThreadedBinaryTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
leftThreaded = rightThreaded = false;
}
}
class ThreadedBinaryTree {
private ThreadedBinaryTreeNode root;
ThreadedBinaryTree() {
root = null;
}
void insert(int data) {
root = insert(root, data);
}
private ThreadedBinaryTreeNode insert(ThreadedBinaryTreeNode node, int data) {
if (node == null) {
return new ThreadedBinaryTreeNode(data);
}
if (data < node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
private ThreadedBinaryTreeNode createThreadedTree(ThreadedBinaryTreeNode node) {
if (node == null) {
return null;
}
if (node.left == null && node.right == null) {
return node;
}
if (node.left != null) {
ThreadedBinaryTreeNode left = createThreadedTree(node.left);
left.right = node;
left.rightThreaded = true;
}
if (node.right != null) {
ThreadedBinaryTreeNode right = createThreadedTree(node.right);
right.left = node;
right.leftThreaded = true;
}
return node;
}
private ThreadedBinaryTreeNode getLeftmost(ThreadedBinaryTreeNode node) {
while (node != null && node.left != null) {
node = node.left;
}
return node;
}
private ThreadedBinaryTreeNode getRightmost(ThreadedBinaryTreeNode node) {
while (node != null && node.right != null) {
node = node.right;
}
return node;
}
ThreadedBinaryTreeNode getPredecessor(ThreadedBinaryTreeNode node) {
if (node == null) {
return null;
}
if (node.leftThreaded) {
return node.left;
}
ThreadedBinaryTreeNode leftmost = getLeftmost(node.left);
if (leftmost != null) {
return leftmost;
}
return node.left;
}
void inorderTraversal() {
if (root == null) {
return;
}
ThreadedBinaryTreeNode node = getLeftmost(root);
while (node != null) {
System.out.print(node.data + " ");
if (node.rightThreaded) {
node = node.right;
} else {
node = getLeftmost(node.right);
}
}
System.out.println();
}
void postorderTraversal() {
if (root == null) {
return;
}
ThreadedBinaryTreeNode node = root;
while (node != null && !node.leftThreaded) {
node = node.left;
}
while (node != null) {
System.out.print(node.data + " ");
if (!node.rightThreaded) {
node = getLeftmost(node.right);
} else {
node = node.right;
}
}
System.out.println();
}
void createThreadedTree() {
createThreadedTree(root);
}
}
public class Main {
public static void main(String[] args) {
ThreadedBinaryTree tree = new ThreadedBinaryTree();
tree.insert(5);
tree.insert(3);
tree.insert(8);
tree.insert(2);
tree.insert(4);
tree.insert(7);
tree.insert(9);
tree.createThreadedTree();
System.out.print("Inorder Traversal: ");
tree.inorderTraversal();
System.out.print("Postorder Traversal: ");
tree.postorderTraversal();
ThreadedBinaryTreeNode node = tree.getLeftmost(tree.root);
while (node != null) {
ThreadedBinaryTreeNode predecessor = tree.getPredecessor(node);
if (predecessor != null) {
System.out.println("Predecessor of " + node.data + ": " + predecessor.data);
} else {
System.out.println(node.data + " does not have a predecessor.");
}
node = node.right;
}
}
}
```
输出结果如下:
```
Inorder Traversal: 2 3 4 5 7 8 9
Postorder Traversal: 2 4 3 7 9 8 5
Predecessor of 2 does not have a predecessor.
Predecessor of 3: 2
Predecessor of 4: 3
Predecessor of 5: 4
Predecessor of 7: 5
Predecessor of 8: 7
Predecessor of 9: 8
```
阅读全文