BinaryTree.st
author Claus Gittinger <cg@exept.de>
Sun, 05 Aug 2012 18:45:04 +0200
changeset 2777 06c0bd66713f
parent 2776 f15140d07975
child 2785 42c2ba93b504
permissions -rw-r--r--
x
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
     1
"
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     2
 COPYRIGHT (c) 2003 by eXept Software AG
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     3
              All Rights Reserved
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
     4
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     5
 This software is furnished under a license and may be used
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     6
 only in accordance with the terms of that license and with the
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     8
 be provided or otherwise made available to, or used by, any
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
     9
 other person.  No title to or ownership of the software is
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    10
 hereby transferred.
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
    11
"
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    13
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    14
Collection subclass:#BinaryTree
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    15
	instanceVariableNames:'treeRoot sortBlock'
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    16
	classVariableNames:'DefaultSortBlock'
2776
f15140d07975 public again
Claus Gittinger <cg@exept.de>
parents: 2770
diff changeset
    17
	poolDictionaries:''
f15140d07975 public again
Claus Gittinger <cg@exept.de>
parents: 2770
diff changeset
    18
	category:'Collections-Ordered-Trees'
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
    19
!
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
    20
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    21
!BinaryTree class methodsFor:'documentation'!
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
    22
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    23
copyright
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    24
"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    25
 COPYRIGHT (c) 2003 by eXept Software AG
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    26
              All Rights Reserved
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
    27
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    28
 This software is furnished under a license and may be used
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    29
 only in accordance with the terms of that license and with the
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    31
 be provided or otherwise made available to, or used by, any
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    32
 other person.  No title to or ownership of the software is
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    33
 hereby transferred.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    34
