Package io.openml.gearbox.binarytree
Class BinaryTreeSerde<T>
java.lang.Object
io.openml.gearbox.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 & 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 <---> "1(2(4))(3)" / 4 ---------------------------------- 1 / \ 2 3 <---> "1(2()(4))(3)" \ 4Note 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 value
using the defaultObjects.toString(Object)
.Returns a string representation of a binary tree by serializing eachnode value
using a specified strategy.static <T> BinaryTreeSerde<T>
usingDelimiterOf
(char leftDelimiter, char rightDelimiter) Creates aBinaryTreeSerde
instance using the specified the delimiter characters.static <T> BinaryTreeSerde<T>
Creates aBinaryTreeSerde
instance using '(' and ')' as the delimiter characters.
-
Method Details
-
usingParenthesesDelimiter
Creates aBinaryTreeSerde
instance using '(' and ')' as the delimiter characters.- Type Parameters:
T
- The type of value store in the tree node- Returns:
- a new instance
-
usingDelimiterOf
Creates aBinaryTreeSerde
instance 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 value
using the defaultObjects.toString(Object)
.- Parameters:
root
- The root of the binary tree- Returns:
- the string representation of the binary tree
- Throws:
NullPointerException
- if anyroot
isnull
-
serialize
Returns a string representation of a binary tree by serializing eachnode value
using a specified strategy.- Parameters:
root
- The root of the binary treeserializer
- The providednode value
serialization 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 & 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
- ifstring
ordeserializer
isnull
-