關於刪除樹中指定節點的例項分析

2022-09-12 14:54:23 字數 682 閱讀 5417

tree *delete(tree *t,int

n)

else

//如果右子樹存在

return

t; }

if(n < t->element)

t->left = delete(t->left,n);

else

t->right = delete(t->right,n);

t->height = max(height(t->left),height(t->right)) + 1

;

returnt;}

下面以例項來解釋,假設要刪除下圖的節點2,

如圖,要刪除節點2,先看節點2有沒有右子樹;有右子樹,找到其右子樹中值最小的節點,於是找到了3;把3的值賦給節點2,現在變成第2附圖;下面要遞迴的刪除其右子樹中的3節點。

5比3大,所以left遞迴找到3;3有右子樹4,所以要找到3的右子樹的值最小的節點。4是其唯一的值,肯定是其最小的節點。於是把4賦給3,變成第3幅圖;下面要刪除4的右子樹中給其賦值的節點4.第乙個就是4,好巧,看看其有沒有右子樹,沒有,光桿司令乙個;直接刪除該節點,把null賦給4;於是變成第4幅圖。

刪除鍊錶中指定節點

刪除鍊錶中指定節點 思路 利用其他結構 這裡利用棧結構 存放鍊錶中除要刪除的節點外的其他節點。依次將鍊錶元素放入棧中,當遇到要刪除的元素時跳過,最後將棧中元素重新連線成鍊錶。具體 public class removevalue public static node remove node head...

刪除單鏈表中指定的節點

include stdafx.h include include include using namespace std struct node 建立單鏈表 node createsinglelist int arr,int n return head 輸出單鏈表 void print node h...

zTree選中指定ID的節點

treedemo class ztree ul var ztreemenu fn.ztree.getztreeobj treedemo 根據treeid ztree的dom容器的id 獲取 ztree物件的方法,必須初始化ztree才可以使用此方法 var id 1 設定指定的id var node...