版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第Java數(shù)據(jù)結構之紅黑樹的原理及實現(xiàn)*在從紅黑樹中刪除插入節(jié)點之后(紅黑樹失去平衡),再調(diào)用該函數(shù);
*目的是將它重新塑造成一顆紅黑樹。
*參數(shù)說明:
*node待修正的節(jié)點
privatevoidremoveFixUp(RBTNodeTnode,RBTNodeTparent){
RBTNodeTother;
while((node==null||isBlack(node))(node!=this.mRoot)){
if(parent.left==node){
other=parent.right;
if(isRed(other)){
//Case1:x的兄弟w是紅色的
setBlack(other);
setRed(parent);
leftRotate(parent);
other=parent.right;
if((other.left==null||isBlack(other.left))
(other.right==null||isBlack(other.right))){
//Case2:x的兄弟w是黑色,且w的倆個孩子也都是黑色的
setRed(other);
node=parent;
parent=parentOf(node);
}else{
if(other.right==null||isBlack(other.right)){
//Case3:x的兄弟w是黑色的,并且w的左孩子是紅色,右孩子為黑色。
setBlack(other.left);
setRed(other);
rightRotate(other);
other=parent.right;
//Case4:x的兄弟w是黑色的;并且w的右孩子是紅色的,左孩子任意顏色。
setColor(other,colorOf(parent));
setBlack(parent);
setBlack(other.right);
leftRotate(parent);
node=this.mRoot;
break;
}else{
other=parent.left;
if(isRed(other)){
//Case1:x的兄弟w是紅色的
setBlack(other);
setRed(parent);
rightRotate(parent);
other=parent.left;
if((other.left==null||isBlack(other.left))
(other.right==null||isBlack(other.right))){
//Case2:x的兄弟w是黑色,且w的倆個孩子也都是黑色的
setRed(other);
node=parent;
parent=parentOf(node);
}else{
if(other.left==null||isBlack(other.left)){
//Case3:x的兄弟w是黑色的,并且w的左孩子是紅色,右孩子為黑色。
setBlack(other.right);
setRed(other);
leftRotate(other);
other=parent.left;
//Case4:x的兄弟w是黑色的;并且w的右孩子是紅色的,左孩子任意顏色。
setColor(other,colorOf(parent));
setBlack(parent);
setBlack(other.left);
rightRotate(parent);
node=this.mRoot;
break;
if(node!=null){
setBlack(node);
*刪除結點(node),并返回被刪除的結點
*參數(shù)說明:
*node刪除的結點
privatevoid
remove(RBTNodeTnode){
RBTNodeTchild,parent;
booleancolor;
//被刪除節(jié)點的"左右孩子都不為空"的情況。
if((node.left!=null)(node.right!=null)){
//被刪節(jié)點的后繼節(jié)點。(稱為"取代節(jié)點")
//用它來取代"被刪節(jié)點"的位置,然后再將"被刪節(jié)點"去掉。
RBTNodeTreplace=node;
//獲取后繼節(jié)點
replace=replace.right;
while(replace.left!=null){
replace=replace.left;
//"node節(jié)點"不是根節(jié)點(只有根節(jié)點不存在父節(jié)點)
if(parentOf(node)!=null){
if(parentOf(node).left==node){
parentOf(node).left=replace;
}else{
parentOf(node).right=replace;
}else{
//"node節(jié)點"是根節(jié)點,更新根節(jié)點。
this.mRoot=replace;
//child是"取代節(jié)點"的右孩子,也是需要"調(diào)整的節(jié)點"。
//"取代節(jié)點"肯定不存在左孩子!因為它是一個后繼節(jié)點。
child=replace.right;
parent=parentOf(replace);
//保存"取代節(jié)點"的顏色
color=colorOf(replace);
//"被刪除節(jié)點"是"它的后繼節(jié)點的父節(jié)點"
if(parent==node){
parent=replace;
}else{
//child不為空
if(child!=null){
setParent(child,parent);
parent.left=child;
replace.right=node.right;
setParent(node.right,replace);
replace.parent=node.parent;
replace.color=node.color;
replace.left=node.left;
node.left.parent=replace;
if(color==BLACK){
removeFixUp(child,parent);
node=null;
return;
if(node.left!=null){
child=node.left;
}else{
child=node.right;
parent=node.parent;
//保存"取代節(jié)點"的顏色
color=node.color;
if(child!=null){
child.parent=parent;
//"node節(jié)點"不是根節(jié)點
if(parent!=null){
if(parent.left==node){
parent.left=child;
}else{
parent.right=child;
}else{
this.mRoot=child;
if(color==BLACK){
removeFixUp(child,parent);
node=null;
*刪除結點(z),并返回被刪除的結點
*參數(shù)說明:
*tree紅黑樹的根結點
*z刪除的結點
publicvoidremove(Tkey){
RBTNodeTnode;
if((node=search(mRoot,key))!=null){
remove(node);
*銷毀紅黑樹
privatevoiddestroy(RBTNodeTtree){
if(tree==null){
return;
if(tree.left!=null){
destroy(tree.left);
if(tree.right!=null){
destroy(tree.right);
tree=null;
publicvoidclear(){
destroy(mRoot);
mRoot=null;
*打印"紅黑樹"
*key--節(jié)點的鍵值
*direction--0,表示該節(jié)點是根節(jié)點;
*-1,表示該節(jié)點是它的父結點的左孩子;
*1,表示該節(jié)點是它的父結點的右孩子。
privatevoidprint(RBTNodeTtree,Tkey,intdirection){
if(tree!=null){
if(direction==0)//tree是根節(jié)點
System.out.printf("%2d(B)isroot\n",tree.key);
}else//tree是分支節(jié)點
System.out.printf("%2d(%s)is%2d's%6schild\n",tree.key,isRed(tree)"R":"B",key,directio
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《GA 557.1-2005互聯(lián)網(wǎng)上網(wǎng)服務營業(yè)場所信息安全管理代碼 第1部分:營業(yè)場所代碼》專題研究報告
- 中學學生社團活動交流合作制度
- 養(yǎng)老院消防演練制度
- 企業(yè)財務分析與預算管理制度
- 2026湖北省定向清華大學選調(diào)生招錄備考題庫附答案
- 2026福建泉州市南安市衛(wèi)生事業(yè)單位赴福建醫(yī)科大學招聘編制內(nèi)衛(wèi)生類人員64人備考題庫附答案
- 2026福建省面向華東理工大學選調(diào)生選拔工作備考題庫附答案
- 2026福建福州第十九中學招聘編外行政人員(勞務派遣)1人備考題庫附答案
- 2026重慶九洲智造科技有限公司招聘研發(fā)工程師10人備考題庫附答案
- 2026遼寧大連理工大學化工學院劉家旭團隊科研助理招聘1人(自聘)參考題庫附答案
- 初中語文新課程標準與解讀課件
- 無人機裝調(diào)檢修工培訓計劃及大綱
- 中建通風與空調(diào)施工方案
- 高考語言運用題型之長短句變換 學案(含答案)
- 春よ、來い(春天來了)高木綾子演奏長笛曲譜鋼琴伴奏
- ARJ21機型理論知識考試題庫(匯總版)
- 2023年婁底市建設系統(tǒng)事業(yè)單位招聘考試筆試模擬試題及答案解析
- GB/T 4623-2014環(huán)形混凝土電桿
- GB/T 32065.4-2015海洋儀器環(huán)境試驗方法第4部分:高溫試驗
- GB/T 16823.3-2010緊固件扭矩-夾緊力試驗
- 中介服務費承諾書
評論
0/150
提交評論