Class Path<T>
- Type Parameters:
T
- The type of value store in theTreeNode
Path
represents a one-way path between two TreeNode
s in a binary tree.
The Path
can be root-to-leaf or node-to-node. The internal of Path
is a doubly linked list, with each
list node wrapping a TreeNode
as list node value.
Path
is designed for enhancing the performance of
path-traversing algorithm
. For example, we can cut a
Path
in the middle using trim(TreeNode)
in constant time, instead of iteratively removing path
tails(O(n) time). In the meantime, we can also get the length of the Path
in constant time.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Appends aTreeNode
to the end of this path.static <T> Path<T>
deepCopyOf
(Path<T> other) static <T> Path<T>
getLast()
Returns the tail of this path.boolean
isEmpty()
Returns whether there are anyTreeNode
already been added to the path yet.int
length()
Returns the length of this path.Deletes the tail of this path.toString()
void
Keeps removingTreeNode
elements from end of an under-construction path (last, 2nd last, 3rd last ...) until the element to be removed equals to a specified element.view()
Returns an immutable view of all path elements in thisPath
at the time of calling this method.
-
Constructor Details
-
Path
public Path()
-
-
Method Details
-
emptyPath
-
deepCopyOf
Constructs aPath
containing the path elements of the specifiedPath
, in the order they are contained in the specifiedPath
.- Type Parameters:
T
- The type of value store in theTreeNode
- Parameters:
other
- ThePath
whose path elements are to be placed into the newPath
- Returns:
- a new
Path
instance - Throws:
NullPointerException
- if theother
isnull
-
view
Returns an immutable view of all path elements in thisPath
at the time of calling this method.Adding or removing elements in either the view or the original
Path
is not reflected on the other side. In other words, since a binary tree is immutable(becauseTreeNode
is immutable), mutating the tree(or a path of it) must result in a new view.Generating the view takes O(n) time where n is the length of the list.
- Returns:
- a
List
of current path elements of a binary tree path
-
trim
Keeps removingTreeNode
elements from end of an under-construction path (last, 2nd last, 3rd last ...) until the element to be removed equals to a specified element.Path is assumed not to be empty.
This method executes in constant time.
- Parameters:
expectedTail
- The specified element that signals the stop of the trimming process
-
addLast
Appends aTreeNode
to the end of this path.- Parameters:
pathElement
- TheTreeNode
to append
-
getLast
Returns the tail of this path.- Returns:
- a
TreeNode
reference - Throws:
NoSuchElementException
- if the path isempty
-
isEmpty
public boolean isEmpty()Returns whether there are anyTreeNode
already been added to the path yet.- Returns:
true
is path is empty orfalse
, otherwise
-
removeLast
Deletes the tail of this path.- Returns:
- the
element
that has been removed from this path - Throws:
NoSuchElementException
- if the path isempty
-
length
public int length()Returns the length of this path.- Returns:
- the number of
TreeNode
s in this path
-
toString
-