Simple trees
The aim of this module is to represent the standard tree structure in efficiency-non-sensitive cases. Please note that the default implementation of tree methods are far from optimal in efficiency. Therefore, please DO NOT use it if you need an efficient tree for addition, deletion, sort and inquiry. This module of codes apply only when the structure of tree matters but not the efficiency.
AbstractSimpleTree
AbstractSimpleTree{N, D}
is the abstract type for all concrete trees. By design, it has two type parameters:
N
: the type of the tree's nodeD
: the type of the tree's data
To fully utilize the methods designed for a tree structure, in our protocol, a concrete subtype must implement the following methods:
- inquiry related methods
Get a tree's root node (root(tree::AbstractSimpleTree{N}) where N -> Union{N, Nothing}
nothing
for empty trees)
Check whether a node is in a tree.haskey(tree::AbstractSimpleTree{N}, node::N) where N -> Bool
Get the number of a tree's nodes.length(tree::AbstractSimpleTree) -> Int
Get the parent of a tree's node or return superparent when the input node is the tree's root.parent(tree::AbstractSimpleTree{N}, node::N, superparent::Union{N, Nothing}=nothing ) where N -> Union{N, Nothing}
Get the children of a tree's node.children(tree::AbstractSimpleTree{N}, node::N) where N -> Vector{N}
- structure modification related methods
Update the structure of a tree by adding a node. When the parent isaddnode!(tree::AbstractSimpleTree{N}, parent::Union{N, Nothing}, node::N ) where N -> typeof(tree)
nothing
, the input tree must be empty and the input node becomes the tree's root.
Update the structure of a tree by deleting a node.deletenode!(tree::AbstractSimpleTree{N}, node::N) where N -> typeof(tree)
- index related methods
Get the data of a tree's nodegetindex(tree::AbstractSimpleTree{N, D}, node::N) where {N, D} -> D
Set the data of a tree's node.setindex!(tree::AbstractSimpleTree{N, D}, node::N, data::D) where {N, D}
Based on these methods, we implement several generic functions for inquiries and manipulations
- inquiry for type parameters:
keytype
,valtype
,eltype
- expansion over nodes/data-records:
keys
,values
,pairs
- inquiry for info of nodes:
isleaf
,level
- inquiry for nodes:
ancestor
,descendants
,siblings
,leaves
- modification:
push!
,append!
,delete!
,empty!
And optionally, when a subtype implement the following method,
empty(tree::AbstractSimpleTree) -> typeof(tree)
which constructs an empty tree of the same type with the input one, two more methods are supported:
SimpleTreeCore and SimpleTree
To implement all the prerequisites listed above costs a bit efforts. We provide two lazy ways to get over this:
- Inheritance
AbstractSimpleTree
withTREECORE::SimpleTreeCore
as one of its attribute - Inclusion an attribute which is an instance of
SimpleTree
SimpleTreeCore
SimpleTreeCore{N, D}
, as the literal meaning indicates, is the core of a tree. It encapsulates all the data structures needed by the default implementation, which contains 4 attributes:
root::N
: the tree's root nodecontents::Dict{N, D}
: the tree's (node, data) pairsparent::Dict{N, N}
: records of the parent of each of the tree's nodeschildren::Dict{N, Vector{N}}
: records of the children of each of the tree's nodes
As above, the first lazy way is to include this struct with the special name :TREECORE
in your concrete subtype one of its attribute.
SimpleTree
SimpleTree{N, D}
is the minimum struct that implements all the default tree methods. You can include an instance of it as an attribute in your own type to utilize all the tree methods.
Manual
QuantumLattices.Prerequisites.SimpleTrees.simpletreedepth
— Constantsimpletreedepth
Indicate that the iteration over a tree is depth-first.
QuantumLattices.Prerequisites.SimpleTrees.simpletreewidth
— Constantsimpletreewidth
Indicate that the iteration over a tree is width-first.
QuantumLattices.Prerequisites.SimpleTrees.AbstractSimpleTree
— TypeAbstractSimpleTree{Node, Data}
Abstract type for all concrete trees.
QuantumLattices.Prerequisites.SimpleTrees.SimpleTree
— TypeSimpleTree{N, D}() where {N, D}
The minimum tree structure that implements all the default tree methods.
QuantumLattices.Prerequisites.SimpleTrees.SimpleTreeCore
— TypeSimpleTreeCore()
The core of a simple tree.
QuantumLattices.Prerequisites.SimpleTrees.SimpleTreeCore
— MethodSimpleTreeCore(tree::AbstractSimpleTree) -> SimpleTreeCore
Get the core of a simple tree.
Base.:==
— Method==(tc1::TC, tc2::TC) where TC<:SimpleTreeCore -> Bool
isequal(tc1::TC, tc2::TC) where TC<:SimpleTreeCore -> Bool
Overloaded equivalent operator.
Base.:==
— Method==(t1::T, t2::T) where {T<:AbstractSimpleTree} -> Bool
Overloaded equivalent operator.
Base.append!
— Methodappend!(tree::AbstractSimpleTree{N, D}, subtree::AbstractSimpleTree{N, D}) where {N, D} -> typeof(tree)
append!(tree::AbstractSimpleTree{N, D}, node::Union{N, Nothing}, subtree::AbstractSimpleTree{N, D}) where {N, D} -> typeof(tree)
Append a subtree to a tree.
Base.delete!
— Methoddelete!(tree::AbstractSimpleTree{N}, node::N) where N -> typeof(tree)
Delete a node and all its descendants from a tree.
Base.eltype
— Methodeltype(tree::AbstractSimpleTree)
eltype(::Type{T}) where {T<:AbstractSimpleTree}
Get the eltype of a tree.
Base.empty!
— Methodempty!(tree::AbstractSimpleTree) -> typeof(tree)
Empty a tree.
Base.empty
— Methodempty(tree::AbstractSimpleTree)
Construct an empty tree of the same type with the input one.
Base.getindex
— Methodgetindex(tree::AbstractSimpleTree{N}, node::N) where N -> N
Get the data of a tree's node.
Base.haskey
— Methodhaskey(tree::AbstractSimpleTree{N}, node::N) where N -> Bool
Check whether a node is in a tree.
Base.isequal
— Methodisequal(t1::T, t2::T) where {T<:AbstractSimpleTree} -> Bool
Overloaded equivalent operator.
Base.keys
— Methodkeys(tree::AbstractSimpleTree{N}, ::SimpleTreeDepth, node::Union{N, Nothing}=root(tree)) where N
keys(tree::AbstractSimpleTree{N}, ::SimpleTreeWidth, node::Union{N, Nothing}=root(tree)) where N
Iterate over a tree's nodes starting from a certain node
by depth first search or width first search.
Base.keytype
— Methodkeytype(tree::AbstractSimpleTree)
keytype(::Type{T}) where {T<:AbstractSimpleTree}
Get a tree's node type.
Base.length
— Methodlength(tree::AbstractSimpleTree) -> Int
Get the number of a tree's nodes.
Base.pairs
— Methodpairs(tree::AbstractSimpleTree{N}, ::SimpleTreeDepth, node::Union{N, Nothing}=root(tree)) where N
pairs(tree::AbstractSimpleTree{N}, ::SimpleTreeWidth, node::Union{N, Nothing}=root(tree)) where N
Iterate over a tree's (node, data) pairs starting from a certain node
by depth first search or width first search.
Base.push!
— Methodpush!(tree::AbstractSimpleTree{N, D}, node::N, data::D) where {N, D} -> typeof(tree)
push!(tree::AbstractSimpleTree{N, D}, parent::Union{N, Nothing}, node::N, data::D) where {N, D} -> typeof(tree)
Push a new node to a tree. When parent
is nothing
, this function set the root node of an empty tree.
Base.setindex!
— Methodsetindex!(tree::AbstractSimpleTree{N, D}, data::D, node::N) where {N, D}
Set the data of a tree's node.
Base.valtype
— Methodvaltype(tree::AbstractSimpleTree)
valtype(::Type{T}) where {T<:AbstractSimpleTree}
Get a tree's data type.
Base.values
— Methodvalues(tree::AbstractSimpleTree{N}, ::SimpleTreeDepth, node::Union{N, Nothing}=root(tree)) where N
values(tree::AbstractSimpleTree{N}, ::SimpleTreeWidth, node::Union{N, Nothing}=root(tree)) where N
Iterate over a tree's data starting from a certain node
by depth first search or width first search.
QuantumLattices.Prerequisites.SimpleTrees.addnode!
— Methodaddnode!(tree::AbstractSimpleTree{N}, node::N) where N} -> typeof(tree)
addnode!(tree::AbstractSimpleTree{N}, ::Nothing, node::N) where N -> typeof(tree)
addnode!(tree::AbstractSimpleTree{N}, parent::N, node::N) where N -> typeof(tree)
Update the structure of a tree by adding a node. When the parent is nothing
, the input tree must be empty and the input node becomes the tree's root.
QuantumLattices.Prerequisites.SimpleTrees.ancestor
— Methodancestor(tree::AbstractSimpleTree{N}, node::N, generation::Int=1) where N -> N
Get the ancestor of a tree's node of the n-th generation.
QuantumLattices.Prerequisites.SimpleTrees.children
— Methodchildren(tree::AbstractSimpleTree) -> Vector{keytype(tree)}
children(tree::AbstractSimpleTree, ::Nothing) -> Vector{keytype(tree)}
children(tree::AbstractSimpleTree{N}, node::N) where N -> Vector{N}
Get the children of a tree's node.
QuantumLattices.Prerequisites.SimpleTrees.deletenode!
— Methoddeletenode!(tree::AbstractSimpleTree{N}, node::N) where N -> typeof(tree)
Update the structure of a tree by deleting a node.
QuantumLattices.Prerequisites.SimpleTrees.descendants
— Methoddescendants(tree::AbstractSimpleTree{N}, node::N, generation::Int=1) where N -> Vector{N}
Get the descendants of a tree's node of the nth generation.
QuantumLattices.Prerequisites.SimpleTrees.isleaf
— Methodisleaf(tree::AbstractSimpleTree{N}, node::N) where N -> Bool
Judge whether a tree's node is a leaf (a node without children) or not.
QuantumLattices.Prerequisites.SimpleTrees.leaves
— Methodleaves(tree::AbstractSimpleTree) -> Vector{keytype(tree)}
Get a tree's leaves.
QuantumLattices.Prerequisites.SimpleTrees.level
— Methodlevel(tree::AbstractSimpleTree{N}, node::N) where N -> Int
Get the level of tree's node.
QuantumLattices.Prerequisites.SimpleTrees.move!
— Methodmove!(tree::AbstractSimpleTree{N}, node::N, parent::N) where N -> typeof(tree)
Move a subtree to a new position.
QuantumLattices.Prerequisites.SimpleTrees.parent
— Methodparent(tree::AbstractSimpleTree{N}, node::N, superparent::Union{N, Nothing}=nothing) where N -> Union{N, Nothing}
Get the parent of a tree's node. When node
is the tree's root, return superparent
.
QuantumLattices.Prerequisites.SimpleTrees.root
— Methodroot(tree::AbstractSimpleTree) -> Union{keytype(tree), Nothing}
Get a tree's root node.
QuantumLattices.Prerequisites.SimpleTrees.siblings
— Methodsiblings(tree::AbstractSimpleTree{N}, node::N) where N -> Vector{N}
Get the siblings (other nodes sharing the same parent) of a tree's node.
QuantumLattices.Prerequisites.SimpleTrees.subtree
— Methodsubtree(tree::AbstractSimpleTree{N}, node::N) where N -> typeof(tree)
Get a subtree whose root is node
.