tree_node

class tree_node #(type T =  int)

Implements a node of a tree.

Parameter

Toptional The type of data collected in a tree_node.  The default is int.
Summary
tree_nodeImplements a node of a tree.
Types
tree_node_typeThe shorthand of the tree node type specialized with type T.
Properties
elemThe element stored at this tree node.
parentThe parent node of this tree node.
childrenThe child nodes this tree node has.
relativesThe tree nodes this tree node is related to.
locationThe queue indicating the location of this tree node in a tree.
Functions
newCreates a new tree node.
addvirtual Creates a tree node of the given element and adds it as a new child.
graftvirtual Grafts the given tree node (and its children) as a new child.
prunevirtual Removes the specified child (and its descendants).
get_num_childrenvirtual Returns the number of (immediate) children of this tree node.
has_childvirtual Returns if this tree node has at least one child.

Types

tree_node_type

typedef tree_node#(T) tree_node_type

The shorthand of the tree node type specialized with type T.

Properties

elem

T elem

The element stored at this tree node.

parent

tree_node_type parent

The parent node of this tree node.

children

tree_node_type children[$]

The child nodes this tree node has.

relatives

tree_node_type relatives[$]

The tree nodes this tree node is related to.  Usage of this property is up to the user.

location

int location[$]

The queue indicating the location of this tree node in a tree.  Every child appends its index to the queue.  The new, add, graft, and prune functions do not update the location automatically.  You must call tree::update_locations to update the entire tree-node locations.  To get a human-readable location string, use tree::get_location_name.

Example

[0] -+-- [0,0] -+-- [0,0,0] ---- [0,0,0,0]
root |          |
node |          +-- [0,0,1]
     |
     +-- [0,1] -+-- [0,1,0] ---- [0,1,0,0]
     |          |
     |          +-- [0,1,1] -+-- [0,1,1,0]
     |                       |
     |                       +-- [0,1,1,1]
     |                       |
     |                       +-- [0,1,1,2]
     +-- [0,2]

See Also

tree::get_location_name

Functions

new

function new(elem)

Creates a new tree node.

Argument

elemData element stored at the tree node.

Example

int i = 123;
tree_node#(int) tn = new( i );

add

virtual function tree_node_type add(e)

virtual Creates a tree node of the given element and adds it as a new child.

Argument

eAn element to be added.

Returns

Newly added tree node.

Example

tree_node#(int) tn;
tree_node#(int) tn_123 = new( 123 );

tn = tn_123.add( 234 );
// (123) ---- (234) <~~ tn

tn = tn_123.add( 345 );
// (123) -+-- (234)
//        |
//        +-- (345) <~~ tn

tn = tn_123.add( 456 ).add( 567 ); // chain
// (123) -+-- (234)
//        |
//        +-- (345)
//        |
//        +-- (456) ---- (567) <~~ tn

graft

virtual function tree_node_type graft(tree_node_type tn)

virtual Grafts the given tree node (and its children) as a new child.

Argument

tnA tree node to be grafted.

Returns

A tree node to be grafted (tn).

Example

tree_node#(int) tn_123;
tree_node#(int) tn_234;
tree_node#(int) tn_345;
tree_node#(int) tn_456;
tree_node#(int) tn;

tn_123 = new( 123 );
tn_234 = tn_123.add( 234 );
// (123) --- (234)
//             \__ tn_234

tn_345 = new( 345 );
tn_456 = tn_345.add( 456 );
// (345) ---- (456)
//   \__ tn_345

tn = tn_234.graft( tn_345 );
// (123) ---- (234) --- (345) ---- (456)
//                        \__ tn

prune

virtual function tree_node_type prune(int index =  0)

virtual Removes the specified child (and its descendants).

Argument

indexoptional The index of the child.  The default is 0.  If the specified child does not exist, this function does nothing.

Returns

This tree node.

Example

tree_node#(int) tn;
tree_node#(int) tn_123 = new( 123 );

tn = tn_123.add( 234 );
tn = tn_123.add( 456 ).add( 567 );
tn = tn_123.add( 345 );
// (123) -+-- (234)
//        |
//        +-- (456) ---- (567)
//        |
//        +-- (345)

tn = tn_123.prune( .index( 1 ) );
// (123) -+-- (234)
//        |
//        +-- (345)

get_num_children

virtual function int get_num_children()

virtual Returns the number of (immediate) children of this tree node.  This function does not count the number of descendants recursively.

Returns

The number of children.

Example

tree_node#(int) tn;
tree_node#(int) tn_123 = new( 123 );

tn = tn_123.add( 234 );
tn = tn_123.add( 345 );
tn = tn_123.add( 456 ).add( 567 );
// (123) -+-- (234)
//        |
//        +-- (345)
//        |
//        +-- (456) ---- (567)

assert( tn_123.get_num_children() == 3 ); // not 4

has_child

virtual function bit has_child()

virtual Returns if this tree node has at least one child.

Returns

If this tree node has a child (or children), returns 1.  Otherwise, returns 0.

Example

tree_node#(int) tn;
tree_node#(int) tn_123 = new( 123 );

tn = tn_123.add( 234 );
tn = tn_123.add( 345 );
tn = tn_123.add( 456 ).add( 567 );
// (123) -+-- (234)
//        |
//        +-- (345)
//        |
//        +-- (456) ---- (567)

assert( tn_123.has_child() == 1 );
class tree_node #(type T =  int)
Implements a node of a tree.
class tree #(type T =  int) extends collection#( T )
Implements a tree structure.
typedef tree_node#(T) tree_node_type
The shorthand of the tree node type specialized with type T.
T elem
The element stored at this tree node.
tree_node_type parent
The parent node of this tree node.
tree_node_type children[$]
The child nodes this tree node has.
tree_node_type relatives[$]
The tree nodes this tree node is related to.
int location[$]
The queue indicating the location of this tree node in a tree.
function new(elem)
Creates a new tree node.
virtual function tree_node_type add(e)
virtual Creates a tree node of the given element and adds it as a new child.
virtual function tree_node_type graft(tree_node_type tn)
virtual Grafts the given tree node (and its children) as a new child.
virtual function tree_node_type prune(int index =  0)
virtual Removes the specified child (and its descendants).
virtual function int get_num_children()
virtual Returns the number of (immediate) children of this tree node.
virtual function bit has_child()
virtual Returns if this tree node has at least one child.
virtual function void update_locations()
virtual Updates the tree_node::location of the entire tree.
virtual function string get_location_name(tree_node_type tn)
virtual Returns the human-readable location string of this node from the root.