class TreeNode {
public TreeNode left; public TreeNode right; public int value; public TreeNode(TreeNode left, TreeNode right, int value) {
this.left = left;
this.right = right;
this.value = value;
}
} public static void stackInOrder(TreeNode root) {
Stack stack = new Stack();
if (root == null)
return;
else
stack.push(root);
TreeNode temp = root.left;
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
temp = (TreeNode) stack.pop();
while (temp != null) {
System.out.println(temp.value);
temp = temp.right;
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
if (stack.empty())
break;
temp = (TreeNode) stack.pop();
// System.out.println(temp.value);
}
}现在就是System.out.println(temp.value)放那里的问题 是么?
谢谢
public TreeNode left; public TreeNode right; public int value; public TreeNode(TreeNode left, TreeNode right, int value) {
this.left = left;
this.right = right;
this.value = value;
}
} public static void stackInOrder(TreeNode root) {
Stack stack = new Stack();
if (root == null)
return;
else
stack.push(root);
TreeNode temp = root.left;
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
temp = (TreeNode) stack.pop();
while (temp != null) {
System.out.println(temp.value);
temp = temp.right;
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
if (stack.empty())
break;
temp = (TreeNode) stack.pop();
// System.out.println(temp.value);
}
}现在就是System.out.println(temp.value)放那里的问题 是么?
谢谢
你能给修改一下 或者自己写一个类似的后序遍历算法么?
谢拉
再说,你的这种代码也太不OO了,是“用Java写的非Java代码”。public class BinaryTree {
private BinaryTree left, right;
private int value;
public BinaryTree(int v, BinaryTree l, BinaryTree r) {
value = v;
left = l;
right = r;
}
public BinaryTree(int v) { // 叶子的构造方法
this(v, null, null);
} public void traversePreOrder() { // 前序遍历
System.out.println(value);
if (left != null) left.traversePreOrder();
if (right != null) right.traversePreOrder();
}
public void traverseInOrder() { // 中序遍历
if (left != null) left.traverseInOrder();
System.out.println(value);
if (right != null) right.traverseInOrder();
}
public void traversePostOrder() { // 后序遍历
if (left != null) left.traversePostOrder();
if (right != null) right.traversePostOrder();
System.out.println(value);
}
}测试代码可以如下:/*
二叉树形如:
5
|
/ \
2 4
|
/ \
1 7
|
\
9
*/
BinaryTree root = new BinaryTree(
5,
new BinaryTree(
2,
new BinaryTree(1, null, new BinaryTree(9)),
new BinaryTree(7)
),
new BinaryTree(4)
);
root.traversePreOrder();
System.out.println();
root.traverseInOrder();
System.out.println();
root.traversePostOrder();
/*
输出结果:
5
2
1
9
7
41
9
2
7
5
49
1
7
2
4
5*/
while(true)
{
stack.push(temp);
if(temp.left!=null)
temp=temp.left;
else
{
System.out.println(temp.value);
if(temp.right!=null)
{
stack.push(temp);
temp=temp.right;
}
else
{
if(!stack.empty())
{
temp=stack.pop();
System.out.println(temp.value);
}
if(!stack.empty())
{
temp=stack.top();
}
else{break;}
}
}
}
没有验证过..只是按自己的逻辑写的
LZ参考下吧.
public class BiTree {
public static void main(String[] args) {
BiTree b = new BiTree();
b.add(4);
b.add(9);
b.add(5);
b.add(2);
b.add(1);
b.add(5);
b.add(8);
b.zhongxu();
} class Node {
private int data;
private Node left;
private Node right; public Node(int n) {
this.data = n;
left = right = null;
} public void add(Node node) {
if (node.data < data) {
if (left == null) {
left = node;
} else {
left.add(node);
}
} else {
if (right == null) {
right = node;
} else {
right.add(node);
}
}
} public void zhongxu() {
if (left != null)
left.zhongxu();
System.out.println(data);
if (right != null)
right.zhongxu();
}
} Node root; public void add(int n) {
Node node = new Node(n);
if (root == null) {
root = node;
} else {
root.add(node);
}
} public void zhongxu() {
if (root == null) {
return;
}
root.zhongxu();
}}