描述:假设目标节点值是6
打印结果:
10 9 8 6
二叉排序树的实现在这里不解释了,我有一博客写的就是实现二叉排序树。
这里解释一下怎么递归实现该功能,假设该节点在树中:
1.首先判断当前节点是否为空,如果为空,结束;否则向右递归遍历,遍历方式采用先右后中,最后向左递归,遍历方式恰好和中序遍历相反
2.当递归过程中发现当前节点值与目标节点值相同,向右递归遍历,当递归结束返回到目标节点时,打印当前值,结束函数
下面是实现代码:
//右中左的中序遍历 //TreeNode中的方法 public void newMidOrder(T data) { if(this.right != null) { this.right.newMidOrder(data); } if(this.data.compareTo(data) > 0) { System.out.print(this.data + " "); }else if(this.data.compareTo(data) == 0){ System.out.print(this.data); return; } if(this.left != null) { this.left.newMidOrder(data); } }
//BinaryTree中的方法 //右中左遍历 public void newMidOrder(T data) { if(this.root != null) { this.root.newMidOrder(data); }else { System.out.println("当前二叉树为空,请先创建二叉树"); } }
完整代码:
package colllection_2; import java.util.Scanner; public class BTTest { public static void main(String[] args) { BinaryTreebinaryTree = new BinaryTree (); Scanner scan = new Scanner(System.in); int number = scan.nextInt(); for(int i = 0;i < number;i++) { int data = scan.nextInt(); binaryTree.add(new TreeNode (data)); } int targerData = scan.nextInt(); binaryTree.newMidOrder(Integer.valueOf(targerData)); } } //二叉树 class BinaryTree >{ private TreeNode root; public BinaryTree() { } public void setRoot(TreeNode root) { this.root = root; } public TreeNode getRoot(){ return this.root; } //增加节点 public void add(TreeNode node) { if(root == null) { root = node; }else { root.add(node); } } //前序遍历 public void preOrder() { if(this.root != null) { this.root.preOrder(); }else { System.out.println("当前二叉树为空,请先创建二叉树!!"); } } //中序遍历 public void midOrder() { if(this.root != null) { this.root.midOrder(); }else { System.out.println("当前二叉树为空,请先创建二叉树"); } } //右中左遍历 public void newMidOrder(T data) { if(this.root != null) { this.root.newMidOrder(data); }else { System.out.println("当前二叉树为空,请先创建二叉树"); } } } //树节点 class TreeNode >{ private T data; private TreeNode left; private TreeNode right; public TreeNode(){ this.left = null; this.right = null; } public TreeNode(T data) { this.data = data; } public void setData(T data) { this.data = data; } public T getData() { return this.data; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getLeft() { return this.left; } public void setRight(TreeNode right) { this.right = right; } public TreeNode getRight() { return this.right; } //递归添加节点 public void add(TreeNode node) { if(node==null){ return; } if(node.data == this.data) { return; } if(this.data.compareTo(node.data) > 0) { if(this.left == null) { this.left = node; }else { this.left.add(node); } }else { if(this.right == null) { this.right = node; }else { this.right.add(node); } } } //前序遍历二叉树 public void preOrder() { System.out.print(this.data+" "); if(this.left != null) { this.left.preOrder(); } if(this.right != null) { this.right.preOrder(); } } //中序遍历 public void midOrder(){ if(this.left != null) { this.left.midOrder(); } System.out.print(this.data+" "); if(this.right != null) { this.right.midOrder(); } } //右中左的中序遍历 public void newMidOrder(T data) { if(this.right != null) { this.right.newMidOrder(data); } if(this.data.compareTo(data) > 0) { System.out.print(this.data + " "); }else if(this.data.compareTo(data) == 0){ System.out.print(this.data); return; } if(this.left != null) { this.left.newMidOrder(data); } } //查找目标节点 public TreeNode nodeSearch(T data) { if(this.data == data) { return this; }else if(this.data.compareTo(data) > 0) { if(this.left != null) { return this.left.nodeSearch(data); } return null; }else { if(this.right != null) { return this.right.nodeSearch(data); } return null; } } }
输入格式:
第一行数据个数
第二行输入数据
第三行输入目标节点的值
输出:逆序打印树中不小于目标节点值的所有值
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)