Class BinaryTreeSerde<T>
java.lang.Object
io.openml.gearbox.algorithms.binarytree.BinaryTreeSerde<T>
- Type Parameters:
T- The type of value store in the tree node
Serializes and deserializes a binary search tree.
Each BinaryTreeSerde instance has a pair of unique reserved characters - left delimiter invalid input: '&' right delimiter.
They are used to enclose a node value. For example, if left delimiter is '(' and right delimiter is ')',
BinaryTreeSerde serializes and deserialzes back-and-forth in the following manner:
1
/ \
2 3 invalid input: '<'---> "1(2(4))(3)"
/
4
----------------------------------
1
/ \
2 3 invalid input: '<'---> "1(2()(4))(3)"
\
4
Note how node values (1, 2, 3, 4) are delimited by "()". Serializations of node values must not contain the reserved
delimiter characters (in the example, they are '(' and ')'), otherwise the behavior of BinaryTreeSerde is
undefined.
To make serialization as compact as possible, BinaryTreeSerde avoids putting unnecessary empty right node
in the serialization. For example, both "1(2(4))(3)" and "1(2(4)())(3)" represent the same tree, but the former one
is actually used.
-
Method Summary
Modifier and TypeMethodDescriptiondeserialize(String string, Function<String, T> deserializer) Deserializes a binary tree in string representation.Returns a string representation of a binary tree by serializing eachnode valueusing the defaultObjects.toString(Object).Returns a string representation of a binary tree by serializing eachnode valueusing a specified strategy.static <T> BinaryTreeSerde<T> usingDelimiterOf(char leftDelimiter, char rightDelimiter) Creates aBinaryTreeSerdeinstance using the specified the delimiter characters.static <T> BinaryTreeSerde<T> Creates aBinaryTreeSerdeinstance using '(' and ')' as the delimiter characters.
-
Method Details
-
usingParenthesesDelimiter
Creates aBinaryTreeSerdeinstance using '(' and ')' as the delimiter characters.- Type Parameters:
T- The type of value store in the tree node- Returns:
- a new instance
-
usingDelimiterOf
Creates aBinaryTreeSerdeinstance using the specified the delimiter characters.- Type Parameters:
T- The type of value store in the tree node- Parameters:
leftDelimiter- The provided left delimiter character, i.e. the char before the first node value charrightDelimiter- The provided right delimiter character, i.e. the char after the last node value char- Returns:
- a new instance
-
serialize
Returns a string representation of a binary tree by serializing eachnode valueusing the defaultObjects.toString(Object).- Parameters:
root- The root of the binary tree- Returns:
- the string representation of the binary tree
- Throws:
NullPointerException- if anyrootisnull
-
serialize
Returns a string representation of a binary tree by serializing eachnode valueusing a specified strategy.- Parameters:
root- The root of the binary treeserializer- The providednode valueserialization strategy- Returns:
- the string representation of the binary tree
- Throws:
NullPointerException- if any argument isnull
-
deserialize
Deserializes a binary tree in string representation.This method serializes in O(n) time invalid input: '&' space without recursion.
The internal of this method first constructs a tree with mutable nodes and then makes an immutable copy of it.
- Parameters:
string- The string representation of the treedeserializer- A strategy to transform a string representation to object- Returns:
- the root of the deserialized tree
- Throws:
NullPointerException- ifstringordeserializerisnull
-