璐村惂鐢ㄦ埛_007ZKQU馃惥 -
关注数: 0 粉丝数: 4 发帖数: 258 关注贴吧数: 0
Splay Tree For VB6 - 未完成 以下是一个 Class, 名叫 CSplayOption ExplicitPrivate Type TNode lChild As Long rChild As Long Father As Long Key As LongEnd TypeDim Root As Long, Node() As TNode, MaxNode As LongDim FreeMemList() As Long, FreeMemCount As LongPrivate Sub Class_Initialize() ReDim Node(16), FreeMemList(16) Root = 0: MaxNode = 0: FreeMemCount = 0End SubPublic Sub Add(ByVal Key As Long) Dim NodeNumber As Long, UBoundNode As Long, Pos As Long, GoLeft As Boolean If FreeMemCount = 0 Then MaxNode = MaxNode + 1 NodeNumber = MaxNode Else MaxNode = FreeMemList(FreeMemCount) FreeMemCount = FreeMemCount - 1 End If UBoundNode = UBound(Node) If NodeNumber > UBoundNode Then ReDim Preserve Node(UBoundNode * 2) With Node(NodeNumber) .Key = Key .lChild = 0 .rChild = 0 End With If Root = 0 Then Root = NodeNumber Node(NodeNumber).Father = 0 Else Pos = Root GoLeft = Key < Node(Pos).Key Do While (GoLeft And (Node(Pos).lChild <> 0)) Or ((Not GoLeft) And (Node(Pos).rChild <> 0)) If GoLeft Then Pos = Node(Pos).lChild Else Pos = Node(Pos).rChild GoLeft = Key < Node(Pos).Key Loop If GoLeft Then Node(Pos).lChild = NodeNumber Else Node(Pos).rChild = NodeNumber Node(NodeNumber).Father = Pos Splay Pos End IfEnd SubPublic Sub MidOrder(Optional ByVal Pos As Long = -1) If Pos = -1 Then Pos = Root If Pos = 0 Then Exit Sub MidOrder Node(Pos).lChild MsgBox Node(Pos).Key MidOrder Node(Pos).rChildEnd SubPrivate Sub Left_Rotate(ByVal Pos As Long) Dim t As Long t = Node(Pos).Key: Node(Pos).Key = Node(Node(Pos).rChild).Key: Node(Node(Pos).rChild).Key = t t = Node(Pos).lChild: Node(Pos).lChild = Node(Pos).rChild: Node(Pos).rChild = t t = Node(Pos).rChild: Node(Pos).rChild = Node(Node(Pos).lChild).rChild Node(Node(Pos).lChild).rChild = Node(Node(Pos).lChild).lChild: Node(Node(Pos).lChild).lChild = t Node(Node(Pos).lChild).Father = Pos: Node(Node(Pos).rChild).Father = Pos Node(Node(Node(Pos).lChild).lChild).Father = Node(Pos).lChild Node(Node(Node(Pos).lChild).rChild).Father = Node(Pos).lChildEnd SubPrivate Sub Right_Rotate(ByVal Pos As Long) Dim t As Long t = Node(Pos).Key: Node(Pos).Key = Node(Node(Pos).lChild).Key: Node(Node(Pos).lChild).Key = t t = Node(Pos).lChild: Node(Pos).lChild = Node(Pos).rChild: Node(Pos).rChild = t t = Node(Pos).lChild: Node(Pos).lChild = Node(Node(Pos).rChild).lChild Node(Node(Pos).rChild).lChild = Node(Node(Pos).rChild).rChild: Node(Node(Pos).rChild).rChild = t Node(Node(Pos).lChild).Father = Pos: Node(Node(Pos).rChild).Father = Pos Node(Node(Node(Pos).rChild).lChild).Father = Node(Pos).rChild Node(Node(Node(Pos).rChild).rChild).Father = Node(Pos).rChildEnd SubPrivate Sub Splay(ByVal Pos As Long) Dim LowerChild As Boolean, UpperChild As Boolean Do While Pos <> Root If Pos = Node(Node(Pos).Father).lChild Then LowerChild = False Else LowerChild = True If Node(Pos).Father = Root Then Pos = Root If LowerChild Then Left_Rotate Pos Else Right_Rotate Pos Else If Node(Pos).Father = Node(Node(Node(Pos).Father).Father).lChild Then UpperChild = True Else UpperChild = False If LowerChild Then If UpperChild Then Pos = Node(Pos).Father: Pos = Node(Pos).Father Left_Rotate Pos: Left_Rotate Pos Else Pos = Node(Pos).Father: Left_Rotate Pos Pos = Node(Pos).Father: Right_Rotate Pos End If Else If UpperChild Then Pos = Node(Pos).Father: Right_Rotate Pos Pos = Node(Pos).Father: Left_Rotate Pos Else Pos = Node(Pos).Father: Pos = Node(Pos).Father Right_Rotate Pos: Right_Rotate Pos End If End If End If LoopEnd Sub
1 下一页