用Java代码完成用二叉链表存储的二叉树的前序遍历、中序遍历、后序遍历算法。
时间: 2024-05-06 14:19:19 浏览: 110
好的,这个问题是关于Java编程的,我可以回答。以下是用Java代码实现二叉链表存储的二叉树的前序遍历、中序遍历、后序遍历算法:
```java
class BinaryTree {
Node root;
void preOrder(Node node) {
if(node != null) {
System.out.print(node.value + " ");
preOrder(node.leftChild);
preOrder(node.rightChild);
}
}
void inOrder(Node node) {
if(node != null) {
inOrder(node.leftChild);
System.out.print(node.value + " ");
inOrder(node.rightChild);
}
}
void postOrder(Node node) {
if(node != null) {
postOrder(node.leftChild);
postOrder(node.rightChild);
System.out.print(node.value + " ");
}
}
class Node {
int value;
Node leftChild;
Node rightChild;
Node(int value) {
this.value = value;
this.leftChild = null;
this.rightChild = null;
}
}
}
// example usage
BinaryTree tree = new BinaryTree();
tree.root = tree.new Node(1);
tree.root.leftChild = tree.new Node(2);
tree.root.rightChild = tree.new Node(3);
tree.root.leftChild.leftChild = tree.new Node(4);
tree.root.leftChild.rightChild = tree
阅读全文