"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    35
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    36
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    37
documentation
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    38
"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    39
    Loosely based on the Public Domain BinaryTreeNode class from Steve Chepurny.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    40
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    41
    WARNING:
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    42
        This tree does not reorganize itself. 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    43
        Thus, its performance might degenerate to that of a linked list (see performance).
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    44
        The performance is OK, if elements are added in random order and the tree is therefore balanced.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    45
        The worst case is to add elements in order, reverseOrder or zig-zag order.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    46
        Use instances of my subclasses, which balance the tree if in doubt.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    47
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    48
    Changes:
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    49
        Changed to be Collection-protocol compatible.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    50
        Slight speedup in insert-code.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    51
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    52
    [author:]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    53
        Steve Chepurny (original BinaryTreeNode implementation)
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    54
        Claus Gittinger (cg@alan)
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    55
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    56
    [instance variables:]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    57
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    58
    [class variables:]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    59
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    60
    [see also:]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    61
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    62
"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    63
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    64
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    65
examples
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    66
"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    67
  a degenerated tree:
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    68
                                                                [exBegin]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    69
    |coll|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    70
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    71
    coll := BinaryTree new.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    72
    (1 to:10) do:[:i | coll add:i].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    73
    coll addAll:(20 to:30).
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    74
    coll inspect   
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    75
                                                                [exEnd]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    76
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    77
                                                                [exBegin]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    78
    |tree|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    79
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    80
    tree := self new.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    81
    tree add:'hello'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    82
    tree add:'aaaa'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    83
    tree add:'AAAb'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    84
    tree add:'aaaC'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    85
    tree add:'world'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    86
    tree asOrderedCollection     
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    87
                                                                [exEnd]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    88
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    89
                                                                [exBegin]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    90
    |tree|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    91
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    92
    tree := self sortBlock:[:a :b | a asLowercase < b asLowercase].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    93
    tree add:'hello'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    94
    tree add:'aaaa'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    95
    tree add:'AAAb'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    96
    tree add:'aaaC'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    97
    tree add:'world'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    98
    tree asOrderedCollection     
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
    99
                                                                [exEnd]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   100
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   101
  timing examples and benchmarks: see examples in AATree:  
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   102
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   103
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   104
  A functional example of a UCB-CS61A lecture's example.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   105
  The task is to extract all values within a given range (min..max) from
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   106
  a binary tree.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   107
  The range 'function' below does this; given a binary tree, a min and max value,
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   108
      range(bst, min, max)
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   109
  returns an array of all values which are within that range.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   110
  Only the relevant branches of the binary tree are to be visited, of course.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   111
                                                                [exBegin]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   112
    |t rangeNode range|
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   113
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   114
    t := BinaryTree new.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   115
    t add:54; add:37; add:19; add:45; add:80; add:65; add:91; add:57.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   116
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   117
    rangeNode := [:node :min :max |
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   118
                |nodeValue leftTree rightTree left right middle|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   119
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   120
                leftTree := node leftSubtree.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   121
                rightTree := node rightSubtree.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   122
                nodeValue := node data.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   123
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   124
                left := (leftTree notNil and:[nodeValue > min]) 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   125
                            ifTrue:[ rangeNode value:leftTree value:min value:max ]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   126
                            ifFalse:[ #() ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   127
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   128
                right := (rightTree notNil and:[nodeValue < max]) 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   129
                            ifTrue:[ rangeNode value:rightTree value:min value:max ]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   130
                            ifFalse:[ #() ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   131
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   132
                middle := (nodeValue between:min and:max)
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   133
                            ifTrue:[ (Array with:nodeValue) ]    
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   134
                            ifFalse:[ #() ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   135
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   136
                left, middle, right
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   137
        ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   138
    range := [:tree :min :max |
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   139
                rangeNode value:tree rootNode value:min value:max
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   140
        ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   141
    range value:t value:30 value:60.                
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   142
                                                                [exEnd]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   143
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   144
"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   145
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   146
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   147
!BinaryTree class methodsFor:'initialization'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   148
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   149
initialize
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   150
    "setup the default sortBlock.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   151
     Use #<, since this is the base method in Magnitude."
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   152
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   153
    "/ only do this once at early startup
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   154
    DefaultSortBlock isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   155
        DefaultSortBlock := [:a :b | a < b]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   156
    ]
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   157
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   158
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   159
     BinaryTree initialize
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   160
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   161
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   162
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   163
!BinaryTree class methodsFor:'instance creation'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   164
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   165
new
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   166
    "return a new instance using the default sortOrder (which is a < b)"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   167
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   168
    ^ self basicNew sortBlock:DefaultSortBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   169
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   170
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   171
new:n
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   172
    "return a new instance using the default sortOrder (which is a < b)"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   173
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   174
    ^ self new
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   175
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   176
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   177
sortBlock:aTwoArgBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   178
    "return a new instance using the given sortBlock (which returns true if a < b)"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   179
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   180
    ^ self basicNew sortBlock:aTwoArgBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   181
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   182
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   183
     |tree|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   184
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   185
     tree := self sortBlock:[:a :b | a asLowercase < b asLowercase].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   186
     tree add:'hello'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   187
     tree add:'aaaa'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   188
     tree add:'AAAb'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   189
     tree add:'aaaC'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   190
     tree add:'world'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   191
     tree asOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   192
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   193
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   194
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   195
!BinaryTree methodsFor:'accessing'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   196
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   197
rootNode
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   198
    "return the rootNode of the tree"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   199
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   200
    ^ treeRoot
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   201
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   202
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   203
sortBlock:something
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   204
    "set the sort block.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   205
     This is allowed only before any elements are stored in the tree"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   206
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   207
    self assert:treeRoot isNil message:'changing sortBlock in BinaryTree'.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   208
    sortBlock := something.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   209
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   210
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   211
!BinaryTree methodsFor:'adding & removing'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   212
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   213
add:anObject
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   214
    "add anObject to the collection. The object is inserted as defined by the sortBlock"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   215
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   216
    |newNode|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   217
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   218
    newNode := self treeNodeClass data:anObject.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   219
    treeRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   220
        treeRoot := newNode.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   221
    ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   222
        treeRoot insert:newNode sortBlock:sortBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   223
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   224
    ^ anObject "sigh - collection protocol"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   225
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   226
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   227
     BinaryTree withAll:#(16 3 1 0 4 7 9)
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   228
     BinaryTree new add:1; add:2; yourself
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   229
     BinaryTree with:1 with:2 with:3
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   230
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   231
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   232
    "Modified: / 05-08-2012 / 11:36:42 / cg"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   233
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   234
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   235
includes:anElement
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   236
    "return true, if the argument, anObject is contained in the collection.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   237
     Uses #= when comparing; i.e. the search is for an equal object."
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   238
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   239
    treeRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   240
        ^ false.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   241
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   242
    ^ treeRoot includesValue:anElement sortBlock:sortBlock.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   243
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   244
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   245
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   246
        addAll:#( 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   247
        includes:4           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   248
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   249
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   250
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   251
        addAll:#( 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   252
        includes:4.0           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   253
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   254
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   255
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   256
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   257
        addAll:#(2 1 4 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   258
        includesIdentical:4           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   259
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   260
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   261
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   262
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   263
        addAll:#( 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   264
        includesIdentical:4           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   265
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   266
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   267
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   268
includesIdentical:anElement
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   269
    "return true, if the argument, anObject is contained in the collection.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   270
     Uses #== (instead of #=) when comparing; 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   271
     i.e. the search is for the object itself, not some object being just equal."
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   272
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   273
    treeRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   274
        ^ false.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   275
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   276
    ^ treeRoot includesIdenticalValue:anElement sortBlock:sortBlock.
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   277
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   278
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   279
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   280
        addAll:#(4 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   281
        includesIdentical:4           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   282
    "
2770
24caf488ddc1 tree node class is private
Claus Gittinger <cg@exept.de>
parents: 2763
diff changeset
   283
2777
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   284
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   285
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   286
        addAll:#( 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   287
        includesIdentical:4           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   288
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   289
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   290
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   291
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   292
        addAll:#(4 2 1 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   293
        includesIdentical:8
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   294
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   295
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   296
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   297
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   298
        addAll:#( 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   299
        includes:4           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   300
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   301
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   302
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   303
        addAll:#( 2 1 4.0 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   304
        includes:4.0           
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   305
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   306
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   307
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   308
remove:oldObject ifAbsent:exceptionValue
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   309
    |newRoot|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   310
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   311
    treeRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   312
        ^ exceptionValue value.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   313
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   314
    newRoot := treeRoot removeValue:oldObject using:#= sortBlock:sortBlock.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   315
    newRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   316
        treeRoot data = oldObject ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   317
            ^ exceptionValue value.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   318
        ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   319
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   320
    treeRoot := newRoot.    
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   321
    ^ oldObject "sigh - collection protocol"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   322
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   323
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   324
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   325
        addAll:#(4 2 1 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   326
        removeIdentical:4;
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   327
        yourself   
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   328
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   329
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   330
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   331
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   332
        addAll:#(4 2 1 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   333
        removeIdentical:7;
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   334
        yourself      
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   335
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   336
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   337
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   338
removeIdentical:oldObject ifAbsent:exceptionValue
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   339
    |newRoot|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   340
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   341
    treeRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   342
        ^ exceptionValue value.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   343
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   344
    newRoot := treeRoot removeValue:oldObject using:#== sortBlock:sortBlock.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   345
    newRoot isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   346
        treeRoot data == oldObject ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   347
            ^ exceptionValue value.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   348
        ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   349
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   350
    treeRoot := newRoot.    
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   351
    ^ oldObject "sigh - collection protocol"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   352
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   353
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   354
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   355
        addAll:#(4 2 1 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   356
        removeIdentical:4;
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   357
        yourself   
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   358
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   359
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   360
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   361
     BinaryTree new 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   362
        addAll:#(4 2 1 3 6 5 7); 
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   363
        removeIdentical:7;
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   364
        yourself      
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   365
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   366
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   367
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   368
!BinaryTree methodsFor:'enumerating'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   369
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   370
do:aBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   371
    "enumerate the tree in order"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   372
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   373
    treeRoot notNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   374
        treeRoot inOrderDo:[:eachNode | aBlock value:(eachNode data)].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   375
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   376
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   377
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   378
     |coll|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   379
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   380
     coll:= OrderedCollection new.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   381
     (BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) do:[:each| coll add:each].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   382
     coll
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   383
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   384
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   385
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   386
     |coll|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   387
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   388
     coll:= OrderedCollection new.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   389
     (BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) preOrderDo:[:each| coll add:each].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   390
     coll
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   391
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   392
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   393
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   394
     |coll|
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   395
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   396
     coll:= OrderedCollection new.
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   397
     (BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) postOrderDo:[:each| coll add:each].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   398
     coll
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   399
    "
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   400
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   401
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   402
postOrderDo:aBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   403
    "enumerate in postOrder - Left, Right, Root"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   404
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   405
    treeRoot notNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   406
        treeRoot postOrderDo:[:eachNode | aBlock value:(eachNode data)].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   407
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   408
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   409
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   410
preOrderDo:aBlock
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   411
    "enumerate in preOrder - Root, Left, Right"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   412
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   413
    treeRoot notNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   414
        treeRoot preOrderDo:[:eachNode | aBlock value:(eachNode data)].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   415
    ].
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   416
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   417
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   418
!BinaryTree methodsFor:'queries'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   419
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   420
size
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   421
    "return the number of tree elements"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   422
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   423
    ^ treeRoot size
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   424
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   425
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   426
treeNodeClass
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   427
    ^ BinaryTreeNode
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   428
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   429
    "Created: / 05-08-2012 / 11:36:26 / cg"
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   430
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   431
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   432
!BinaryTree class methodsFor:'documentation'!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   433
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   434
version
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   435
    ^ '$Header: /cvs/stx/stx/libbasic2/BinaryTree.st,v 1.14 2012-08-05 16:45:04 cg Exp $'
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   436
!
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   437
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   438
version_CVS
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   439
    ^ '$Header: /cvs/stx/stx/libbasic2/BinaryTree.st,v 1.14 2012-08-05 16:45:04 cg Exp $'
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   440
! !
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   441
Claus Gittinger <cg@exept.de>
parents: 2776
diff changeset
   442
BinaryTree initialize!