如何在Java中创建一个使用二进制搜索树获取前一个节点的方法?
我正在研究一种方法,该方法使用二叉搜索树获取前一个节点。现在我想我明白了,但是我正在努力处理我的if语句 说明如下:getPrevNode(BSTNode)方法应在树中找到参数前面的节点。这是我正在使用的算法 •如果节点具有左子节点,则向下移动左子树以获得最大节点 •否则,如果节点有父节点,我们需要按如下方式向上移动树: •如果节点是正确的子节点,则返回其父节点 •如果节点是左子节点,则向上移动树,直到您是右子节点并返回其 母公司 •如果您到达根节点,并且从来都不是正确的子节点,那么就没有以前的节点 请注意,这也是一个辅助方法。因此,这里是我的代码,我到目前为止,遵循该算法如何在Java中创建一个使用二进制搜索树获取前一个节点的方法?,java,recursion,binary-search-tree,nodes,Java,Recursion,Binary Search Tree,Nodes,我正在研究一种方法,该方法使用二叉搜索树获取前一个节点。现在我想我明白了,但是我正在努力处理我的if语句 说明如下:getPrevNode(BSTNode)方法应在树中找到参数前面的节点。这是我正在使用的算法 •如果节点具有左子节点,则向下移动左子树以获得最大节点 •否则,如果节点有父节点,我们需要按如下方式向上移动树: •如果节点是正确的子节点,则返回其父节点 •如果节点是左子节点,则向上移动树,直到您是右子节点并返回其 母公司 •如果您到达根节点,并且从来都不是正确的子节点,那么就没有以前的
private BSTNode<E> getPrevNode(BSTNode<E> node)
{
if(node.left != null)
{
return getPrevNode(node.left);
}
else if(node.parent != null)
{
if(node == node.right)
{
return node.parent;
}
else if(node == node.left)
{
return node.parent;
}
}
return getPrevNode(node);
}
private BSTNode getPrevNode(BSTNode节点)
{
if(node.left!=null)
{
返回getPrevNode(node.left);
}
else if(node.parent!=null)
{
if(node==node.right)
{
返回node.parent;
}
else if(node==node.left)
{
返回node.parent;
}
}
返回getPrevNode(节点);
}
现在我知道它不准确,但这就是为什么我要问。我会尝试给出一些关于这段代码的信息,但我会留下一些方法,因为我不希望它太长
public class BinarySearchTree<E extends Comparable<E>>
{
private BSTNode<E> root; // root of overall tree
private int numElements;
private BSTNode<E> first;
// post: constructs an empty search tree
public BinarySearchTree()
{
this.root = null;
this.numElements = 0;
}
private BSTNode<E> getPrevNode(BSTNode<E> node)
{
if(node.left != null)
{
return getPrevNode(node.left);
}
else if(node.parent != null)
{
if(node == node.right)
{
return node.parent;
}
else if(node == node.left)
{
return node.parent;
}
}
return getPrevNode(node);
}
public class Iterator
{
private BSTNode<E> currentNode;
public Iterator()
{
currentNode = first;
}
public boolean hasNext()
{
return currentNode != null;
}
public E next()
{
E value = currentNode.data;
currentNode = currentNode.next;
return value;
}
}
private static class BSTNode<E>
{
public E data;
public BSTNode<E> left;
public BSTNode<E> right;
public BSTNode<E> parent;
public BSTNode<E> next;
public BSTNode(E data)
{
this(data, null, null, null, null);
}
public BSTNode(E data, BSTNode<E> left, BSTNode<E> right, BSTNode<E> parent, BSTNode<E> next)
{
this.data = data;
this.left = left;
this.right = right;
this.parent = parent;
this.next = next;
}
}
}
公共类二进制搜索树
{
私有节点根;//整个树的根
私人住宅;
私有节点优先;
//post:构造一个空的搜索树
公共二进制搜索树()
{
this.root=null;
这个数值等于0;
}
专用BSTNode getPrevNode(BSTNode节点)
{
if(node.left!=null)
{
返回getPrevNode(node.left);
}
else if(node.parent!=null)
{
if(node==node.right)
{
返回node.parent;
}
else if(node==node.left)
{
返回node.parent;
}
}
返回getPrevNode(节点);
}
公共类迭代器
{
私有节点currentNode;
公共迭代器()
{
currentNode=第一;
}
公共布尔hasNext()
{
返回currentNode!=null;
}
公共教育
{
E值=currentNode.data;
currentNode=currentNode.next;
返回值;
}
}
私有静态类节点
{
公共电子数据;
公共节点左;
公共节点权;
公共节点父节点;
下一个公共节点;
公共节点(E数据)
{
这(数据,null,null,null,null);
}
公共BSTNode(数据,BSTNode左,BSTNode右,BSTNode父,BSTNode下)
{
这个数据=数据;
this.left=左;
这个。右=右;
this.parent=parent;
this.next=next;
}
}
}
我希望这些信息是有用的 试试这个:
private BSTNode<E> getPrevNode(BSTNode<E> node) {
if(node.left != null) {
node = node.left;
while(node.right != null) {
node = node.right;
}
return node;
} else if(node.parent != null) {
// If the node is a right child, return its parent
if(node.parent.right == node) {
return node.parent;
}
// If the node is a left child, move up the tree
// until you are a right child and return its parent
if(node.parent.left == node) {
while(node.parent.right != node) {
// If you reach the root and are never a right child, no previous node return null
if(node.parent == root) {
return null;
}
node = node.parent;
}
return node.parent;
}
}
return null;
}
private BSTNode getPrevNode(BSTNode节点){
if(node.left!=null){
node=node.left;
while(node.right!=null){
node=node.right;
}
返回节点;
}else if(node.parent!=null){
//如果节点是正确的子节点,则返回其父节点
if(node.parent.right==节点){
返回node.parent;
}
//如果节点是左子节点,请向上移动树
//直到你是一个正确的孩子,并返回其父母
if(node.parent.left==节点){
while(node.parent.right!=节点){
//如果您到达根节点并且从来都不是正确的子节点,那么前面的节点都不会返回null
if(node.parent==根){
返回null;
}
node=node.parent;
}
返回node.parent;
}
}
返回null;
}
利亚姆的答案到目前为止也是正确的,但这里有另一种解决方法
private BSTNode<E> getPrevNode(BSTNode<E> node)
{
if(node.left != null)
{
node = node.left;
while(node.right != null)
{
node = node.right;
}
return node;
}
else if(node.parent != null)
{
if(node.parent.right == node)
{
return node.parent;
}
if(node.parent.left == node)
{
while(node.parent != null && node.parent.left == node)
{
node = node.parent;
}
if(node == root)
{
return null;
}
else
{
return node.parent;
}
}
}
return null;
}
private BSTNode getPrevNode(BSTNode节点)
{
if(node.left!=null)
{
node=node.left;
while(node.right!=null)
{
node=node.right;
}
返回节点;
}
else if(node.parent!=null)
{
if(node.parent.right==节点)
{
返回node.parent;
}
if(node.parent.left==节点)
{
while(node.parent!=null&&node.parent.left==node)
{
node=node.parent;
}
如果(节点==根)
{
返回null;
}
其他的
{
返回node.parent;
}
}
}
返回null;
}
这里有一个代码更少的解决方案
private BSTNode<E> getPrevNode(BSTNode<E> node, int val) {
if (node == null) return null;
BSTNode<E> prev = null;
while (node != null) {
if (val < node.data) {
prev = node;
node = node.left;
} else if (val > node.data) {
prev = node;
node = node.right;
} else {
break;
}
}
return node != null ? prev : null;
}
private BSTNode getPrevNode(BSTNode节点,int val){
如果(node==null)返回null;
BSTNode prev=null;
while(节点!=null){
if(valnode.data){
prev=节点;
node=node.right;
}否则{
打破
}
}
返回节点!=null?上一个:null;
}
我会尝试一下,不过我觉得这很准确。