BinaryTree.st
author Stefan Vogel <sv@exept.de>
Thu, 06 Dec 2007 22:51:49 +0100
changeset 1921 8ed4ef884253
parent 1628 0c7d951c6f99
child 2038 5c9febcb5a6a
permissions -rw-r--r--
Use non-recursive algorithms to add and enumerate
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:libbasic2' }"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
     3
Collection subclass:#BinaryTree
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:'treeRoot sortBlock'
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'Collections-Ordered'
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
Object subclass:#BinaryTreeNode
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
	instanceVariableNames:'data leftSubtree rightSubtree'
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
	classVariableNames:''
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
	poolDictionaries:''
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
	privateIn:BinaryTree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
!BinaryTree class methodsFor:'documentation'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
documentation
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
    Loosely based on the Public Domain BinaryTreeNode class from Steve Chepurny.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
    Changes:
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
        Changed to be Collection-protocol compatible.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
        Slight speedup in insert-code.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
    [author:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
        Steve Chepurny (original BinaryTreeNode implementation)
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
        Claus Gittinger (cg@alan)
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
    [instance variables:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
    [class variables:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
    [see also:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
examples
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
                                                                [exBegin]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    |coll|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    coll := BinaryTree new.
1628
0c7d951c6f99 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1625
diff changeset
    46
    (1 to:10) do:[:i | coll add:i].
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    coll addAll:(20 to:30).
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    coll    
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
                                                                [exEnd]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
  timing:  
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
                                                                [exBegin]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
    |N randomNumbers coll1 coll2 t1 t2|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
    N := 1000000.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    randomNumbers := (1 to:N) collect:[:i | Random nextInteger].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    58
    ObjectMemory garbageCollect.
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    t1 := Time millisecondsToRun:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
        coll1 := BinaryTree new.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
        coll1 addAll:randomNumbers
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    64
    coll1 := nil.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    65
    ObjectMemory garbageCollect.
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    t2 := Time millisecondsToRun:[
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    67
        coll2 := SortedCollection new.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    68
        coll2 addAll:randomNumbers
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    Transcript show:'Time to insert '; show:N; show:' into BinaryTree: '; show:t1; showCR:'ms'.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    Transcript show:'Time to insert '; show:N; show:' into SortedCollection: '; show:t2; showCR:'ms'.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
                                                                [exEnd]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    74
  timing 2:  
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    75
                                                                [exBegin]
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    76
    |allSelectors coll1 coll2 t0 t1 t2|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    77
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    78
    allSelectors := OrderedCollection new.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    79
    Smalltalk allClassesDo:[:cls |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    80
        cls instAndClassSelectorsAndMethodsDo:[:sel :mthd |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    81
            allSelectors add:sel.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    82
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    83
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    84
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    85
    t1 := Time millisecondsToRun:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    86
        coll1 := SortedCollection new.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    87
        allSelectors do:[:sel |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    88
            coll1 add:sel
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    89
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    90
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    91
    Transcript show:'Time to insert '; show:coll1 size; show:' selectors into SortedCollection: '; show:t1; showCR:'ms'.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    92
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    93
    t2 := Time millisecondsToRun:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    94
        coll2 := BinaryTree new.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    95
        allSelectors do:[:sel |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    96
            coll2 add:sel
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    97
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    98
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
    99
    Transcript show:'Time to insert '; show:coll2 size; show:' selectors into BinaryTree: '; show:t2; showCR:'ms'.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   100
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   101
    t1 := Time millisecondsToRun:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   102
        allSelectors do:[:sel |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   103
            coll1 remove:sel
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   104
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   105
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   106
    self assert:(coll1 isEmpty).
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   107
    Transcript show:'Time to remove selectors from SortedCollection: '; show:t1; showCR:'ms'.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   108
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   109
    t2 := Time millisecondsToRun:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   110
        allSelectors do:[:sel |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   111
            coll2 remove:sel
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   112
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   113
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   114
    self assert:(coll2 isEmpty).
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   115
    Transcript show:'Time to remove selectors from BinaryTree: '; show:t2; showCR:'ms'.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   116
                                                                [exEnd]
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   117
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
!BinaryTree class methodsFor:'instance creation'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
new
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   124
    ^ self sortBlock:[:a :b | a < b]
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
new:n
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
    ^ self new
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   129
!
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   130
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   131
sortBlock:aTwoArgBlock
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   132
    ^ self basicNew sortBlock:aTwoArgBlock
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
1625
3ec198518bdc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1475
diff changeset
   135
!BinaryTree methodsFor:'accessing'!
3ec198518bdc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1475
diff changeset
   136
3ec198518bdc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1475
diff changeset
   137
sortBlock:something
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   138
    "set the sort block.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   139
     This is allowed only before any elements are stored in the tree"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   140
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   141
    self assert:treeRoot isNil message:'changing sortBlock in BinaryTree'.
1625
3ec198518bdc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1475
diff changeset
   142
    sortBlock := something.
3ec198518bdc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1475
diff changeset
   143
! !
3ec198518bdc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1475
diff changeset
   144
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
!BinaryTree methodsFor:'adding & removing'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
add:anObject
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
    |newNode|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
    newNode := BinaryTreeNode data:anObject.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
    treeRoot isNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
        treeRoot := newNode.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    ] ifFalse:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
        treeRoot insert:newNode sortBlock:sortBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
    ^ anObject "sigh - collection protocol"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
    "
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   159
     BinaryTree withAll:#(16 3 1 0 4 7 9)
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
     BinaryTree new add:1; add:2; yourself
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
     BinaryTree with:1 with:2 with:3
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
    "
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   165
includesIdentical:anElement
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   166
    treeRoot isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   167
        ^ false.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   168
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   169
    ^ treeRoot includesIdenticalValue:anElement sortBlock:sortBlock.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   170
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   171
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   172
     BinaryTree new 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   173
        addAll:#(4 2 1 3 6 5 7); 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   174
        includesIdentical:4
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   175
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   176
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   177
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   178
     BinaryTree new 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   179
        addAll:#(4 2 1 3 6 5 7); 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   180
        includesIdentical:8
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   181
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   182
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   183
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   184
remove:oldObject ifAbsent:exceptionValue
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   185
    |newRoot|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   186
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   187
    treeRoot isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   188
        ^ exceptionValue value.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   189
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   190
    newRoot := treeRoot removeValue:oldObject sortBlock:sortBlock.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   191
    newRoot isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   192
        treeRoot data = oldObject ifFalse:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   193
            ^ exceptionValue value.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   194
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   195
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   196
    treeRoot := newRoot    
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   197
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   198
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   199
     BinaryTree new 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   200
        addAll:#(4 2 1 3 6 5 7); 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   201
        removeIdentical:4;
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   202
        yourself   
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   203
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   204
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   205
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   206
     BinaryTree new 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   207
        addAll:#(4 2 1 3 6 5 7); 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   208
        removeIdentical:7;
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   209
        yourself      
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   210
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   211
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   212
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   213
removeIdentical:oldObject ifAbsent:exceptionValue
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   214
    |newRoot|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   215
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   216
    treeRoot isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   217
        ^ exceptionValue value.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   218
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   219
    newRoot := treeRoot removeIdenticalValue:oldObject sortBlock:sortBlock.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   220
    newRoot isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   221
        ^ exceptionValue value.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   222
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   223
    treeRoot := newRoot    
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   224
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   225
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   226
     BinaryTree new 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   227
        addAll:#(4 2 1 3 6 5 7); 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   228
        removeIdentical:4;
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   229
        yourself   
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   230
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   231
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   232
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   233
     BinaryTree new 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   234
        addAll:#(4 2 1 3 6 5 7); 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   235
        removeIdentical:7;
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   236
        yourself      
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   237
    "
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   240
!BinaryTree methodsFor:'enumerating'!
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   241
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   242
do:aBlock
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   243
    "enumerate the tree in order"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   244
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   245
    treeRoot notNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   246
        treeRoot inOrderDo:[:eachNode | aBlock value:(eachNode data)].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   247
    ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   248
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   249
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   250
     |coll|
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   251
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   252
     coll:= OrderedCollection new.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   253
     (BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) do:[:each| coll add:each].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   254
     coll
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   255
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   256
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   257
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   258
     |coll|
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   260
     coll:= OrderedCollection new.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   261
     (BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) preOrderDo:[:each| coll add:each].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   262
     coll
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   263
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   264
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   265
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   266
     |coll|
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   267
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   268
     coll:= OrderedCollection new.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   269
     (BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) postOrderDo:[:each| coll add:each].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   270
     coll
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   271
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   272
!
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   273
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   274
postOrderDo:aBlock
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   275
    "enumerate in postOrder - Left, Right, Root"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   276
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   277
    treeRoot notNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   278
        treeRoot postOrderDo:[:eachNode | aBlock value:(eachNode data)].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   279
    ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   280
!
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   281
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   282
preOrderDo:aBlock
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   283
    "enumerate in preOrder - Root, Left, Right"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   284
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   285
    treeRoot notNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   286
        treeRoot preOrderDo:[:eachNode | aBlock value:(eachNode data)].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   287
    ].
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
!BinaryTree methodsFor:'queries'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
size
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
    ^ treeRoot size
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
!BinaryTree::BinaryTreeNode class methodsFor:'documentation'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
copyright
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
    Public domain (I guess, as it was published in c.l.s)
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
    no limitation on use.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
    This class is provided as-is, without any warranty. 
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
    It is not part of or covered by the ST/X copyright.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
documentation
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
    goody from comp.lang.smalltalk;
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
    original header:
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
	Here's a complete implementation of a binary tree class:
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
    claus:
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
	I would have called this a BinaryTreeNode ....
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
    [organization:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
	The National Capital FreeNet, Ottawa, Ontario, Canada
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
    [author:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
	al938@FreeNet.Carleton.CA (Steve Chepurny)
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
    [see also:]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
	LinkedList Chain
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
	Link ValueLink ChainLink
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
examples
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
  manual building of a tree:
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
                                                                        [exBegin]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
    |tree|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
    tree := BinaryTreeNode data:2.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
    tree leftSubtree:(BinaryTreeNode new data:1).
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
    tree rightSubtree:(BinaryTreeNode new data:3).
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
    tree printOn:Transcript.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
                                                                        [exEnd]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
  insertion:
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
                                                                        [exBegin]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
    |tree|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
    tree := BinaryTreeNode data:'hello'.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
    #('the' 'quick' 'brown' 'fox' 'jumps' 'over' 'the' 'lazy' 'dogs')
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
    do:[:word |
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
        tree insert:(BinaryTreeNode data:word).
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
    tree inOrderDo:[:node |
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
        Transcript showCR:node data
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
    ]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
                                                                        [exEnd]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
!BinaryTree::BinaryTreeNode class methodsFor:'instance creation'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
data:data
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
    "Returns a new binary tree node, holding data"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
    ^ self basicNew initialize data:data
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
    "Modified: 10.5.1996 / 15:00:13 / cg"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
    "Created: 10.5.1996 / 15:00:35 / cg"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
empty
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
    "Returns a new binary tree with subtrees as binary tree nodes"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
    ^ self new
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
	    leftSubtree: self new;
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
	    rightSubtree: self new
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
    "Modified: 10.5.1996 / 15:00:02 / cg"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
new
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
    "Returns a new empty binary tree node"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
    ^ self basicNew initialize
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
    "Modified: 10.5.1996 / 15:00:13 / cg"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
!BinaryTree::BinaryTreeNode methodsFor:'accessing'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
data
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
    ^data
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
data:   anObject
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
    data := anObject
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
leftSubtree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
    ^leftSubtree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
leftSubtree: aBinaryTree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
    leftSubtree := aBinaryTree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
rightSubtree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
    ^rightSubtree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
rightSubtree: aBinaryTree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
    rightSubtree := aBinaryTree
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
!BinaryTree::BinaryTreeNode methodsFor:'enumeration'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
do: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
    "applies aBlock to each elements data in the binary tree in inorder"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
    self inOrderDo:[:eachNode | aBlock value:eachNode data]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
inOrderDo:aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
    "Traverses the elements of the binary tree in
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
        LEFT - ROOT - RIGHT order, 
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   426
     applying a block to each node.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   427
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   428
     We use an interative approach here, to avoid VM stack overflow"
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   430
    |nextNode stack|
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   431
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   432
    stack := Stack new.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   433
    nextNode := self.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   434
    [
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   435
        |left|
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   436
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   437
        stack push:nextNode.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   438
        left := nextNode leftSubtree.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   439
        left isNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   440
            [
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   441
                stack isEmpty ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   442
                    ^ self
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   443
                ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   444
                nextNode := stack pop.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   445
                aBlock value:nextNode.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   446
                nextNode := nextNode rightSubtree.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   447
            ] doWhile:[nextNode isNil]
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   448
        ] ifFalse:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   449
            nextNode := left.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   450
        ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   451
    ] loop.
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   453
    "
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   454
      BinaryTree withAll:#(2 16 3 1 0 4 7 9)
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   455
    "
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
postOrderDo: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
    "Traverses the elements of the binary tree in
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
        LEFT - RIGHT - ROOT order, 
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
     applying a block to each node"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   463
    leftSubtree notNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
        leftSubtree postOrderDo: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
    rightSubtree notNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   467
        rightSubtree postOrderDo: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
    aBlock value: self.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
preOrderDo: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
    "Traverses the elements of the binary tree in
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
        ROOT - LEFT - RIGHT order, 
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
     applying a block to each node"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
    aBlock value: self.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
    leftSubtree notNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
        leftSubtree preOrderDo: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
    rightSubtree notNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
        rightSubtree preOrderDo: aBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   488
!BinaryTree::BinaryTreeNode methodsFor:'insert & delete'!
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
insert:aBinaryTreeNode
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
    "insert a node, comparing nodes using a default sort rule"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
    ^ self
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
        insert:aBinaryTreeNode
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
        sortBlock:[:a :b | a < b]
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
    "Modified: 10.5.1996 / 15:08:30 / cg"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   498
    "Created: 10.5.1996 / 15:09:44 / cg"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
insert:newBinaryTreeNode sortBlock:sortBlock
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
    "insert a node, comparing nodes using sortBlock"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   504
    "/ the following might be ugly - however, it it slightly faster than the stuff below.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   505
    "/ AND it does not suffer stack exhaustion....
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   506
    "/ (we MUST have LCO in smalltalk for this to be automatically faster
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   507
    
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   508
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   509
    |node newValue left right|
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   510
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   511
    node := self.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   512
    newValue := newBinaryTreeNode data.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   513
    [true] whileTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   514
        "newValue is less the node data"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   515
        (sortBlock value:newValue value:node data) ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   516
            left := node leftSubtree.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   517
            left isNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   518
                node leftSubtree:newBinaryTreeNode.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   519
                ^ self
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   520
            ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   521
            node := left
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   522
        ] ifFalse:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   523
            "newValue is larger or equal than node data"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   524
            right := node rightSubtree.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   525
            "if right data is less than node, we would be jumping back..."
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   526
            right isNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   527
                node rightSubtree:newBinaryTreeNode.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   528
                ^ self
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   529
            ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   530
            node := right
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   531
        ]
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   532
    ].
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   533
    "not reached"
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   534
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   535
"/    (sortBlock value:newBinaryTreeNode data value:data) ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   536
"/        leftSubtree isNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   537
"/            leftSubtree := newBinaryTreeNode.
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   538
"/        ] ifFalse:[
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   539
"/            leftSubtree insert:newBinaryTreeNode sortBlock:sortBlock
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   540
"/        ]
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   541
"/    ] ifFalse:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   542
"/        rightSubtree isNil ifTrue:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   543
"/            rightSubtree := newBinaryTreeNode.
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   544
"/        ] ifFalse:[
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   545
"/            rightSubtree insert:newBinaryTreeNode sortBlock:sortBlock
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   546
"/        ]
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   547
"/    ]
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   548
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   549
     BinaryTree withAll:#(16 3 1 0 4 7 9)
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   550
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   551
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   552
removeIdenticalValue:oldValue sortBlock:sortBlock
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   553
    "remove a value - returns a new treeNode, or nil if the value is not in the tree"
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   554
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   555
    ^ self removeValue:oldValue using:#== sortBlock:sortBlock
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   556
!
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   557
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   558
removeValue:oldValue sortBlock:sortBlock
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   559
    "remove a value - returns a new treeNode, or nil if the value is not in the tree"
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   560
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   561
    ^ self removeValue:oldValue using:#= sortBlock:sortBlock
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   562
!
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   563
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   564
removeValue:oldValue using:compareOp sortBlock:sortBlock
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   565
    "remove a value - returns a new treeNode, or nil if the value is not in the tree"
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   566
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   567
    |thisIsMyNode newTop|
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   568
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   569
    "/ speed hack - avoids message sends
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   570
    compareOp == #== ifTrue:[
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   571
        thisIsMyNode := (data == oldValue).
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   572
    ] ifFalse:[
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   573
        compareOp == #= ifTrue:[
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   574
            thisIsMyNode := (data = oldValue).
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   575
        ] ifFalse:[
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   576
            thisIsMyNode := data perform:compareOp with:oldValue.
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   577
        ].
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   578
    ].
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   579
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   580
    thisIsMyNode ifTrue:[
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   581
        leftSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   582
            ^ rightSubtree
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   583
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   584
        rightSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   585
            ^ leftSubtree
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   586
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   587
        newTop := self removeLeftRightMostNode.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   588
        newTop leftSubtree:leftSubtree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   589
        newTop rightSubtree:rightSubtree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   590
        ^ newTop.
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   593
    (sortBlock value:oldValue value:data) ifTrue:[
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   594
        "/ the value should be in the left half.
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   595
        leftSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   596
            ^ nil
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   597
        ].
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   598
        leftSubtree := leftSubtree removeValue:oldValue using:compareOp sortBlock:sortBlock.
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   599
    ] ifFalse:[
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   600
        "/ the value should be in the right half.
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   601
        rightSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   602
            ^ nil
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   603
        ].
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   604
        rightSubtree := rightSubtree removeValue:oldValue using:compareOp sortBlock:sortBlock.
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   605
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   606
    ^ self. 
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
!BinaryTree::BinaryTreeNode methodsFor:'printing'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
printOn: aStream
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
    "Append the ascii representation to aStream"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
    data isNil
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
        ifTrue: [aStream nextPutAll: '--']
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
        ifFalse: [
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
            aStream nextPut: $(.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   618
            data printOn: aStream. aStream nextPut: $ .
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   619
            leftSubtree printOn: aStream. aStream nextPut: $ .
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
            rightSubtree printOn: aStream.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   621
            aStream nextPut: $)]
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   622
!
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   623
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   624
printOn:aStream indent:i
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   625
    "Append the graphical ascii representation to aStream"
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   626
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   627
    data isNil
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   628
        ifTrue: [aStream spaces:i. aStream nextPutAll: '--']
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   629
        ifFalse: [
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   630
            aStream spaces:i. aStream nextPut: $(.
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   631
            data printOn: aStream. 
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   632
            aStream cr.
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   633
            leftSubtree isNil 
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   634
                ifTrue:[ aStream spaces:i+2. '--' printOn: aStream]
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   635
                ifFalse:[ leftSubtree printOn: aStream indent:i+2 ]. 
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   636
            aStream cr.
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   637
            rightSubtree isNil 
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   638
                ifTrue:[ aStream spaces:i+2. '--' printOn: aStream]
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   639
                ifFalse:[ rightSubtree printOn: aStream indent:i+2 ]. 
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   640
            aStream nextPut: $)
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   641
        ]
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   642
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   643
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   644
!BinaryTree::BinaryTreeNode methodsFor:'private helpers'!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   645
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   646
removeLeftRightMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   647
    |rightMost|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   648
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   649
    leftSubtree rightSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   650
        rightMost := leftSubtree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   651
        leftSubtree := leftSubtree leftSubtree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   652
        ^ rightMost.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   653
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   654
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   655
    ^ leftSubtree removeRightMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   656
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   657
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   658
     |tree|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   659
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   660
     tree := BinaryTreeNode data:4.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   661
     #(2 6 1 3 5 7)
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   662
     do:[:word |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   663
         tree insert:(BinaryTreeNode data:word).
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   664
     ].
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   665
     tree printOn:Transcript indent:0. Transcript cr.
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   666
     '---------------------------' printOn:Transcript. Transcript cr.
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   667
     tree removeLeftRightMostNode.
1475
37c5fb7333f7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1380
diff changeset
   668
     tree printOn:Transcript indent:0. Transcript cr.
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   669
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   670
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   671
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   672
removeRightMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   673
    |removedNode|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   674
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   675
    rightSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   676
        self error:'should not happen'
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   677
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   678
    rightSubtree rightSubtree notNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   679
        ^ rightSubtree removeRightMostNode.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   680
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   681
    removedNode := rightSubtree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   682
    rightSubtree := nil.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   683
    ^ removedNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   684
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   685
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   686
     |tree|
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   687
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   688
     tree := BinaryTreeNode data:4.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   689
     #(2 6 1 3 5 7)
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   690
     do:[:word |
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   691
         tree insert:(BinaryTreeNode data:word).
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   692
     ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   693
     Transcript showCR:tree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   694
     Transcript showCR:(tree removeLeftRightMostNode). 
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   695
     Transcript showCR:tree.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   696
    "
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   697
! !
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   698
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
!BinaryTree::BinaryTreeNode methodsFor:'queries'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
depth
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
    "Returns the depth of the binary list"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
    ^ self level - 1.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   705
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   706
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   707
getTreeWithAnInteger: anInteger
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   708
    "Private - Returns the BinaryTree with data anInteger.  
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
     If anInteger not in the tree it returns nil."
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   710
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
    self inOrderDo: [:each| each data = anInteger ifTrue:[^each]].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   712
    ^nil.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   715
inOrderSuccessor
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   716
    "Returns the in-order successor the of receiver.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   717
     (that is the leftMost node on the right side)
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   718
     If receiver is empty then returns the receiver."
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   719
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   720
    rightSubtree isNil ifTrue:[^ self].
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   721
    ^ rightSubtree leftMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   722
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   723
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   724
includesIdenticalValue:aValue sortBlock:sortBlock
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   725
    "return true, if aValue is contained as some node's data"
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   726
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   727
    data == aValue ifTrue:[ ^ true ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   728
    (sortBlock value:aValue value:data) ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   729
        leftSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   730
            ^ false
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   731
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   732
        ^ leftSubtree includesIdenticalValue:aValue sortBlock:sortBlock.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   733
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   734
    rightSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   735
        ^ false
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   736
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   737
    ^ rightSubtree includesIdenticalValue:aValue sortBlock:sortBlock.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   738
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   739
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   740
includesValue:aValue sortBlock:sortBlock
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   741
    "return true, if some node's data is equal to aValue"
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   742
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   743
    data = aValue ifTrue:[ ^ true ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   744
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   745
    (sortBlock value:aValue value:data) ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   746
        leftSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   747
            ^ false
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   748
        ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   749
        ^ leftSubtree includesIdenticalValue:aValue.
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   750
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   751
    rightSubtree isNil ifTrue:[
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   752
        ^ false
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   753
    ].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   754
    ^ rightSubtree includesIdenticalValue:aValue.
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   755
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
isEmpty
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
    "returns true if the binary tree is empty and false otherwise"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   759
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
    ^ data isNil
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
isLeaf
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
    "Returns true if self is a leaf"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
    ^ ((leftSubtree isNil) and: [rightSubtree isNil])
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   769
leftMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   770
    "Returns the leftMost (smallest-valued) node"
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   771
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   772
    leftSubtree isNil ifTrue:[^ self].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   773
    ^ leftSubtree leftMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   774
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   775
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
level
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   777
    "Returns the depth of the binary tree"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   778
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   779
    |l|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   780
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   781
    l := 0.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   782
    leftSubtree notNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   783
        l := leftSubtree level
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   784
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   785
    rightSubtree notNil ifTrue:[
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   786
        l := l max:(rightSubtree level)
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   787
    ].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   788
    ^ l + 1
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   789
!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   790
1380
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   791
rightMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   792
    "Returns the rightMost (largest-valued) node"
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   793
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   794
    rightSubtree isNil ifTrue:[^ self].
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   795
    ^ rightSubtree rightMostNode
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   796
!
60a7125ce957 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1379
diff changeset
   797
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   798
size
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   799
    "Returns the size of the binary tree by traversing each element inorder"
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   800
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   801
    |count|
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   803
    count := 0.
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   804
    self inOrderDo: [:each | count := count + 1].
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   805
    ^ count
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   806
! !
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   807
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   808
!BinaryTree class methodsFor:'documentation'!
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   809
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   810
version
1921
8ed4ef884253 Use non-recursive algorithms to add and enumerate
Stefan Vogel <sv@exept.de>
parents: 1628
diff changeset
   811
    ^ '$Header: /cvs/stx/stx/libbasic2/BinaryTree.st,v 1.6 2007-12-06 21:51:49 stefan Exp $'
1379
ff2b64379b66 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   812
! !