Class: CodeCook::Greedy::Node

Inherits:
Object
  • Object
show all
Defined in:
手写代码必备手册(Ruby版).rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Node) initialize(params = {})

Returns a new instance of Node



544
545
546
547
548
549
550
# File '手写代码必备手册(Ruby版).rb', line 544

def initialize(params = {})
    @weight   = params[:weight]  || 0
    @symbol   = params[:symbol]  || ''
    @left     = params[:left]    || nil
    @right    = params[:right]   || nil
    @parent   = params[:parent]  || nil
end

Instance Attribute Details

- (Object) left

Returns the value of attribute left



542
543
544
# File '手写代码必备手册(Ruby版).rb', line 542

def left
  @left
end

- (Object) parent

Returns the value of attribute parent



542
543
544
# File '手写代码必备手册(Ruby版).rb', line 542

def parent
  @parent
end

- (Object) right

Returns the value of attribute right



542
543
544
# File '手写代码必备手册(Ruby版).rb', line 542

def right
  @right
end

- (Object) symbol

Returns the value of attribute symbol



542
543
544
# File '手写代码必备手册(Ruby版).rb', line 542

def symbol
  @symbol
end

- (Object) weight

Returns the value of attribute weight



542
543
544
# File '手写代码必备手册(Ruby版).rb', line 542

def weight
  @weight
end

Instance Method Details

- (Boolean) internal?

Returns:

  • (Boolean)


566
567
568
# File '手写代码必备手册(Ruby版).rb', line 566

def internal?
    @symbol == ''
end

- (Boolean) leaf?

Returns:

  • (Boolean)


562
563
564
# File '手写代码必备手册(Ruby版).rb', line 562

def leaf?
    @symbol != ''
end

- (Boolean) root?

Returns:

  • (Boolean)


570
571
572
# File '手写代码必备手册(Ruby版).rb', line 570

def root?
    internal? and @parent.nil?
end

- (Object) walk(&block)



552
553
554
# File '手写代码必备手册(Ruby版).rb', line 552

def walk(&block)
    walk_node('', &block)
end

- (Object) walk_node(code) {|_self, code| ... }

Yields:

  • (_self, code)

Yield Parameters:



556
557
558
559
560
# File '手写代码必备手册(Ruby版).rb', line 556

def walk_node(code, &block)
    yield(self, code)
    @left.walk_node(code + '0', &block) unless @left.nil?
    @right.walk_node(code + '1', &block) unless @right.nil?
end