如何打印大于树中特定节点的所有节点?
How can print all nodes that are greater than specific node in tree?
我编写此代码来搜索键然后打印所有大于此键的节点
public boolean search(int data) {
return search(root, data);
}
private boolean search(BSTnode p, int data) {
if (p == null)
return false;
else {
// if the data we are searching for is found at p (at the current root)
if (data == p.getData())
return true;
else if (data < p.getData())
return search(p.getLeft(), data);
else
return search(p.getRight(), data);
}
}
我尝试让这段代码打印所有大于键值的节点
public void printAllNodeGreaterMyFoundNode(int data) {
printAllNodeGreaterMyFoundNode(root, data);
}
private void printAllNodeGreaterMyFoundNode(BSTnode p, int data){
if(data<p.getData())
System.out.print(p.getData());
最后这是我的 void main
System.out.print("> What value do you want to search for: ");
value = input.nextInt();
if (myTree.search(value)){
System.out.println("> " + value + " was found in the tree and the values greater than "+value+"are:");
myTree.printAllNodeGreaterMyFoundNode(value);
谁能帮帮我?
您可以通过遍历整棵树并使用简单的 if
比较器语句来做到这一点:
private void printAllNodeGreaterMyFoundNode(BSTnode p, int data){
if(p == null){
//do nothing reached the end
return;
}
else{
if(p.getData() >= data){
System.out.println(p.getData());
}
printAllNodeGreaterMyFoundNode(p.getLeft(), data);
printAllNodeGreaterMyFoundNode(p.getRight(), data);
}
}
方法一:当每个BSTNode都可以访问其父节点时
public void printGreater(BSTNode root, int value) {
BSTNode data = search(root, value);
BSTNode succ;
if (data != null) {
System.out.println("> " + value + " was found in the tree and the values greater than " + value + "are:");
while (data != null) {
succ = successor(data);
if (succ != null) {
System.out.println(succ.data);
}
data = succ;
}
}
}
private BSTNode search(BSTNode p, int data) {
if (p == null)
return null;
else {
// if the data we are searching for is found at p (at the current
// root)
if (data == p.data)
return p;
else if (data < p.data)
return search(p.left, data);
else
return search(p.right, data);
}
}
在主要方法中
System.out.print("> What value do you want to search for: ");
value = input.nextInt();
printGreater(bstRootNode, value);
方法二:如果你没有权限访问BSTNode中的父节点class
public void printGreaterWithoutParent(BSTNode root, int value) {
BSTNode data = search(root, value);
if (data != null) {
System.out
.println("> " + value + " was found in the tree and the values greater than " + value + "are:");
greaterNodes(root, data);
}
}
private void greaterNodes(BSTNode n, BSTNode k) {
if (n == null)
return;
greaterNodes(n.left, k);
if (n.data > k.data) {
System.out.println(n.data);
}
greaterNodes(n.right, k);
}
在主要方法中
System.out.print("> What value do you want to search for: ");
value = input.nextInt();
printGreaterWithoutParent(bstRootNode, value);
一旦找到BS树中的节点,就可以在右子树上遍历,以搜索到的节点为根。
我编写此代码来搜索键然后打印所有大于此键的节点
public boolean search(int data) {
return search(root, data);
}
private boolean search(BSTnode p, int data) {
if (p == null)
return false;
else {
// if the data we are searching for is found at p (at the current root)
if (data == p.getData())
return true;
else if (data < p.getData())
return search(p.getLeft(), data);
else
return search(p.getRight(), data);
}
}
我尝试让这段代码打印所有大于键值的节点
public void printAllNodeGreaterMyFoundNode(int data) {
printAllNodeGreaterMyFoundNode(root, data);
}
private void printAllNodeGreaterMyFoundNode(BSTnode p, int data){
if(data<p.getData())
System.out.print(p.getData());
最后这是我的 void main
System.out.print("> What value do you want to search for: ");
value = input.nextInt();
if (myTree.search(value)){
System.out.println("> " + value + " was found in the tree and the values greater than "+value+"are:");
myTree.printAllNodeGreaterMyFoundNode(value);
谁能帮帮我?
您可以通过遍历整棵树并使用简单的 if
比较器语句来做到这一点:
private void printAllNodeGreaterMyFoundNode(BSTnode p, int data){
if(p == null){
//do nothing reached the end
return;
}
else{
if(p.getData() >= data){
System.out.println(p.getData());
}
printAllNodeGreaterMyFoundNode(p.getLeft(), data);
printAllNodeGreaterMyFoundNode(p.getRight(), data);
}
}
方法一:当每个BSTNode都可以访问其父节点时
public void printGreater(BSTNode root, int value) {
BSTNode data = search(root, value);
BSTNode succ;
if (data != null) {
System.out.println("> " + value + " was found in the tree and the values greater than " + value + "are:");
while (data != null) {
succ = successor(data);
if (succ != null) {
System.out.println(succ.data);
}
data = succ;
}
}
}
private BSTNode search(BSTNode p, int data) {
if (p == null)
return null;
else {
// if the data we are searching for is found at p (at the current
// root)
if (data == p.data)
return p;
else if (data < p.data)
return search(p.left, data);
else
return search(p.right, data);
}
}
在主要方法中
System.out.print("> What value do you want to search for: ");
value = input.nextInt();
printGreater(bstRootNode, value);
方法二:如果你没有权限访问BSTNode中的父节点class
public void printGreaterWithoutParent(BSTNode root, int value) {
BSTNode data = search(root, value);
if (data != null) {
System.out
.println("> " + value + " was found in the tree and the values greater than " + value + "are:");
greaterNodes(root, data);
}
}
private void greaterNodes(BSTNode n, BSTNode k) {
if (n == null)
return;
greaterNodes(n.left, k);
if (n.data > k.data) {
System.out.println(n.data);
}
greaterNodes(n.right, k);
}
在主要方法中
System.out.print("> What value do you want to search for: ");
value = input.nextInt();
printGreaterWithoutParent(bstRootNode, value);
一旦找到BS树中的节点,就可以在右子树上遍历,以搜索到的节点为根。