BalancedBinarySearchTree
Short summary
Interface for a balanced, binary, search tree
- balanced means that every node has a balanced (see
Tree_Balance) - binary means that every node has two childs (they can be null)
- search means it is a set, every object can be only contained once
Attention: search not only means you can't store an object with the same hash only once, instead it means you can only one element with this comparevalue. So if you compare elements by size and you have five diffrent objects with the same size, only one of them can be stored
Warning: Calling __DELETE directly on a Collection will bypass the destruct Context and therefore also delete the contained elements. Do not change the compare value of objects while they are in the tree, doing so will destroy your tree which can lead to not finding elements, hurt of search and balanced properties and os on. If you have to alter the value the correct way is: remove from tree, alter, insert. Same applies for foreach / iterate do not perform any operations changing the comparevalue
| Access | Abstract | Final | Extends | Implements |
|---|---|---|---|---|
| - | No | No | AbstractCollection | CNM_CollectionInterfaces.IBalancedBinarySearchTree, ISetHelper, ITreeHelper |
UML Diagram
Parameters
none
Properties
className
Type: CNM_AbstractObject.ClassName
This abstract property returns the class name of the concrete object, …
maxCycleCompares
Type: UDINT
This property defines the maximum amount of compares that should be done per Cycle. Is optional as default should be set by class.…
root
Type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
This property points to the root of a tree…
set
Type: CNM_CollectionInterfaces.ISetStrategy
This property returns a fluent interface to set operations, e.g. intersect() and subtract().…
size
Type: UDINT
This property allows to change the collection size without using insert / remove etc.…
tree
Type: CNM_CollectionInterfaces.ITreeStrategy
This property returns a fluent interface to tree operations, e.g. insert() and remove().…
treeHeight
Type: USINT
Function to calculate the height of a tree, as its a balanced binary tree, is defined as …
Methods
adaptBalanceAfterDoubleRotation
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): the element for that childs balance should be adapted
- Return type:
VOID
after a double rotation balance have to be adopted…
announceChange
- Return type:
VOID
called every time the tree changes, can be used to notify components…
checkLeftRotation
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): The element for that the rotation should be checkedremoving(BOOL): indicates if method was called in context of remove (then true) or in context of insert (then false)heightNotChanged(BOOL): signals if the height of any subtree has changed
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
check if a leftrotation is neccesary (=> is right subtree to deep?), if necessary it is performed…
checkRightRotation
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): The root of the subtree that should be checked for rotationremoving(BOOL): indicates if method was called in context of remove (then true) or in context of insert (then false)heightNotChanged(BOOL): signals if the height of any subtree changed
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
check if a rightrotation is neccesary (=> is left subtree to deep?), if necessary it is performed…
clear
- Return type:
VOID
This method clears/deletes all clearable data from this object.…
clearContents
- Return type:
VOID
This method is called in the destruct method and is intended to zero all references and interfaces that should not be destructed. …
clone
- Return type: CNM_AbstractObject.CNM_ReturnTypes.CloneResult
This method is used to create a new instance…
compareNodes
- Parameters:
object1(CNM_AbstractObject.IObject): first object to compareobject2(CNM_AbstractObject.IObject): second object to compare
- Return type: CNM_ReturnTypes.ComparationResult
compares two objects (should be nodevalues, or values to be inserted), uses the comparator set with method setNodeComparator…
compareTo
- Parameters:
object(CNM_AbstractObject.IObject): foreign object to compare
- Return type: CNM_AbstractObject.CNM_ReturnTypes.ComparationResult
This method compares a foreign object with the own one, this is needed for sort orders.…
containsEqualObject
- Parameters:
object(CNM_AbstractObject.IObject): the object to be checked if an object with equal value exists inside the collection
- Return type:
BOOL
True if the tree contains any object where either the injected comparator or, if no comparator is used, the object.compareTo …
containsObject
- Parameters:
object(CNM_AbstractObject.IObject): the object to be checked if it exists inside the collection
- Return type:
BOOL
This method checks if a given object is contained in the collection.…
createNewIterator
This method returns a NEW Iterator instance which can iterate the collection.…
dec
- Parameters:
value(__XWORD): value to decrease
- Return type:
VOID
Method to decrease __XWORD without overflow…
decrementSize
- Return type:
VOID
decrements the size of the collection…
deepClone
- Return type: CNM_ReturnTypes.CloneResult
This method is used to create a new instance…
destructNodeAndAllChilds
- Parameters:
node(CNM_CollectionInterfaces.IBinaryTreeNode): the node that should be destructed with all it's childsdestructNodeContents(BOOL): if true also the node contents will be destructed, if false only the nodes will be destructed
- Return type:
VOID
destructs / deepdestructs all nodes in the subtrees of a specified node and this node…
FB_Exit
- Parameters:
bInCopyCode(BOOL): TRUE: the exit method is called in order to leave the instance which will be copied afterwards (online change).
- Return type:
BOOL
Mark the object as deleted, as it stays in the memory.…
FB_init
- Parameters:
bInitRetains(BOOL): if TRUE, the retain variables are initialized (warm start / cold start)bInCopyCode(BOOL): if TRUE, the instance afterwards gets moved into the copy code (online change)nodeComparator(CNM_AbstractObject.IComparator): Optional comparator
- Return type:
BOOL
The constructor FB_init is needed to create an unique hash code.…
getClonedComparator
- Parameters:
deepCloned(BOOL): True if called from deepclonecontext
- Return type: CNM_ReturnTypes.CloneResult
prepares a new Comparator (if set)…
getClonedEmptyTree
- Parameters:
deepCloned(BOOL): indicates if tree should be cloned or deepcloned
- Return type: CNM_ReturnTypes.CloneResult
prepares a new empty tree object with the same internal state as THIS^…
getEqualObject
- Parameters:
object(CNM_AbstractObject.IObject): The object for that an aquvivelent is searched
- Return type:
BOOL
Searches an Object, that is equal to the provided object in terms of the compareTo Method (or an provided comparator from the parentstructure). …
getInorderSuccessor
- Parameters:
nodebuffer(POINTER TO CNM_CollectionInterfaces.IBinaryTreeNode)bufferindex(__XWORD)
- Return type:
BOOL
finds the next greater node (inorder successor) of a specified node…
getSmallestNode
- Parameters:
startNode(CNM_CollectionInterfaces.IBinaryTreeNode): the node from that the search should be startednodebuffer(POINTER TO CNM_CollectionInterfaces.IBinaryTreeNode)bufferindex(__XWORD)
- Return type: CNM_CollectionInterfaces.IBinaryTreeNode
finds the smallest node in a specified (sub)tree…
inc
- Parameters:
value(__XWORD)
- Return type:
VOID
method to increase __xword without overflow…
incrementSize
- Return type:
VOID
increments the size of the collection…
insert
- Parameters:
object(CNM_AbstractObject.IObject): The object that should be inserted
- Return type: CNM_ReturnTypes.SingleExecutionResult
This method can be used to insert a single element into the tree.…
insertCollection
- Parameters:
collection(CNM_CollectionInterfaces.ICollection): The collection that should be insertedexecute(BOOL): If insert should be processed
- Return type: CNM_ReturnTypes.SingleExecutionState
This method inserts all elements of a collection into the tree.…
instantInsertCollection
- Parameters:
collection(CNM_CollectionInterfaces.ICollection): The collection that should be inserted
- Return type: CNM_ReturnTypes.SingleExecutionResult
This method inserts all elements of a collection into the tree…
instantIntersect
- Parameters:
set(CNM_CollectionInterfaces.ISet): foreign set to intersect with
- Return type: CNM_ReturnTypes.SingleExecutionResult
overwrites the content of this set with the intersection of this set and the given set…
instantMerge
- Parameters:
set(CNM_CollectionInterfaces.ISet): foreign set to merge with
- Return type: CNM_ReturnTypes.SingleExecutionResult
overwrites the content of this set with the union of this set and the given set…
instantSubtract
- Parameters:
set(CNM_CollectionInterfaces.ISet): foreign set to construct substraction
- Return type: CNM_ReturnTypes.SingleExecutionResult
overwrites the content of this set with the set difference of this set and the given set…
intersect
- Parameters:
set(CNM_CollectionInterfaces.ISet): foreign set to intersect withexecute(BOOL): If intersect should be processed
- Return type: CNM_ReturnTypes.SingleExecutionState
overwrites the content of this set with the intersection of this set and the given set…
iterate
- Parameters:
execute(BOOL)
- Return type: CNM_ReturnTypes.SingleExecutionState
This method returns the next object in the iteration and returns an execution state.…
merge
- Parameters:
set(CNM_CollectionInterfaces.ISet): foreign set to merge withexecute(BOOL): If merge should be processed
- Return type: CNM_ReturnTypes.SingleExecutionState
overwrites the content of this set with the union of this set and the given set…
recursivInsert
- Parameters:
object(CNM_AbstractObject.IObject): the object that shoul be insertedelem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): the root of the tree we try to insert inheightNotChanged(BOOL): is changed to false when insert was successful
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
recursiv insert function, tries to insert in the Tree and if not on right position call itself again on a subtree, on way out of recursin (re)balance tree…
recursivRemove
- Parameters:
object(CNM_AbstractObject.IObject): The object that should be removedelem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): The root of the tree we try to delete the object fromheightNotChanged(BOOL): changed to false when node is removed
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
recursiv remove an element tries to remove element from tree, if not possible calls itself on subtree, on way out of recursin (re)balance tree…
recursivSearchSuccessor
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): the element a successor should be searched forheightNotChanged(BOOL): True if the successor was removed
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
gets the successor of a node and removes it from the tree so it can be inserted at another place (used when remove) and rebalances tree…
remove
- Parameters:
object(CNM_AbstractObject.IObject): The object that should be removed
- Return type: CNM_ReturnTypes.SingleExecutionResult
Method to remove an object from the tree.…
reorganizeLeftRotate
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): The element around that should be rotatetremoving(BOOL): indicates if method was called in context of remove (then true) or in context of insert (then false)heightNotChanged(BOOL): false if the height of the tree changed
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
performs a left rotation around a specified node and cares about the subtress and balance…
reorganizeRightRotate
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): the node around the rotation should be performedremoving(BOOL): indicates if method was called in context of remove (then true) or in context of insert (then false)heightNotChanged(BOOL): false if height changed, set to true when rotation rebalanced the tree
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
rotates right around a specified node, and cares about subtrees and balance…
rotateLeft
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): The element that sould be rotatet around
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
rotates a tree around a node…
rotateRight
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): the element that should be rotatet around
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
rotates right around a specified node, doesnt care about balance and stuff, this is done in method reorganizeRightRotate…
searchSuccessor
- Parameters:
elem(CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode): The element that the successor is searched fromheightNotChanged(BOOL): True if operation did not change the height of the subtree / heightchange was handled
- Return type: CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode
searches a successor and removes it from the tree so it can be used to replace another (deleted) node…
setNodeComparator
- Parameters:
nodeComparator(CNM_AbstractObject.IComparator): the comparator that should be used to compare two nodes
- Return type: CNM_ReturnTypes.SingleExecutionResult
Sets a comparator, that is used to compare two nodes of this tree.…
subtract
- Parameters:
set(CNM_CollectionInterfaces.ISet): foreign set to construct substractionexecute(BOOL): If substract should be processed
- Return type: CNM_ReturnTypes.SingleExecutionState
overwrites the content of this set with the set difference of this set and the given set…
Code
Declaration
{attribute 'enable_dynamic_creation'}
FUNCTION_BLOCK BalancedBinarySearchTree EXTENDS AbstractCollection IMPLEMENTS CNM_CollectionInterfaces.IBalancedBinarySearchTree, ISetHelper, ITreeHelper
VAR
rootNode :CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode;
nodeComparator :CNM_AbstractObject.IComparator;
(*nodestack used for iterate, placed in instance to be able to destroy it on destruct*)
nodestack :POINTER TO CNM_CollectionInterfaces.IBalancedBinarySearchTreeNode;
(* set iterator for subtract *)
subtractIterator :CNM_CollectionInterfaces.IIterator;
(* collection iterator for insertCollection *)
insertIterator :CNM_CollectionInterfaces.IIterator;
(* set iterator for intersect *)
intersectIterator :CNM_CollectionInterfaces.IIterator;
(* the amount of compare operations per cycle *)
upperCompareBound :UDINT := GeneralParameters.DEFAULT_OPERATIONS_PER_CYCLE;
(* insert buffer to roll back inserted Objects in error case *)
insertBuffer :POINTER TO CNM_AbstractObject.IObject;
(* intersect tree *)
intersectedTree :POINTER TO BalancedBinarySearchTree;
END_VAR