"Exercise: BinarySearchTree"

binarySearchTree: aCollection
"Insert and search for numbers in a binary tree.
    When we need to represent sorted data, an array does not make a good data structure.
    Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes `[1, 3, 4, 5, 2]`.
    Now we must sort the entire array again!
    We can improve on this by realizing that we only need to make space for the new item `[1, nil, 3, 4, 5]`, and then adding the item in the space we added.
    But this still requires us to shift many elements down by one.
    Binary Search Trees, however, can operate on sorted data much more efficiently.
    A binary search tree consists of a series of connected nodes.
    Each node contains a piece of data (e.g. the number 3), a variable named `left`, and a variable named `right`.
    The `left` and `right` variables point at `nil`, or other nodes.
    Since these other nodes in turn have other nodes beneath them, we say that the left and right variables are pointing at subtrees.
    All data in the left subtree is less than or equal to the current node's data, and all data in the right subtree is greater than the current node's data."
"((self new binarySearchTree: #('2' '1' '3')) at: #data) >>> '2'"
"(((self new binarySearchTree: #('2' '1' '3')) at: #left) at: #data) >>> '1'"
"(((self new binarySearchTree: #('2' '1' '3')) at: #right) at: #data) >>> '3'"
"((self new binarySearchTree: #('2' '1' '3')) at: #sorted) >>> #('1' '2' '3')"
| root current inserted stack node left right post leftSorted rightSorted acc |
	root := nil.
	aCollection do: [ :element |
			root
				ifNil: [
						root := Dictionary newFrom: {
								        (#data -> element).
								        (#left -> nil).
								        (#right -> nil).
								        (#sorted -> #(  )) } ]
				ifNotNil: [
						current := root.
						inserted := false.
						[ inserted ] whileFalse: [
								element <= (current at: #data)
									ifTrue: [
											left := current at: #left.
											left
												ifNil: [
														node := Dictionary newFrom: {
																        (#data -> element).
																        (#left -> nil).
																        (#right -> nil).
																        (#sorted -> #(  )) }.
														current at: #left put: node.
														inserted := true ]
												ifNotNil: [ current := left ] ]
									ifFalse: [
											right := current at: #right.
											right
												ifNil: [
														node := Dictionary newFrom: {
																        (#data -> element).
																        (#left -> nil).
																        (#right -> nil).
																        (#sorted -> #(  )) }.
														current at: #right put: node.
														inserted := true ]
												ifNotNil: [ current := right ] ] ] ] ].

	root ifNil: [
			^ Dictionary newFrom: {
					  (#data -> nil).
					  (#left -> nil).
					  (#right -> nil).
					  (#sorted -> #(  )) } ].

	stack := OrderedCollection new.
	post := OrderedCollection new.
	stack addLast: root.
	[ stack isEmpty ] whileFalse: [
			current := stack removeLast.
			post addLast: current.
			left := current at: #left.
			right := current at: #right.
			left ifNotNil: [ stack addLast: left ].
			right ifNotNil: [ stack addLast: right ] ].

	[ post isEmpty ] whileFalse: [
			node := post removeLast.
			left := node at: #left.
			right := node at: #right.
			leftSorted := left ifNil: [ #(  ) ] ifNotNil: [ left at: #sorted ].
			rightSorted := right
				               ifNil: [ #(  ) ]
				               ifNotNil: [ right at: #sorted ].

			acc := OrderedCollection new.
			acc addAll: (leftSorted collect: [ :e | e ]).
			acc add: (node at: #data).
			acc addAll: (rightSorted collect: [ :e | e ]).
			node at: #sorted put: acc asArray ].

	^ root


"Tests"
testBinarySearchTree
| result |

  "data is retained"
  result := task binarySearchTree: #('4').
  self assert: (result at: #data) equals: '4'.
  self assert: (result at: #right) isNil.
  self assert: (result at: #left) isNil.

  "smaller number at left node"
  result := task binarySearchTree: #('4' '2').
  self assert: (result at: #data) equals: '4'.
  self assert: (result at: #right) isNil.
  self assert: ((result at: #left) at: #data) equals: '2'.
  self assert: ((result at: #left) at: #right) isNil.
  self assert: ((result at: #left) at: #left) isNil.

  "same number at left node"
  result := task binarySearchTree: #('4' '4').
  self assert: (result at: #data) equals: '4'.
  self assert: (result at: #right) isNil.
  self assert: ((result at: #left) at: #data) equals: '4'.
  self assert: ((result at: #left) at: #right) isNil.
  self assert: ((result at: #left) at: #left) isNil.

  "greater number at right node"
  result := task binarySearchTree: #('4' '5').
  self assert: (result at: #data) equals: '4'.
  self assert: ((result at: #right) at: #data) equals: '5'.
  self assert: (((result at: #right) at: #right)) isNil.
  self assert: (((result at: #right) at: #left)) isNil.
  self assert: (result at: #left) isNil.

  "can create complex tree"
  result := task binarySearchTree: #('4' '2' '6' '1' '3' '5' '7').
  self assert: (result at: #data) equals: '4'.
  self assert: (((result at: #right) at: #data)) equals: '6'.
  self assert: ((((result at: #right) at: #right) at: #data)) equals: '7'.
  self assert: ((((result at: #right) at: #right) at: #right)) isNil.
  self assert: ((((result at: #right) at: #right) at: #left)) isNil.
  self assert: ((((result at: #right) at: #left) at: #data)) equals: '5'.
  self assert: ((((result at: #right) at: #left) at: #right)) isNil.
  self assert: ((((result at: #right) at: #left) at: #left)) isNil.
  self assert: (((result at: #left) at: #data)) equals: '2'.
  self assert: ((((result at: #left) at: #right) at: #data)) equals: '3'.
  self assert: ((((result at: #left) at: #right) at: #right)) isNil.
  self assert: ((((result at: #left) at: #right) at: #left)) isNil.
  self assert: ((((result at: #left) at: #left) at: #data)) equals: '1'.
  self assert: ((((result at: #left) at: #left) at: #right)) isNil.
  self assert: ((((result at: #left) at: #left) at: #left)) isNil.

  "can sort single number"
  result := task binarySearchTree: #('2').
  self assert: (result at: #sorted) equals: #('2').

  "can sort if second number is smaller than first"
  result := task binarySearchTree: #('2' '1').
  self assert: (result at: #sorted) equals: #('1' '2').

  "can sort if second number is same as first"
  result := task binarySearchTree: #('2' '2').
  self assert: (result at: #sorted) equals: #('2' '2').

  "can sort if second number is greater than first"
  result := task binarySearchTree: #('2' '3').
  self assert: (result at: #sorted) equals: #('2' '3').

  "can sort complex tree"
  result := task binarySearchTree: #('2' '1' '3' '6' '7' '5').
  self assert: (result at: #sorted) equals: #('1' '2' '3' '5' '6' '7')