代码之家  ›  专栏  ›  技术社区  ›  MMelvin0581

为什么在我的LinkedBinaryTree实现中只添加根目录

  •  2
  • MMelvin0581  · 技术社区  · 7 年前

    这是我的密码。LinkedBinaryTree和Position类来自教科书,如果需要,我可以为它们提供代码,但我不确定这是否必要。这个类应该能够向二叉树中添加一个新节点,这样对于每个内部节点,存储在父节点左侧的元素小于父节点的元素,存储在父节点右侧的元素大于父节点,因此它应该创建一个二叉搜索树。问题是,当我打印出结果树(见下文)时,我只得到根元素。

    import net.datastructures.LinkedBinaryTree;
    import net.datastructures.Position;
    
    import java.util.Iterator;
    
    public class IntLinkedBinaryTree extends LinkedBinaryTree<Integer> {
    
    
    // define instance variables and methods, including a constructor(s) as needed
    private Position<Integer> root;
    private int size;
    
    /**
     * Creates an empty IntLinkedBinaryTree
     */
    public IntLinkedBinaryTree() {
        root = null;
        size = 0;
    }
    
    /**
     * Add a new node with e to the tree rooted at position p
     *
     * @param p The root of the tree to which new node is added
     * @param e The element of the new node
     * @return If a node with e does not exist, a new node with e is added and
     * reference to the node is returned. If a node with e exists, null is returned.
     */
    public Position<Integer> add(Position<Integer> p, Integer e) {
    
        if (p == null) {
            root = addRoot(e);
            size++;
            return p;
        }
        Position<Integer> x = p;
        Position<Integer> y = x;
        while (x != null) {
            if (x.getElement().equals(e)) {
                return null;
            } else if (x.getElement() > e) {
                y = x;
                x = left(x);
            } else {
                y = x;
                x = right(x);
            }
        }   // end while
        Position<Integer> temp;
        if (y.getElement() > e) {
            temp = createNode(e, validate(y), null, null);
            addLeft(temp, e);
        } else {
            temp = createNode(e, validate(y), null, null);
            addRight(temp, e);
        }
        size++;
        return temp;
    }
    
    
    public static void main(String[] args) {
    
        // create a new binary tree instance
        IntLinkedBinaryTree t = new IntLinkedBinaryTree();
    
        // add some integers
        t.add(t.root, 100);
    
        t.add(t.root, 50);
    
        t.add(t.root, 150);
    
        t.add(t.root, 70);
    
        // print all integers in the tree in increasing order
        // after adding above four integers, the following should be printed
        // 50 70 100 150
    
        Iterator<Position<Integer>> it = t.inorder().iterator();
        System.out.println();
        while (it.hasNext()) {
            System.out.print(it.next().getElement() + " ");
        }
        System.out.println();
      }
    }
    

    我的产出只有100英镑,我要把头发拔出来了。讲师在LinkedBinaryTree类中提供了addRoot()、addLeft()和addRight()、inorder()和iterator()方法,整个main方法也是。当我使用intelliJ调试代码时,它调用了add*方法,但只向控制台打印了100次。

    1 回复  |  直到 7 年前
        1
  •  1
  •   MMelvin0581    7 年前

    我将addLeft()和addRight()更改为setLeft()和setRight()。它现在可以工作了,但我不知道为什么,因为addLeft/Right()在超类中调用setLeft/Right()。。。