Class: Treebank::ParentedNode

Inherits:
Node
  • Object
show all
Defined in:
lib/treebank.rb

Overview

A Node in a tree that can locate its parent.

The ParentedNode adds a pointer back to the parent node to the Node class.

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #label

Instance Method Summary collapse

Methods inherited from Node

#==, #[], #breadth_first_enumerator, #create_child!, #depth_first_enumerator, #each, #each_breadth_first, #each_depth_first, #empty?, from_s, #inspect, #leaf?, #leaves, #preterminal?, #to_s

Constructor Details

#initialize(label = nil, child_labels = [], parent = nil) ⇒ ParentedNode

Create a node specifying its parent, its label, and its children’s labels.

label

The label of this node

child_labels

List of labels for children of this node

parent

The parent of this node



496
497
498
499
# File 'lib/treebank.rb', line 496

def initialize(label = nil, child_labels = [], parent = nil)
  super(label, child_labels)
  @parent = parent
end

Instance Attribute Details

#parentObject

This node’s parent



488
489
490
# File 'lib/treebank.rb', line 488

def parent
  @parent
end

Instance Method Details

#attach_child!(node, index = nil) ⇒ Object

See Treebank::Node.attach_child!



502
503
504
505
506
# File 'lib/treebank.rb', line 502

def attach_child!(node, index = nil)
  child = super(node, index)
  child.parent = self
  child
end

#detach_child!(node) ⇒ Object

See Treebank::Node.detach_child!



509
510
511
512
# File 'lib/treebank.rb', line 509

def detach_child!(node)
  super(node)
  node.parent = nil
end

#each_parentObject

Enumerate this node and its ancestors.



523
524
525
526
527
528
529
# File 'lib/treebank.rb', line 523

def each_parent
  node = self
  while not node.nil?
    yield node
    node = node.parent
  end
end

#parent_enumeratorObject

Return an Enumerator object that enumerates this node and its parents.



533
534
535
# File 'lib/treebank.rb', line 533

def parent_enumerator
  Enumerable::Enumerator.new(self, :each_parent)
end