Table of Contents

Class BaseTree<TValue, TTree, TTreeNode>

Namespace
Garyon.DataStructures.Trees
Assembly
Garyon.dll

Represents a tree.

public abstract class BaseTree<TValue, TTree, TTreeNode> : ITree<TValue, TTree, TTreeNode>, ITree<TValue>, ICollection<TValue>, IEnumerable<TValue>, IEnumerable where TTree : BaseTree<TValue, TTree, TTreeNode> where TTreeNode : BaseTreeNode<TValue, TTree, TTreeNode>

Type Parameters

TValue

The type of the elements the tree nodes store.

TTree

The type of the tree that this type is used in.

TTreeNode

The type of the tree nodes that are used in the TTree type.

Inheritance
BaseTree<TValue, TTree, TTreeNode>
Implements
ITree<TValue, TTree, TTreeNode>
ITree<TValue>
ICollection<TValue>
IEnumerable<TValue>
Derived
Inherited Members
Extension Methods

Constructors

BaseTree()

Initializes a new instance of the Tree<T> class with no root node.

public BaseTree()

BaseTree(TValue)

Initializes a new instance of the Tree<T> class with a root value.

public BaseTree(TValue rootValue)

Parameters

rootValue TValue

The value of the root node.

BaseTree(TTree)

Initializes a new instance of the Tree<T> class from a different tree.

public BaseTree(TTree tree)

Parameters

tree TTree

The tree to create this tree from. Both trees remain independent.

BaseTree(TTreeNode)

Initializes a new instance of the Tree<T> class with a root node.

public BaseTree(TTreeNode root)

Parameters

root TTreeNode

The root node.

Fields

CachedCount

The cached count of elements within this tree.

It is null if the element count has never been calculated.

If the value is not null and any modification occurs, it will be set to null.

protected int? CachedCount

Field Value

int?

InternalRoot

The internal field for the root of this tree. If no side-effects are bound to occur, prefer setting this over the Root property.

protected TTreeNode InternalRoot

Field Value

TTreeNode

Properties

Breadth

Gets the breadth of this tree. It is equal to the breadth of the root node.

public int Breadth { get; }

Property Value

int

Count

Gets the number of elements that this tree has. The count is only calculated upon the first request after any adjustments that were made to the tree.

public int Count { get; }

Property Value

int

Height

Gets the height of this tree. It is equal to the height of the root node.

public int Height { get; }

Property Value

int

Root

Gets or sets the root node.

public TTreeNode Root { get; set; }

Property Value

TTreeNode

This

Gets this tree node instance as a TTree.

protected TTree This { get; }

Property Value

TTree

Methods

Clear()

Clears all this tree's nodes, including its root.

public void Clear()

ClearChildren(TValue)

Clears the children of a node with the specified value, that is within this tree.

public void ClearChildren(TValue value)

Parameters

value TValue

The value of the parent node whose children to clear. The node must be within this tree.

ClearChildren(TValue, bool)

Clears the children of a node with the specified value, that is within this tree.

public void ClearChildren(TValue value, bool propagate)

Parameters

value TValue

The value of the parent node whose children to clear. The node must be within this tree.

propagate bool

Determines whether this function should propagate to the Children property. Only set this to false if the new children are already set in the node before calling this function.

ClearChildren(TTreeNode)

Clears the children of a node within this tree.

public void ClearChildren(TTreeNode node)

Parameters

node TTreeNode

The parent node whose children to clear. The node must be within this tree.

ClearChildren(TTreeNode, bool)

Clears the children of a node within this tree.

public abstract void ClearChildren(TTreeNode node, bool propagate)

Parameters

node TTreeNode

The parent node whose children to clear. The node must be within this tree.

propagate bool

Determines whether this function should propagate to the Children property. Only set this to false if the new children are already set in the node before calling this function.

Contains(TValue)

Determines whether this tree contains a node with the specified value.

public virtual bool Contains(TValue value)

Parameters

value TValue

The value to determine whether it is contained within the tree.

Returns

bool

A value determining whether the requested value was found.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

Equals(TTree)

public bool Equals(TTree other)

Parameters

other TTree

Returns

bool

GetEnumerator()

Gets an enumerator that traverses the tree using pre-order traversal.

public IEnumerator<TValue> GetEnumerator()

Returns

IEnumerator<TValue>

The IEnumerator<T> that traverses the tree using pre-order traversal.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

GetNode(TValue)

Gets a child node within this tree that has the specified value.

public virtual TTreeNode GetNode(TValue value)

Parameters

value TValue

The value of the child tree node to find.

Returns

TTreeNode

The TreeNode<T> with the specified value, if found; otherwise false.

GetTreeView(int)

Gets the tree view of this tree node as a subtree.

public string GetTreeView(int childIndent = 4)

Parameters

childIndent int

The length of the indentation for each subsequent child.

Returns

string

The tree view.

InitializeNewNode(TValue)

Initializes a new instance of the TreeNode<T> class with no base tree or parent node.

protected abstract TTreeNode InitializeNewNode(TValue value = default)

Parameters

value TValue

