Class BinaryTree
java.lang.Object
io.openml.gearbox.algorithms.binarytree.BinaryTree
Binary tree gearbox that can be used practically.
(Art of Computer Programming - Volume 1, Ch. 2.3)A binary tree is an information structure recursively defined formally as a finite T of one or more nodes such that
- there is one specially designated node called the root of the tree, root(T); and
- the remaining nodes (excluding the root) are partitioned into m ≥ 0 disjoint sets T_1, ..., T_m and each of these sets in turn is a tree. The trees T_1, ..., T_m are called the subtrees of the root
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,R> boolean checkEqualTree(TreeNode<T> root, Function<TreeNode<T>, R> computeProperty) Returnstrueif it is possible to partition a binary tree to two trees such that both have the same specified property after removing exactly one edge from the original tree.findDuplicateSubtrees(TreeNode<T> root, Function<T, String> serializer) Return all duplicate subtrees in a binary tree.static <T> MutableTreeNode<T> flatten(MutableTreeNode<T> root) Flattens a binary tree to a linked list.generateBst(int max) Returns all structurally unique binary search trees that store values 1 ...static <T> TreeNode<T> getRightMostNode(TreeNode<T> root) Returns the right most leaf of a binary tree.static <T> TreeNode<T> invertTree(TreeNode<T> root) Inverts a binary tree.static <T> intlargestBstSubtree(TreeNode<T> root, Comparator<T> comparator) Returns the size of the largest BST in a binary tree.static <T> intlongestConsecutive(TreeNode<T> root, BiPredicate<T, T> isConsecutive) Returns the length of the longest consecutive sequence path of a binary tree.static <T> intlongestConsecutiveNodeToNode(TreeNode<T> root, BiPredicate<T, T> isConsecutive) Returns the length of the longest consecutive sequence node-to-node path of a binary tree.static <T> TreeNode<T> mergeTrees(TreeNode<T> first, TreeNode<T> second, BiFunction<T, T, T> valueAdder) Merges two binary trees and returns the root of the new tree.pathCondition(TreeNode<T> root, Predicate<List<TreeNode<T>>> condition) Returns all root-to-leaf paths of a binary tree where each path satisfies a given condition.static <T> voidrecoverTree(TreeNode<T> root, Comparator<T> comparator) Recover a binary search tree whose 2 elements are out of order.static <T> TreeNode<T> subtreeWithAllDeepest(TreeNode<T> root) Returns the binary tree node with the largest depth such that it contains all the deepest nodes in its subtree.static <T> MutableTreeNode<T> upsideDownBinaryTree(MutableTreeNode<T> root) Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, this method flips it upside down, turns it into a tree where the original right nodes turned into left leaf nodes, and return the new root.
-
Method Details
-
recoverTree
Recover a binary search tree whose 2 elements are out of order.- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the binary treecomparator- A strategy that defines the relative ordering of node values. For example, if node values are integers, thenComparator<Integer> comparator = Integer::compare
-
invertTree
-
flatten
Flattens a binary tree to a linked list.If the root is
null, this method does nothing. For example1 1 / \ \ 2 5 => 2 / \ \ \ 3 4 6 3 \ 4 \ 5 \ 6- Type Parameters:
T- The type of value store in the node- Parameters:
root- the root of the binary tree- Returns:
- the head of the flattened list
-
upsideDownBinaryTree
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, this method flips it upside down, turns it into a tree where the original right nodes turned into left leaf nodes, and return the new root.For example
1 / \ 2 3 / \ 4 5becomes4 / \ 5 2 / \ 3 1
- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the original tree- Returns:
- the root of the new tree
-
getRightMostNode
Returns the right most leaf of a binary tree.If the tree root is
null, this method returns null- Type Parameters:
T- The type of value store in the node- Parameters:
root- the root of the tree- Returns:
- the right most leaf of the binary tree.
-
findDuplicateSubtrees
public static <T> List<TreeNode<T>> findDuplicateSubtrees(TreeNode<T> root, Function<T, String> serializer) Return all duplicate subtrees in a binary tree.For example
1 / \ 2 3 / / \ 4 2 4 / 4The duplicates are2 / and 4 4
- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the binary treeserializer- A strategy that generates a unique string for each node of the tree- Returns:
- roots of all duplicates
-
generateBst
Returns all structurally unique binary search trees that store values 1 ... max.For example, given max = 3, return all 5 unique BST's shown below
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3- Parameters:
max- the max value stored in the tree- Returns:
- all structurally unique binary search trees that store values 1 ... max
-
subtreeWithAllDeepest
Returns the binary tree node with the largest depth such that it contains all the deepest nodes in its subtree.For example
This method returns the node with value 2 in the diagram, because 7 and 4 are the deepest nodes. Although nodes 5, 3 and 2 all contain the deepest nodes but node 2 is the smallest subtree among them.3 / \ 5 1 / \ / \ 6 2 0 8 / \ 7 4- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the tree- Returns:
- the binary tree node with the largest depth such that it contains all the deepest nodes in its subtree
-
longestConsecutive
Returns the length of the longest consecutive sequence path of a binary tree.For example, return 3 for
1 \ 3 / \ 2 4 \ 5Return 2 for2 \ 3 / 2 / 1- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the binary treeisConsecutive- A strategy that takes 2 node value objects and determines whether the 2nd is consecutively larger than the 1st. For example, if node value is of integer type, then the strategy is simplyBiPredicate<Integer, Integer> isConsecutive = (int1, int2) -> int2 == int1 + 1;- Returns:
- the length of the longest consecutive sequence path of the binary tree
-
longestConsecutiveNodeToNode
public static <T> int longestConsecutiveNodeToNode(TreeNode<T> root, BiPredicate<T, T> isConsecutive) Returns the length of the longest consecutive sequence node-to-node path of a binary tree.For example, return 3 from
2 / \ 1 3
- Type Parameters:
T- The type of the node value- Parameters:
root- the root of the binary treeisConsecutive- A strategy that takes 2 node value objects and determines whether the 2nd is consecutively larger than the 1st. For example, if node value is of integer type, then the strategy is simplyBiPredicate<Integer, Integer> isConsecutive = (int1, int2) -> int2 == int1 + 1;- Returns:
- the length of the longest consecutive sequence path of the binary tree
-
largestBstSubtree
Returns the size of the largest BST in a binary tree.For example,
10 / \ 5 15 / \ \ 1 8 7
The largest BST is5 / \ 1 8
- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the treecomparator- A strategy that defines the relative ordering of node values. For example, if node values are integers, thenComparator<Integer> comparator = Integer::compare- Returns:
- the size of the largest BST in the binary tree or 0 if
rootisnull
-
mergeTrees
public static <T> TreeNode<T> mergeTrees(TreeNode<T> first, TreeNode<T> second, BiFunction<T, T, T> valueAdder) Merges two binary trees and returns the root of the new tree.The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the non-null node will be used as the node of new tree.
- Type Parameters:
T- The type of the node value- Parameters:
first- The root of the first binary tree to be mergedsecond- The root of the second binary tree to be mergedvalueAdder- A strategy that adds up two node values. For example, if node values are of type integer, thenBiFunction<Integer, Integer, Integer> valueAdder = Integer::sum- Returns:
- the root of the new tree which is the merge of the two trees
-
pathCondition
public static <T> List<List<TreeNode<T>>> pathCondition(TreeNode<T> root, Predicate<List<TreeNode<T>>> condition) Returns all root-to-leaf paths of a binary tree where each path satisfies a given condition.- Type Parameters:
T- The type of the node value- Parameters:
root- The root of the binary treecondition- A strategy that determines whether the condition has been met given an ordered root-to-leaf path- Returns:
- all root-to-leaf paths of the binary tree where each path's sum equals to the given sum.
-
checkEqualTree
public static <T,R> boolean checkEqualTree(TreeNode<T> root, Function<TreeNode<T>, R> computeProperty) Returnstrueif it is possible to partition a binary tree to two trees such that both have the same specified property after removing exactly one edge from the original tree.For example, this method returns
truefor a binary tree with integer node values like the following5 / \ 10 10 / \ 2 3
if we remove the right edge between 5 and right 10 when the provided property is the sum of all nodes- Type Parameters:
T- The type of the node valueR- The type of the property. TheObject.equals(Object)method must be implemented on the type- Parameters:
root- The root of the binary treecomputeProperty- A strategy that computes a defined property of a binary tree- Returns:
trueif it is possible to partition a binary tree with the satisfying condition
-