Java 在无父指针的二叉搜索树中查找元素的后续元素
我试图实现一种方法,当给定一个元素时,它将返回该元素的后续元素。我已经实现了一些helper方法,但是不能引用父对象,也不知道如何正确地递归树。在元素有右子树的情况下,我可以简单地调用右子树上最小的方法Java 在无父指针的二叉搜索树中查找元素的后续元素,java,binary-search-tree,Java,Binary Search Tree,我试图实现一种方法,当给定一个元素时,它将返回该元素的后续元素。我已经实现了一些helper方法,但是不能引用父对象,也不知道如何正确地递归树。在元素有右子树的情况下,我可以简单地调用右子树上最小的方法 /** * NonEmptyBinaryTree - this is a binary search tree that is either an inner node * of the tree or a leaf node. Leaf nodes will have 2 empty n
/**
* NonEmptyBinaryTree - this is a binary search tree that is either an inner node
* of the tree or a leaf node. Leaf nodes will have 2 empty nodes attached to
* the right and the left subtrees. Note that the insert and remove operation return
* the changed tree and they will generally modify existing trees.
*/
public class NonEmptyBinaryTree <T extends Comparable<T>> extends BinaryTree<T> {
T data; // data of the root node of this tree
BinaryTree<T> left, right; // left and right sub-trees
/**
* Create a NonEmptyBinaryTree tree with root node.
* The tree will not have sub-trees.
*
* @param data the data of the root node.
*/
public NonEmptyBinaryTree(T data) { //Equivalent to BSTree construct.
this.data = data;
left = new EmptyBinaryTree<T>();
right = new EmptyBinaryTree<T>();
}
public static void main(String[] args) {
NonEmptyBinaryTree tree = new NonEmptyBinaryTree(7); //Root node
tree.insert(3);
tree.insert(4);
tree.insert(10);
}
/**
* Create a NonEmptyBinaryTree with left and right sub-trees
* @param data value of the root node
* @param left sub-tree of the new NonEmptyBinaryTree tree
* @param right sub-tree of the new NonEmptyBinaryTree tree
*/
public NonEmptyBinaryTree(T data, BinaryTree<T> left, BinaryTree<T> right) {
this.data = data;
this.left = left;
this.right = right;
}
/**
* Insert a new node whose value is d into the existing tree.
* This function should return the binary tree with d inserted.
* If the tree has already a node with d, do not create a new node
* and return the original tree.
*
* Hint: You can implement insert function recursively.
* (Each subtree (left or right) is a tree which has insert function)
*
* @param d data of the new node
* @return BinaryTree<T>
*/
public BinaryTree<T> insert(T d) {
// TODO: Add your implementation here
if (this.data == null) { //IF no tree exists, create a new one and assign d to the root
return new NonEmptyBinaryTree<>(d);
}
if (this.data.equals(d)) { //If root is the same just return the tree
return this;
}
if (d.compareTo(this.data) > 0 ) { //If its greater than the data, go down thr right subtree
this.right = this.right.insert(d);
return new NonEmptyBinaryTree<>(data, left, right);
}
else { //If less than make the trees left insert the node
this.left = this.left.insert(d);
return new NonEmptyBinaryTree<>(data, left, right);
}
}
@Override
public T successor(T element){
if (data == null) return null;
else return;
}
/**
* This function will find the biggest node in a tree.
*/
@Override
public T biggest() {
if (right.isEmpty()) return data;
else return right.biggest();
}
/**
* This function will find the smallest node in a tree.
*/
@Override
public T smallest() {
if (left.isEmpty()) return data;
else return left.smallest();
}
/**
* Find whether a specific data is in the tree or not.
*/
@Override
public boolean find(T d) {
if (data == d) return true;
else if (d.compareTo(data) < 0) return left.find(d);
else return right.find(d);
}
}
/**
*NoneEmptyBinaryTree-这是一个二叉搜索树,它是一个内部节点
*树或叶节点的。叶节点将有2个空节点连接到
*右子树和左子树。请注意,插入和移除操作返回
*更改的树,它们通常会修改现有树。
*/
公共类NoneEmptyBinaryTree扩展了BinaryTree{
T data;//此树的根节点的数据
BinaryTree left,right;//左和右子树
/**
*创建一个具有根节点的非空二叉树。
*该树将没有子树。
*
*@param data根节点的数据。
*/
公共非空二进制树(T数据){//等价于BSTree构造。
这个数据=数据;
左=新的空二进制树();
右=新的EmptyBinaryTree();
}
公共静态void main(字符串[]args){
NoneEmptyBinaryTree=新的NoneEmptyBinaryTree(7);//根节点
树。插入(3);
树。插入(4);
树。插入(10);
}
/**
*创建具有左、右子树的非空二叉树
*@param根节点的数据值
*@param新非空二叉树的左子树
*新非空二叉树的@param right子树
*/
公共非空二叉树(T数据,左二叉树,右二叉树){
这个数据=数据;
this.left=左;
这个。右=右;
}
/**
*在现有树中插入一个值为d的新节点。
*此函数应返回插入d的二叉树。
*如果树已经有一个带有d的节点,请不要创建新节点
*并返回原始树。
*
*提示:您可以递归地实现插入函数。
*(每个子树(左或右)都是具有插入功能的树)
*
*@param d新节点的数据
*@return二进制树
*/
公共二叉树插入(td){
//TODO:在此处添加您的实现
如果(this.data==null){//如果不存在树,则创建一个新树并将d分配给根
返回新的非空二叉树(d);
}
如果(this.data.equals(d)){//如果根相同,则返回树
归还这个;
}
如果(d.compareTo(this.data)>0{//如果它大于数据,则向下搜索右子树
this.right=this.right.插入(d);
返回新的非空二进制树(数据,左,右);
}
else{//如果小于,则使树向左插入节点
this.left=this.left.insert(d);
返回新的非空二进制树(数据,左,右);
}
}
@凌驾
公共T后继(T元素){
if(data==null)返回null;
否则返回;
}
/**
*此函数将查找树中最大的节点。
*/
@凌驾
公共交通{
if(right.isEmpty())返回数据;
否则返回右边。最大();
}
/**
*此函数将查找树中的最小节点。
*/
@凌驾
公共交通{
if(left.isEmpty())返回数据;
否则返回左边;
}
/**
*查找特定数据是否在树中。
*/
@凌驾
公共布尔查找(td){
如果(数据==d)返回true;
否则如果(d.compareTo(data)<0)返回left.find(d);
否则返回右边。找到(d);
}
}
您所说的“没有父指针”是什么意思?@ScientificMethod节点没有附加父值观察1:由于无法保证compareTo
与equals
一致,因此insert
和find
方法都是错误的<只有当compareTo
返回0
时,才应认为code>数据和d
相等,因此在插入中使用等于和查找中使用=
都是错误的。在find
中使用=
肯定是错误的。观察2:代码一致性使代码更容易理解并减少编码错误,因此在insert
中测试compareTo(…)>0
,但find
中的compareTo(…)<0
是不一致的。您应该更改代码,以便以相同的方式比较两者。--此外,如果真块无条件返回,则无需使用else
。观察3:要了解如何在二元搜索树中查找下一个值(节点),请尝试web搜索,例如。令人惊讶的是,有多少问题已经有了答案。你所需要做的就是寻找它们,所以现在请做你的研究和寻找。