The value of the node.

Returns

TTreeNode

InitializeNewNode(TTree, TValue)

Initializes a new instance of the TreeNode<T> class with a base tree, but no parent node.

protected abstract TTreeNode InitializeNewNode(TTree baseTree, TValue value = default)

Parameters

baseTree TTree

The base tree that contains this node.

value TValue

The value of the node.

Returns

TTreeNode

InitializeNewNode(TTreeNode, TValue)

Initializes a new instance of the TreeNode<T> class with a parent node. The base tree is considered to be that of the parent.

protected abstract TTreeNode InitializeNewNode(TTreeNode parentNode, TValue value = default)

Parameters

parentNode TTreeNode

The parent node.

value TValue

The value of the node.

Returns

TTreeNode

PrepareChildrenAdjustmentOperation(TTreeNode)

Performs some operations to prepare for any children adjustment operation (add, remove, set, clear).

protected bool PrepareChildrenAdjustmentOperation(TTreeNode node)

Parameters

node TTreeNode

The node on which the children adjustment operation is to be performed.

Returns

bool

true if the operation can be performed, otherwise false. This requires node to not be null and belong to this tree.

Remove(TValue)

Removes a node from the tree that has the value that is equal to the provided value.

public bool Remove(TValue value)

Parameters

value TValue

The value of the node to find and remove.

Returns

bool

true if the node with the requested value was found and removed, otherwise false.

RemoveChild(TTreeNode, TTreeNode, bool)

Removes a child from a node within this tree.

public bool RemoveChild(TTreeNode node, TTreeNode childToRemove, bool propagate)

Parameters

node TTreeNode

The parent node whose children to set. The node must be within this tree.

childToRemove TTreeNode

The child to remove from the node.

propagate bool

Determines whether this function should propagate to the RemoveChild(TTreeNode) function. Only set this to false if the new child was already added before calling this function.

Returns

bool

true if the child was successfully removed from the node, otherwise false.

RemoveNode(TValue)

Removes a child within this tree.

public bool RemoveNode(TValue value)

Parameters

value TValue

The value of the child to remove from the node.

Returns

bool

RemoveNode(TValue, bool)

Removes a child within this tree.

public bool RemoveNode(TValue value, bool propagate)

Parameters

value TValue

The value of the child to remove from the node.

propagate bool

Determines whether this function should propagate to the RemoveChild(TTreeNode) function. Only set this to false if the new child was already added before calling this function.

Returns

bool

true if the node was successfully removed, otherwise false.

RemoveNode(TTreeNode)

Removes a child within this tree.

public bool RemoveNode(TTreeNode childToRemove)

Parameters

childToRemove TTreeNode

The child to remove from the node.

Returns

bool

RemoveNode(TTreeNode, bool)

Removes a child within this tree.

public bool RemoveNode(TTreeNode childToRemove, bool propagate)

Parameters

childToRemove TTreeNode

The child to remove from the node.

propagate bool

Determines whether this function should propagate to the RemoveChild(TTreeNode) function. Only set this to false if the new child was already added before calling this function.

Returns

bool

true if the node was successfully removed, otherwise false.

RemoveNodes(IEnumerable<TValue>)

Removes nodes within this tree that contain the specified values.

public void RemoveNodes(IEnumerable<TValue> values)

Parameters

values IEnumerable<TValue>

The values of the children to remove.

RemoveNodes(TTreeNode, IEnumerable<TValue>)

Removes nodes within a subtree with root as the specified node that contain the specified values.

public void RemoveNodes(TTreeNode node, IEnumerable<TValue> values)

Parameters

node TTreeNode

The node which is the root of the subtree in which to remove the nodes at.

values IEnumerable<TValue>

The values of the children to remove.

TraverseLevelOrder()

Traverses the tree from the root using level-order. The elements are returned with yield return.

public IEnumerable<TValue> TraverseLevelOrder()

Returns

IEnumerable<TValue>

The yielded values.

TraverseLevelOrderNodes()

Traverses the tree from the root using level-order. The nodes are returned with yield return.

public IEnumerable<TTreeNode> TraverseLevelOrderNodes()

Returns

IEnumerable<TTreeNode>

The yielded nodes.

TraversePostOrder()

Traverses the tree from the root using post-order. The elements are returned with yield return.

public IEnumerable<TValue> TraversePostOrder()

Returns

IEnumerable<TValue>

The yielded values.

TraversePostOrderNodes()

Traverses the tree from the root using post-order. The nodes are returned with yield return.

public IEnumerable<TTreeNode> TraversePostOrderNodes()

Returns

IEnumerable<TTreeNode>

The yielded nodes.

TraversePreOrder()

Traverses the tree from the root using pre-order. The elements are returned with yield return.

public IEnumerable<TValue> TraversePreOrder()

Returns

IEnumerable<TValue>

The yielded values.

TraversePreOrderNodes()

Traverses the tree from the root using pre-order. The nodes are returned with yield return.

public IEnumerable<TTreeNode> TraversePreOrderNodes()

Returns

IEnumerable<TTreeNode>

The yielded nodes.