Java 有没有一种方法可以在不将父节点合并到程序中的情况下从BST中删除节点?
这是我的Data Structures类中的一个赋值,我的教授希望我创建一个remove方法,该方法基于整数键从树中删除节点。如果找不到该键,则该函数不应执行任何操作。我发现的所有示例都是基于具有父节点(左、右节点)的树。此程序只有一个左右节点。我尽量把我认为相关的课程包括进去。几乎只有打印树木的方法才被采用Java 有没有一种方法可以在不将父节点合并到程序中的情况下从BST中删除节点?,java,binary-search-tree,Java,Binary Search Tree,这是我的Data Structures类中的一个赋值,我的教授希望我创建一个remove方法,该方法基于整数键从树中删除节点。如果找不到该键,则该函数不应执行任何操作。我发现的所有示例都是基于具有父节点(左、右节点)的树。此程序只有一个左右节点。我尽量把我认为相关的课程包括进去。几乎只有打印树木的方法才被采用 public class BinarySearchTree { private Node root; public BinarySearchTree() { this.
public class BinarySearchTree
{
private Node root;
public BinarySearchTree() { this.root = null; }
public BinarySearchTree ( Node root ) { this.root = root; }
public void add ( Integer key, String value )
{
if (root == null)
root = new Node( key, value);
else
root.add( key, value);
}
public boolean contains ( Integer key )
{
return root == null ? null : ( boolean ) root.contains( key );
}
public void remove ( Integer key )
{
}
}
public class Node
{
public Node left;
public Node right;
public Integer key;
public String value;
public Node (String value, Node left, Node right)
{
this.value = value;
this.left = left;
this.right = right;
}
public Node (String value)
{
this.value = value;
this.left = null;
this.right = null;
}
public Node (Integer key, String value)
{
this.key = key;
this.value = value;
}
public void add ( Integer key, String value )
{
if ( key.compareTo ( this.key ) < 0)
{
if ( left != null )
left.add ( key, value );
else
left = new Node ( key, value );
}
else if ( key.compareTo ( this.key ) > 0 )
{
if ( right != null )
right.add ( key, value );
else
right = new Node ( key, value);
}
else
this.value = value;
}
public Serializable contains ( Integer key )
{
if ( this.key == ( key ) )
return value;
if ( key.compareTo ( this.key ) < 0 )
return left == null ? null : left.contains ( key );
else
return right == null ? null : right.contains ( key );
}
public void remove ( Integer key )
{
}
}
公共类二进制搜索树
{
私有节点根;
public BinarySearchTree(){this.root=null;}
公共二进制搜索树(节点根){this.root=root;}
公共void add(整型键、字符串值)
{
if(root==null)
根=新节点(键、值);
其他的
root.add(键、值);
}
公共布尔包含(整数键)
{
返回root==null?null:(布尔)root.contains(键);
}
公共无效删除(整数键)
{
}
}
公共类节点
{
公共节点左;
公共节点权;
公开整数密钥;
公共字符串值;
公共节点(字符串值、左节点、右节点)
{
这个值=值;
this.left=左;
这个。右=右;
}
公共节点(字符串值)
{
这个值=值;
this.left=null;
this.right=null;
}
公共节点(整数键、字符串值)
{
this.key=key;
这个值=值;
}
公共void add(整型键、字符串值)
{
如果(key.compareTo(this.key)<0)
{
if(左!=null)
left.add(键、值);
其他的
左=新节点(键、值);
}
如果(key.compareTo(this.key)>0,则为else
{
if(右!=null)
右。添加(键、值);
其他的
右=新节点(键、值);
}
其他的
这个值=值;
}
公共可序列化包含(整数键)
{
如果(this.key==(key))
返回值;
如果(key.compareTo(this.key)<0)
返回left==null?null:left.contains(键);
其他的
return right==null?null:right.contains(key);
}
公共无效删除(整数键)
{
}
}
这里,remove()
没有节点
类有父类
Node remove(Node root, int key) {
if (root == null) return root;
if (key < root.key)
root.left = remove(root.left, key);
else if (key > root.key)
root.right = remove(root.right, key);
else {
// node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node with two children: Get the inorder successor (smallest
// in the right subtree)
root.key = minValue(root.right);
// Delete the inorder successor
root.right = remove(root.right, root.key);
}
return root;
}
节点删除(节点根,int键){
if(root==null)返回root;
if(keyroot.key)
root.right=删除(root.right,键);
否则{
//只有一个子节点或没有子节点的节点
if(root.left==null)
返回root.right;
else if(root.right==null)
返回root.left;
//具有两个子节点的节点:获取顺序后继节点(最小)
//在右子树中)
root.key=minValue(root.right);
//删除顺序继承项
root.right=remove(root.right,root.key);
}
返回根;
}
专用节点根目录代码>-这看起来像是我的父节点。@HarshalParekh我想他说的是每个节点中的父指针。布莱恩,仔细看看。有许多bst delete示例不需要父指针。例如,任何好的数据结构教科书都会涵盖这一点。