第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用三個(gè)參數(shù)將節(jié)點(diǎn)插入到 Treap 上

如何使用三個(gè)參數(shù)將節(jié)點(diǎn)插入到 Treap 上

呼如林 2024-01-25 21:30:38
我在將 Treapnode 插入 Treap 時(shí)遇到問(wèn)題。它接受 3 個(gè)參數(shù)。添加(E 鍵,P 優(yōu)先級(jí),treapnode x)。我嘗試了很多方法,但總是出現(xiàn)空指針異常。我嘗試檢查左右樹中的空情況。private TreapNode add (E key, P priority, TreapNode x)        throws ElementFoundException {    // For You To Complete    int compare = key.compareTo(x.element());    if (x == null){        return new TreapNode(key, priority);    }    //root is larger than the key    else if (compare == 0) {        throw new ElementFoundException("Element was found, and tree was not changed.");    } else if (compare < 0) {        if (x.left() == null) {         //TreapNode y = new TreapNode(key, priority);            TreapNode y = x.left;            x.left = y.right;            y.right = x;            return y;        } else {            x.left = add(key, priority, x.left());        }    }    //root is smaller than the key    else if (compare > 0) {        if (x.right() == null) {            //TreapNode y = new TreapNode(key, priority);            TreapNode z = x.right;            x.right = z.left;            z.left = x;            return z;        }    }    return x;}
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是你做錯(cuò)了x.right()。情況也是如此x.left()


if (x.right() == null) {

? ? ? ? //TreapNode y = new TreapNode(key, priority);

? ? ? ? TreapNode z = x.right; // z = null

? ? ? ? x.right = z.left;? // z.left will throw NPE

應(yīng)該


if (x.right() == null) {

? ?x.right() = new TreapNode(key, priority);

? ?return x; // return parent node?

}

另外,我認(rèn)為這也是一個(gè)錯(cuò)誤的比較,int不能null但是Integer類可以


int compare = key.compareTo(x.element());? //comoareTo return an int?

if (x == null){? // does not make sense to compare and int type to Object type

? ....

}


查看完整回答
反對(duì) 回復(fù) 2024-01-25
  • 1 回答
  • 0 關(guān)注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)