在Java中实现二叉搜索树插入操作
我有一个TreeNode类,它表示二叉树的节点在Java中实现二叉搜索树插入操作,java,data-structures,binary-search-tree,insertion,Java,Data Structures,Binary Search Tree,Insertion,我有一个TreeNode类,它表示二叉树的节点 public class TreeNode { private static Object key=null; private static Object value=null; private TreeNode parent; private TreeNode left=null; private TreeNode right=null; /** * @return the value */ public static Object
public class TreeNode {
private static Object key=null;
private static Object value=null;
private TreeNode parent;
private TreeNode left=null;
private TreeNode right=null;
/**
* @return the value
*/
public static Object getValue() {
return value;
}
/**
* @param aValue the value to set
*/
public static void setValue(Object aValue) {
value = aValue;
}
public TreeNode()
{
this(key,value);
}
public TreeNode(Object key,Object value)
{
this.key = key;
this.value = value;
}
/**
* @return the key
*/
public Object getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(Object key) {
this.key = key;
}
/**
* @return the parent
*/
public TreeNode getParent() {
return parent;
}
/**
* @param parent the parent to set
*/
public void setParent(TreeNode parent) {
this.parent = parent;
}
/**
* @return the left
*/
public TreeNode getLeftChild() {
return left;
}
/**
* @param left the left to set
*/
public void setLeftChild(TreeNode left) {
this.left = left;
}
/**
* @return the right
*/
public TreeNode getRightChild() {
return right;
}
/**
* @param right the right to set
*/
public void setRightChild(TreeNode right) {
this.right = right;
}
}
我有一个BinarySearchTree类
public class BinarySearchTree implements DataStructures.interfaces.BinarySearchTree {
private int size=0;
private TreeNode root = new TreeNode();
@Override
public void insert(Object key, Object value)
{
insertOperation(key,value,root);
}
private void insertOperation(Object element, Object value, TreeNode node)
{
if(node.getKey() == null) {
node.setKey(element);
node.setValue(value);
}
else
{
if((int) node.getKey() > (int) element)
{
if(node.getLeftChild() != null)
insertOperation(element,value,node.getLeftChild());
else
{
TreeNode child = new TreeNode(element, value);
child.setKey(element);
child.setValue(value);
child.setParent(node);
node.setLeftChild(child);
size++;
}
}
else if((int) node.getKey() <= (int) element)
{
if(node.getRightChild() != null)
insertOperation(element,value,node.getRightChild());
else
{
TreeNode child = new TreeNode(element, value);
child.setKey(element);
child.setValue(value);
child.setParent(node);
node.setRightChild(child);
size++;
}
}
}
}
///more methods
}
我告诉java要做的就是,如果所讨论的节点没有左子节点,那么创建一个左子节点,并将当前节点的左侧对象设置为子对象
如果有问题的节点确实有一个左子节点,则检查该子节点,并通过为有问题的节点的子节点调用相同的函数,查看是否应在左或右插入新节点。。。我不明白当我设置子节点的键值时,为什么节点(传递的TreeNode对象)的键会被更新 我不太明白你的意思 问题是,当我创建一个子节点并设置父子链接时。父节点(我传递的节点对象)的值也会更新并引用子对象 但我确实注意到一件会导致错误的事情:
else if((int)node.getKey()为什么你的key
和value
是静态对象?这意味着所有树节点都将共享相同的key/value。删除static关键字,它应该可以正常工作。你应该显示你的setParent和set*子方法,以确保..@SB.我已经添加了setter和getter方法。并从getter中删除static
还有treeNode中的setter。是的,刚刚解决了这个问题!谢谢。这是一个迟钝的错误。说得好。我将删除它和另一个禁止插入重复键的案例。问题是,当我创建treeNode类的新“child”对象时,“root”对象的“key”和“value”属性更改为“child”的“key”和“value”。原因是我的键和值属性是静态的。因此,所有对象都共享相同的静态值。我的IDE警告过我这一点,但我忽略了它。现在我意识到了倾听警告的重要性。哈哈。我明白了。有时候警告很烦人,但大多数时候都很有用。祝你好运=)@Anupam
if(node.getLeftChild() != null)
insertOperation(element,value,node.getLeftChild());
else
{
TreeNode child = new TreeNode(element, value);
child.setKey(element);
child.setValue(value);
child.setParent(node);
node.setLeftChild(child);
size++;
}
else if((int) node.getKey() <= (int) element)