插入元素后更新树高

时间:2019-06-04 18:36:34

标签: binary-search-tree

我编写了一种将新元素插入到我的二进制搜索树中的方法。但是我还必须确保在每个元素中保存树的高度。不幸的是,我无法找到一种方法,希望在这里找到建议。

因此,插入有效,但是高度(在我的代码中称为size)还不正确。

public void insert(int x){

    if (root==null) {
        root = new Node(x);
        left = new searchtree();
        right = new searchtree();
        root.size++;
    }else {
        if (root.key > x) {
            if(right==null) {
                root.size ++;
            }
            left.insert(x);
        }else {
            if (left==null) {
                root.size ++;
            }
            right.insert(x);
        }
    }

}

希望获得建议。

0 个答案:

没有答案