0

我有一个表示二叉树节点的 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 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
}

问题是当我创建一个子节点并设置父子链接时。父节点(我传递的节点对象)的值也被更新并引用子对象。

那不是我的意图。

我想创建一个可以通过“根”树节点对象访问的树节点对象链。

但这没有发生,我不明白我做错了什么。

我知道问题出在此代码片段的逻辑上(不仅用于插入左侧,还用于插入左孩子和右孩子),但我不明白究竟是什么。

           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++;
            }

我告诉java要做的就是如果有问题的节点没有左子节点,那么创建一个左子节点并将当前节点的左侧对象设置为子对象。

如果有问题的节点确实有一个左子节点,那么检查该子节点,并通过为有问题的节点的子节点调用相同的函数来查看新节点是否应该插入到左边或右边......我不明白为什么节点的(传递的 TreeNode 对象)键得到更新,当我设置孩子的键值时。

4

2 回答 2

1

我不太明白你的意思

问题是当我创建一个子节点并设置父子链接时。父节点(我传递的节点对象)的值也被更新并引用子对象。

但我确实注意到会导致错误的一件事:

    else if((int) node.getKey() <= (int) element)

当node.getKey() == element时,表示bst已经有一个以“element”为key的节点,应该是另一种特殊情况。相反,您仍在遍历它的右孩子。

另外,如果你能更清楚地阐述你的错误,那就太好了......

于 2013-10-30T04:01:51.550 回答
1

为什么是你的keyvalue静态的对象?这意味着所有 TreeNode 将共享相同的键/值。删除 static 关键字,它应该可以正常工作。

于 2013-10-30T04:02:40.817 回答