書上的文字描述是這樣的:在沿著左鏈接向下的過(guò)程中,保證以下情況之一成立:1、如果當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)不是2-節(jié)點(diǎn),完成;2、如果當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)時(shí)2-節(jié)點(diǎn),而他的親兄弟節(jié)點(diǎn)不是2-節(jié)點(diǎn),將左子節(jié)點(diǎn)的兄弟節(jié)點(diǎn)中的一個(gè)鍵移動(dòng)到左子節(jié)點(diǎn)中;3、如果當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)和它的親兄弟節(jié)點(diǎn)都是2-節(jié)點(diǎn),將左子節(jié)點(diǎn),父親節(jié)點(diǎn)中的最小鍵和左子節(jié)點(diǎn)最近的兄弟節(jié)點(diǎn)合并一個(gè)4-節(jié)點(diǎn)。這樣的話刪除后仍可以保持紅黑樹的狀態(tài)。上面這段話我是看懂了,但是放到代碼里就不知道哪里有對(duì)應(yīng)上面這段話中的操作,代碼如下:代碼我貼了紅黑樹的全部API,只要看刪除最小鍵那就好了。package?unit3_3;
public?class?RedBlackBST<Key?extends?Comparable<Key>,?Value>?{
????private?Node?root;
????private?static?final?boolean?RED?=?true;
????private?static?final?boolean?BLACK?=?false;
????private?class?Node?{
????????Key?key;
????????Value?val;
????????Node?left,?right;
????????int?N;
????????boolean?color;
????????Node(Key?key,?Value?val,?int?N,?boolean?color)?{
????????????this.key?=?key;
????????????this.val?=?val;
????????????this.N?=?N;
????????????this.color?=?color;
????????}
????}
????private?boolean?isRed(Node?x)?{
????????if?(x?==?null)
????????????return?false;
????????return?x.color?=?RED;
????}
????//?節(jié)點(diǎn)左旋
????private?Node?rotateLeft(Node?h)?{
????????Node?x?=?h.right;
????????h.right?=?x.left;
????????x.left?=?h;
????????x.color?=?h.color;
????????h.color?=?RED;
????????x.N?=?h.N;
????????h.N?=?1?+?size(h.left)?+?size(h.right);
????????return?x;
????}
????//?節(jié)點(diǎn)右旋
????private?Node?rotateRight(Node?h)?{
????????Node?x?=?h.left;
????????h.left?=?x.right;
????????x.right?=?h;
????????x.color?=?h.color;
????????x.N?=?h.N;
????????h.N?=?1?+?size(h.left)?+?size(h.right);
????????return?x;
????}
????//?改變顏色
????void?flipColors(Node?h)?{
????????h.color?=?!h.color;
????????h.left.color?=?!h.left.color;
????????h.right.color?=?!h.right.color;
????}
????public?int?size()?{
????????return?size(root);
????}
????private?int?size(Node?x)?{
????????if?(x?==?null)?{
????????????return?0;
????????}?else
????????????return?x.N;
????}
????public?void?put(Key?key,?Value?val)?{
????????root?=?put(root,?key,?val);
????????root.color?=?BLACK;
????}
????public?Node?put(Node?h,?Key?key,?Value?val)?{
????????if?(h?==?null)
????????????return?new?Node(key,?val,?1,?RED);
????????int?cmp?=?key.compareTo(h.key);
????????if?(cmp?<?0)
????????????h.left?=?put(h.left,?key,?val);
????????else?if?(cmp?>?0)
????????????h.right?=?put(h.right,?key,?val);
????????else
????????????h.val?=?val;
????????if?(isRed(h.right)?&&?!isRed(h.left))
????????????h?=?rotateLeft(h);
????????if?(isRed(h.left)?&&?isRed(h.left.left))
????????????h?=?rotateRight(h);
????????if?(isRed(h.left)?&&?isRed(h.right))
????????????flipColors(h);
????????h.N?=?size(h.left)?+?size(h.right)?+?1;
????????return?h;
????}
????public?boolean?isEmpty()?{
????????return?size()?==?0;
????}
????private?Node?balance(Node?h)?{
????????if?(isRed(h.right))
????????????h?=?rotateLeft(h);
????????if?(isRed(h.right)?&&?!isRed(h.left))
????????????h?=?rotateLeft(h);
????????if?(isRed(h.left)?&&?isRed(h.left.left))
????????????h?=?rotateRight(h);
????????if?(isRed(h.left)?&&?isRed(h.right))
????????????flipColors(h);
????????h.N?=?size(h.left)?+?size(h.right)?+?1;
????????return?h;
????}
????public?Value?get(Key?key)?{
????????return?get(root,?key);
????}
????private?Value?get(Node?x,?Key?key)?{
????????if?(x?==?null)
????????????return?null;
????????int?cmp?=?key.compareTo(x.key);
????????if?(cmp?<?0)
????????????return?get(x.left,?key);
????????else?if?(cmp?>?0)
????????????return?get(x.right,?key);
????????else
????????????return?x.val;
????}
????//?求最小值
????public?Key?min()?{
????????return?min(root).key;
????}
????private?Node?min(Node?x)?{
????????if?(x.left?==?null)
????????????return?x;
????????return?min(x.left);
????}
????//?刪除最小鍵
????private?Node?removeRedLeft(Node?h)?{
????????flipColors(h);
????????if?(isRed(h.right.left))?{
????????????h.right?=?rotateRight(h.right);
????????????h?=?rotateLeft(h);
????????}
????????return?h;
????}
????public?void?deleteMin()?{
????????if?(!isRed(root.left)?&&?!isRed(root.right))
????????????root.color?=?RED;
????????root?=?deleteMin(root);
????????if?(!isEmpty())
????????????root.color?=?BLACK;
????}
????private?Node?deleteMin(Node?h)?{
????????if?(h.left?==?null)
????????????return?null;
????????if?(!isRed(h.left)?&&?!isRed(h.left.left))
????????????h?=?removeRedLeft(h.left);
????????h.left?=?deleteMin(h.left);
????????return?balance(h);
????}
????//?刪除最大鍵
????private?Node?moveRedRight(Node?h)?{
????????flipColors(h);
????????if?(!isRed(h.left.left))
????????????h?=?rotateRight(h);
????????return?h;
????}
????public?void?deleteMax()?{
????????if?(!isRed(root.left)?&&?!isRed(root.right))
????????????root.color?=?RED;
????????root?=?deleteMax(root);
????????if?(!isEmpty())
????????????root.color?=?BLACK;
????}
????private?Node?deleteMax(Node?h)?{
????????if?(isRed(h.left))
????????????h?=?rotateRight(h);
????????if?(h.right?==?null)
????????????return?null;
????????if?(!isRed(h.right)?&&?!isRed(h.right.left))
????????????h?=?moveRedRight(h);
????????h.right?=?deleteMax(h.right);
????????return?balance(h);
????}
????//?刪除任意鍵
????public?void?delete(Key?key)?{
????????if?(!isRed(root.left)?&&?!isRed(root.right))
????????????root.color?=?RED;
????????root?=?delete(root,?key);
????????if?(!isEmpty())
????????????root.color?=?BLACK;
????}
????private?Node?delete(Node?h,?Key?key)?{
????????if?(key.compareTo(h.key)?<?0)?{
????????????if?(!isRed(h.left)?&&?!isRed(h.left.left))
????????????????h?=?removeRedLeft(h);
????????????h.left?=?delete(h.left,?key);
????????}?else?{
????????????if?(isRed(h.left))
????????????????h?=?rotateRight(h);
????????????if?(key.compareTo(h.key)?==?0?&&?(h.right?==?null))
????????????????return?null;
????????????if?(!isRed(h.right)?&&?!isRed(h.right.left))
????????????????h?=?moveRedRight(h);
????????????if?(key.compareTo(h.key)?==?0)?{
????????????????h.val?=?get(h.right,?min(h.right).key);
????????????????h.key?=?min(h.right).key;
????????????????h.right?=?deleteMin(h.right);
????????????}?else
????????????????h.right?=?delete(h.right,?key);
????????}
????????return?balance(h);
????}
}
1 回答
已采納

sunbohan00
TA貢獻(xiàn)44條經(jīng)驗(yàn) 獲得超73個(gè)贊
我看了一下刪除最小鍵的地方,沒(méi)有無(wú)法理解的地方,你具體說(shuō)一下哪里不懂,或者是多少行,回復(fù)我吧
點(diǎn)擊展開(kāi)后面1條
添加回答
舉報(bào)
0/150
提交
取消