補充廣義樹節點的核心屬性實作細節,包含 Key、Degree、IsEmpty/IsLeaf 判斷邏輯,以及正確覆寫 Equals/GetHashCode 確保集合行為正確。
public override object Key => _key;
public override uint Degree => _degree;
public override bool IsEmpty() => false; // 實例化即非空
public override bool IsLeaf() => _degree == 0; // 無子樹即葉子
public override bool Equals(object obj)
{
if (!base.Equals(obj)) return false; // 基類型別檢查
var other = (GeneralTree)obj;
// 比較子樹集合 (參考型別)
if (!Object.Equals(_subTrees, other._subTrees)) return false;
// 比較值型別成員 (Key, Degree 等)
return true;
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + (_key?.GetHashCode() ?? 0);
hash = hash * 23 + _subTrees.GetHashCode();
return hash;
}
null 或專用單例表示。Key 與子樹集合雜湊,確保相等物件雜湊碼相同。正確實作屬性判斷與物件相等比較,是廣義樹能正常運作於 HashSet、Dictionary 等集合、並支援序列化/深度複製的前提。