수색…


소개

[TODO :이 주제는 모든 기본 CS 101 데이터 구조의 예제 여야하며 VBA에서 데이터 구조를 구현하는 방법에 대한 개요를 설명합니다. 이는 VBA 문서의 클래스 관련 항목에 도입 된 개념을 묶고 강화할 수있는 좋은 기회입니다.]

연결된 목록

이 연결된 목록 예제에서는 Set 추상 데이터 형식 작업을 구현합니다.

SinglyLinkedNode 클래스

Option Explicit

Private Value As Variant
Private NextNode As SinglyLinkedNode '"Next" is a keyword in VBA and therefore is not a valid variable name

LinkedList 클래스

Option Explicit

Private head As SinglyLinkedNode

'Set type operations

Public Sub Add(value As Variant)
    Dim node As SinglyLinkedNode
        
    Set node = New SinglyLinkedNode
    node.value = value
    Set node.nextNode = head
    
    Set head = node
End Sub

Public Sub Remove(value As Variant)
    Dim node As SinglyLinkedNode
    Dim prev As SinglyLinkedNode
    
    Set node = head
    
    While Not node Is Nothing
        If node.value = value Then
            'remove node
            If node Is head Then
                Set head = node.nextNode
            Else
                Set prev.nextNode = node.nextNode
            End If
            Exit Sub
        End If
        Set prev = node
        Set node = node.nextNode
    Wend

End Sub

Public Function Exists(value As Variant) As Boolean
    Dim node As SinglyLinkedNode
    
    Set node = head
    While Not node Is Nothing
        If node.value = value Then
            Exists = True
            Exit Function
        End If
        Set node = node.nextNode
    Wend
End Function

Public Function Count() As Long
    Dim node As SinglyLinkedNode
    
    Set node = head
    
    While Not node Is Nothing
        Count = Count + 1
        Set node = node.nextNode
    Wend
    
End Function

이진 트리

이것은 불균형 이진 검색 트리 의 예입니다. 이진 트리는 개념적으로 공통 루트에서 아래쪽으로 내림차순으로 노드의 계층 구조로 구성됩니다. 각 노드에는 왼쪽과 오른쪽의 두 자식이 있습니다. 예를 들어 숫자 7, 5, 9, 3, 11, 6, 12, 14 및 15가 BinaryTree에 삽입되었다고 가정하십시오. 구조는 다음과 같습니다. 이 이진 트리는 균형 이 맞지 않으므로 검색 성능을 보장하는 데 바람직한 특성이 될 수 있습니다. 자체 균형 조정 이진 검색 트리의 예를 보려면 AVL 트리 를 참조하십시오.

             7
            / \
           5   9
          / \   \
         3   6   11
                   \ 
                    12
                      \
                       14
                        \
                         15

BinaryTreeNode 클래스

Option Explicit

Public left As BinaryTreeNode
Public right As BinaryTreeNode
Public key As Variant
Public value As Variant

BinaryTree 클래스

[할 것]



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow