[英]How to traverse Binary search tree and only print certain elements (JAVA)
我自己实现了一个二叉搜索树,一切似乎都很好。 在这棵树中,我存储了我自己的具有(字符串)状态属性的 class 的元素。 现在我只想打印状态为“YES”的树的元素。
在我的树 class 中,我使用它来打印:
public void print()
{
//Here you make the instance of the previously described abstract treeAction
//just for 1 purpose, used here (Note brackets!)
traverseInOrder(new TreeAction() {
//Only here u provide the implementation of run that was previously not defined
//u start
public void run(TreeNode n)
{
System.out.println(((TreeNode)n).getValue());
}
});
//We implemented the same thing in the treeprinter class, instead of defining the treeAction within the arguments here
//This implementation has the advantage of not creating as much overhead: no new class that extends treeAction needs to be created for every action
}
和
public void traverseNode(TreeNode n,TreeAction action)
{
if(n != null) {
if(n.getLeftTree() != null) traverseNode(n.getLeftTree(),action);
action.run(n);
if(n.getRightTree() != null) traverseNode(n.getRightTree(),action);
}
}
public void traverseInOrder(TreeAction action)
{
traverseNode(root,action);
}
我想过只打印所有元素并存储它们,然后再次遍历树以查找每个元素并检查状态,然后打印它们,但这似乎效率极低。 理想情况下,这只会发生在 1 次树遍历中。
我还考虑在树 class 中实现一个替代打印语句,它看起来像:
public void print(**Boolean condition**)
{
traverseInOrder(new TreeAction() {
//Only here u provide the implementation of run that was previously not defined
//u start
public void run(TreeNode n)
{
**if(condition)**{
System.out.println(((TreeNode)n).getValue());
}
}
});
}
但是不可能在其中集成我的 class 的 getStatus 方法。 (这样做似乎是非常糟糕的做法)。
所以我有点失落,任何建议将不胜感激。
我自己实现了一个二叉搜索树,一切似乎都很好。 在这棵树中,我存储了我自己的具有(字符串)状态属性的 class 的元素。 现在我只想打印状态为“YES”的树的元素。
在我的树 class 中,我使用它来打印:
public void print()
{
//Here you make the instance of the previously described abstract treeAction
//just for 1 purpose, used here (Note brackets!)
traverseInOrder(new TreeAction() {
//Only here u provide the implementation of run that was previously not defined
//u start
public void run(TreeNode n)
{
System.out.println(((TreeNode)n).getValue());
}
});
//We implemented the same thing in the treeprinter class, instead of defining the treeAction within the arguments here
//This implementation has the advantage of not creating as much overhead: no new class that extends treeAction needs to be created for every action
}
和
public void traverseNode(TreeNode n,TreeAction action)
{
if(n != null) {
if(n.getLeftTree() != null) traverseNode(n.getLeftTree(),action);
action.run(n);
if(n.getRightTree() != null) traverseNode(n.getRightTree(),action);
}
}
public void traverseInOrder(TreeAction action)
{
traverseNode(root,action);
}
我想过只打印所有元素并存储它们,然后再次遍历树以查找每个元素并检查状态,然后打印它们,但这似乎效率极低。 理想情况下,这只会发生在 1 次树遍历中。
我还考虑在树 class 中实现一个替代打印语句,它看起来像:
public void print(**Boolean condition**)
{
traverseInOrder(new TreeAction() {
//Only here u provide the implementation of run that was previously not defined
//u start
public void run(TreeNode n)
{
**if(condition)**{
System.out.println(((TreeNode)n).getValue());
}
}
});
}
但是不可能在其中集成我的 class 的 getStatus 方法。 (这样做似乎是非常糟糕的做法)。
所以我有点失落,任何建议将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.