AATreeNode.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4266 c04e8a065da5
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2009 by eXept Software AG
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
2782
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
4266
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
    14
"{ NameSpace: Smalltalk }"
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
    15
2782
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
    16
BinaryTreeNode subclass:#AATreeNode
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'level'
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Ordered-Trees'
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!AATreeNode class methodsFor:'documentation'!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
copyright
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
"
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
 COPYRIGHT (c) 2009 by eXept Software AG
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
              All Rights Reserved
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 This software is furnished under a license and may be used
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 only in accordance with the terms of that license and with the
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 be provided or otherwise made available to, or used by, any
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 other person.  No title to or ownership of the software is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 hereby transferred.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
"
3140
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    37
!
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    38
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    39
documentation
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    40
"
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    41
    I represent nodes in an AA tree, which is a form of balanced tree used for storing and retrieving ordered data efficiently.
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    42
    AA trees are named for Arne Andersson, their inventor.
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    43
    See details in AATree
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
    44
"
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
! !
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!AATreeNode class methodsFor:'instance creation'!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
data:dataArg level:levelArg
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    ^ self basicNew data:dataArg level:levelArg
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
! !
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
!AATreeNode methodsFor:'accessing'!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
4266
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
    55
data:dataArg level:levelIntegerArg
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    data := dataArg.
4266
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
    57
    level := levelIntegerArg.
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
level
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    ^ level
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
4266
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
    64
level:anInteger
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
    65
    level := anInteger.
2779
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
! !
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
!AATreeNode methodsFor:'helpers'!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
decrease_level
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
"/    function decrease_level is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
"/        input: T, a tree for which we want to remove links that skip levels.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
"/        output: T with its level decreased.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
"/        should_be = min(level(left(T)), level(right(T))) + 1
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
"/        if should_be < level(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
"/            level(T) := should_be
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
"/            if should_be < level(right(T)) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
"/                level(right(T)) := should_be
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
"/            end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
"/        end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
"/        return T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
"/    end function
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    |shouldBe leftLevel rightLevel|
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
    leftLevel := leftSubtree isNil ifTrue:[0] ifFalse:[leftSubtree level].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
    rightLevel := rightSubtree isNil ifTrue:[0] ifFalse:[rightSubtree level].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    shouldBe := (leftLevel min:rightLevel) + 1.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    shouldBe < level ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
        level := shouldBe.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
        shouldBe < rightLevel ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
            rightSubtree level:shouldBe
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
        ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
