二叉搜尋樹(偽碼)

2021-10-23 04:24:46 字數 1030 閱讀 4706

//                      二叉搜尋樹

//二叉搜尋樹(bst,binary search tree),

//也稱二叉排序樹或二叉查詢樹

//查詢的遞迴實現

posotion find(elementtype x,bintree bst)

//查詢的非遞迴(迭代)實現

posotion iterfind(elementtype x,bintree bst)

return null ; //查詢失敗

}//查詢的效率決定於樹的高度

//查詢最小元素的遞迴函式

position findmin( bintree bst )

//查詢最大元素的迭代函式

position findmax( bintree bst )

} return bst;

}

//二叉搜尋樹的插入

bintree insert( bintree bst, elementtype x )

else // 開始找要插入元素的位置

return bst;}

//二叉搜尋樹的刪除

bintree delete( bintree bst, elementtype x )

else /* 被刪除結點有乙個或無子結點 */}}

return bst;

}

刪除的三種情況一、要刪除的是葉節點

二、要刪除的節點只有乙個孩子 

三、要刪除的節點有左右兩棵子樹

二叉搜尋樹 二叉搜尋樹

題目 二叉搜尋樹 time limit 2000 1000 ms j a others memory limit 32768 32768 k j a others total submission s 6945 accepted submission s 3077 problem descripti...

二叉搜尋樹 修剪二叉搜尋樹

第一反應是重構,看來別人的解答發現,其實不用重構那麼複雜。treenode trimbst treenode root,int low,int high if root val high 下一層處理完左子樹的結果賦給root left,處理完右子樹的結果賦給root right。root left ...

樹 二叉樹 二叉搜尋樹

給定乙個二叉樹,判斷其是否是乙個有效的二叉搜尋樹。假設乙個二叉搜尋樹具有如下特徵 節點的左子樹只包含小於當前節點的數。節點的右子樹只包含大於當前節點的數。所有左子樹和右子樹自身必須也是二叉搜尋樹。示例 1 輸入 2 13輸出 true 示例 2 輸入 5 14 3 6輸出 false 解釋 輸入為 ...