二叉樹刪除結點

2021-10-03 10:59:35 字數 3403 閱讀 8790

二叉樹完成刪除結點的操作.

規定:1)如果刪除的節點是葉子節點,則刪除該節點

2)如果刪除的節點是非葉子節點,則刪除該子樹

思路首先先處理:

考慮如果樹是空樹root,如果只有乙個root結點,則等價將二叉樹置空

//然後進行下面步驟

1.因為我們的二叉樹是單向的,所以我們是判斷當前結點的子結點是否需要刪除結點,而不能去判斷當前這個結點是不是需要刪除結點

2.如果當前結點的左子結點不為空,並且左子結點就是要刪除結點,就將this.left=null;

並且就返回(結束遞迴刪除)

3.如果當前結點的右子結點不為空,並且右子結點就是要刪除結點,就將this.right=null ;

並且就返回(結束遞迴刪除)

4.如果第2和第3步沒有刪除結點,那麼我們就需要向左子樹進行遞迴刪除

5.如果第4步也沒有刪除結點,則應當向右子樹進行遞迴刪除.

public class binarytreedemo else 

//中序查詢

resheronode = binarytree.infixordersearch(5);

if (resheronode != null)else

//後序查詢

resheronode = binarytree.postordersearch(5);

if (resheronode != null)else

*///刪除前

system.out.

println

("刪除前,遍歷");

binarytree.

preorder()

; binarytree.

delnode(5

);system.out.

println

("刪除後");

binarytree.

preorder()

;}}//定義binarytree 二叉樹

class binarytree

//刪除節點

public void

delnode

(int no)

else

}else

}//前序遍歷

public void

preorder()

else

}//中序遍歷

public void

infixorder()

else

}//後序遍歷

public void

postorder()

else

}//前序查詢

public heronode preordersearch

(int no)

else

}//中序查詢

public heronode infixordersearch

(int no)

else

}//後序查詢

public heronode postordersearch

(int no)

else}}

class heronode

public string getname()

public void

setname

(string name)

public int

getno()

public void

setno

(int no)

public void

setleft

(heronode left)

public void

setright

(heronode right)

public heronode getleft()

public heronode getright()

@override

public string tostring()

';}//編寫前序遍歷

public void

preorder()

//遞迴向右子樹前去遍歷

if(this.right != null)

}//編寫中序遍歷

public void

infixorder()

//輸出父節點

system.out.

println

(this)

;//遞迴向右子樹中序遍歷

if(this.right != null)

}//編寫後序遍歷

public void

postorder()

if(this.right != null)

system.out.

println

(this);}

//前序遍歷查詢

public heronode preordersearch

(int no)

heronode resheronode = null;

//判斷當前節點的左子節點是否為空,如果不為空,則遞迴前序查詢

if(this.left != null)

if(resheronode != null)

//左遞迴前序查詢,找到節點,則返回,繼續判斷

if(this.right != null)

return resheronode;

}//中序遍歷查詢

public heronode infixordersearch

(int no)

if(reshernode != null)

//如果找到,則返回,如果沒有找到,就和當前結點比較,如果是則返回當前節點

if(this.no == no)

//否則繼續進行右遞迴的中序查詢

if(this.right != null)

return reshernode;

}//後序查詢

public heronode postordersearch

(int no)

if(resheronode != null)

//如果左子樹沒有找到,則向右子樹遞迴進行後序遍歷查詢

if(this.right != null)

if(resheronode != null)

//左右子樹都沒有找到, 就比較當前結點是不是

if(this.no == no)

return resheronode;

}//刪除節點

public void

delnode

(int no)

if(this.right != null && this.right.no == no)

if(this.left != null)

if(this.right != null)

}}

二十 刪除二叉樹結點

原理 三種型別 1.刪除的是葉子結點 2.刪除的結點有乙個子節點 3.刪除的結點有兩個子節點 public class tree else else 查詢結點 public node find int value else if current null return current 刪除結點 pu...

leetcode 練習 二叉樹刪除結點

delete node in a bst 給定乙個二叉搜尋樹的根節點 root 和乙個值 key,刪除二叉搜尋樹中的 key 對應的節點,並保證二叉搜尋樹的性質不變。返回二叉搜尋樹 有可能被更新 的根節點的引用。一般來說,刪除節點可分為兩個步驟 首先找到需要刪除的節點 如果找到了,刪除它。說明 要求...

二叉樹結點, 排序

二叉樹結點,排序 1.二叉搜尋樹結點最小距離 給定乙個二叉搜尋樹的根結點 root,返回樹中任意兩節點的差的最小值 對這個序列相鄰相減,取最小值即可。實現時,可以優化掉這個序列。在遍歷時記錄上乙個訪問的節點值,和當前節點相減,記錄下最小值即可 定義樹節點 public class treenode ...