insert:anObject usingSortBlock:sortBlock
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
"/    function insert is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
"/        input: X, the value to be inserted, and T, the root of the tree to insert it into.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
"/        output: A balanced version T including X.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
"/        Do the normal binary tree insertion procedure.  Set the result of the
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
"/        recursive call to the correct child in case a new node was created or the
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
"/        root of the subtree changes.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
"/        if nil(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
"/            Create a new leaf node with X.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
"/            return node(X, 1, Nil, Nil)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
"/        else if X < value(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
"/            left(T) := insert(X, left(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
"/        else if X > value(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
"/            right(T) := insert(X, right(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
"/        end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
"/        Note that the case of X == value(T) is unspecified.  As given, an insert
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
"/        will have no effect.  The implementor may desire different behavior.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
"/        Perform skew and then split.  The conditionals that determine whether or
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
"/        not a rotation will occur or not are inside of the procedures, as given
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
"/        above.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
"/        T := skew(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
"/        T := split(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
"/        return T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
"/    end function
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
    (sortBlock value:anObject value:data) ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
        leftSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
            leftSubtree := self class data:anObject level:1
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
        ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
            leftSubtree := (leftSubtree insert:anObject usingSortBlock:sortBlock)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
        ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
    ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
        rightSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
            rightSubtree := self class data:anObject level:1
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
        ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
            rightSubtree := (rightSubtree insert:anObject usingSortBlock:sortBlock)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
        ]
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
    ^ self skew split.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
    "Modified: / 05-08-2012 / 11:40:23 / cg"
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
removeValue:oldValue using:equalSelector sortBlock:sortBlock
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
    "remove a value - returns a new treeNode, or nil if the value is not in the tree"
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
    |nextValue prevValue newTop rr thisIsMyNode newLeftSubtree newRightSubtree|
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
"/    function delete is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
"/        input: X, the value to delete, and T, the root of the tree from which it should be deleted.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
"/        output: T, balanced, without the value X.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
"/        if X > value(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
"/            right(T) := delete(X, right(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
"/        else if X < value(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
"/            left(T) := delete(X, left(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
"/        else
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
"/            If we're a leaf, easy, otherwise reduce to leaf case. 
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
"/            if leaf(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
"/                return Nil
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
"/            else if nil(left(T)) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
"/                L := successor(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
"/                right(T) := delete(L, right(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
"/                value(T) := L
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
"/            else
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
"/                L := predecessor(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
"/                left(T) := delete(L, left(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
"/                value(T) := L
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
"/            end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
"/        end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
"/        Rebalance the tree.  Decrease the level of all nodes in this level if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
"/        necessary, and then skew and split all nodes in the new level.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
"/        T := decrease_level(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
"/        T := skew(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
"/        right(T) := skew(right(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
"/        right(right(T)) := skew(right(right(T)))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
"/        T := split(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
"/        right(T) := split(right(T))
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
"/        return T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
"/    end function
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
    "/ speed hack - avoids message sends (and also better inline caching)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
    equalSelector == #== ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
        thisIsMyNode := (data == oldValue).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
    ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
        equalSelector == #= ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
            thisIsMyNode := (data = oldValue).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
        ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
            thisIsMyNode := data perform:equalSelector with:oldValue.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
        ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    thisIsMyNode ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
        leftSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
            rightSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
                ^ nil
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
            ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
            nextValue := rightSubtree leftMostNode data.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
            rightSubtree := (rightSubtree removeValue:nextValue using:equalSelector sortBlock:sortBlock).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
            data := nextValue.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
        ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
            prevValue := leftSubtree rightMostNode data.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
            leftSubtree := (leftSubtree removeValue:prevValue using:equalSelector sortBlock:sortBlock).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
            data := prevValue.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
        ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
    ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
        (sortBlock value:oldValue value:data) ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
            "/ the value should be in the left part.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
            leftSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
                ^ nil.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
            ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
            newLeftSubtree := leftSubtree removeValue:oldValue using:equalSelector sortBlock:sortBlock.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
            newLeftSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
                (leftSubtree data perform:equalSelector with:oldValue) ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
                    ^ nil
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
                ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
            ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
            leftSubtree := newLeftSubtree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
        ] ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
            "/ the value should be in the right part.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
            rightSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
                ^ nil.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
            ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
            newRightSubtree := rightSubtree removeValue:oldValue using:equalSelector sortBlock:sortBlock.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
            newRightSubtree isNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
                (rightSubtree data perform:equalSelector with:oldValue) ifFalse:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
                    ^ nil
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
                ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
            ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
            rightSubtree := newRightSubtree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
        ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
    self decrease_level.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
    newTop := self skew.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
    (rr := newTop rightSubtree) notNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
        newTop rightSubtree:(rr skew).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
        (rr := newTop rightSubtree rightSubtree) notNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
            newTop rightSubtree rightSubtree:(rr skew).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
        ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
    newTop := newTop split.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
    (rr := newTop rightSubtree) notNil ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
        newTop rightSubtree:(rr split).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
    ^ newTop
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
skew
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
    |oldLeft|
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
"/    function skew is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
"/        input: T, a node representing an AA tree that needs to be rebalanced.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
"/        output: Another node representing the rebalanced AA tree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
"/        if nil(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
"/            return Nil
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
"/        else if level(left(T)) == level(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
"/            Swap the pointers of horizontal left links.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
"/            L = left(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
"/            left(T) := right(L)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
"/            right(L) := T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
"/            return L
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
"/        else
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
"/            return T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
"/        end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
"/    end function
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    leftSubtree isNil ifTrue:[^ self].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
    leftSubtree level == level ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
        oldLeft := leftSubtree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
        leftSubtree := (oldLeft rightSubtree).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
        oldLeft rightSubtree:self.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
        ^ oldLeft
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
    ^ self
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
!
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
split
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
    |oldRight oldRightRight|
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
"/    function split is
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
"/        input: T, a node representing an AA tree that needs to be rebalanced.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
"/        output: Another node representing the rebalanced AA tree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
"/
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
"/        if nil(T) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
"/            return Nil
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
"/        else if level(T) == level(right(right(T))) then
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
"/            We have two horizontal right links.  Take the middle node, elevate it, and return it.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
"/            R = right(T)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
"/            right(T) := left(R)
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
"/            left(R) := T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
"/            level(R) := level(R) + 1
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
"/            return R
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
"/        else
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
"/            return T
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
"/        end if
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
"/    end function
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
    rightSubtree isNil ifTrue:[^ self].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
    oldRightRight := rightSubtree rightSubtree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
    oldRightRight isNil ifTrue:[^ self].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
    level == oldRightRight level ifTrue:[
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
        oldRight := rightSubtree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
        rightSubtree := oldRight leftSubtree.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
        oldRight leftSubtree:self.
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
        oldRight level:(oldRight level + 1).
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
        ^ oldRight
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
    ].
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
    ^ self
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
! !
7c538129201e public again
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
2782
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   315
!AATreeNode class methodsFor:'documentation'!
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   316
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   317
version
4266
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
   318
    ^ '$Header$'
2782
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   319
!
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   320
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   321
version_CVS
4266
c04e8a065da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3140
diff changeset
   322
    ^ '$Header$'
2782
9c137c52845b defined source container
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   323
! !
3140
7b0ae64625a3 class: AATreeNode
Claus Gittinger <cg@exept.de>
parents: 2782
diff changeset
   324