Author |
Message |
Tony
|
Posted: Fri Sep 02, 2005 5:07 pm Post subject: building trees in Ruby |
|
|
how would I go about putting one together and then traversing it? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Fri Sep 02, 2005 6:23 pm Post subject: (No subject) |
|
|
code: | class BTreeNode
attr_reader :value
attr_accessor :left, :right
def initialize(value, left = nil, right = nil)
@value = value
@left, @right = left, right
end
def to_a
...
end
def each
...
end
end |
|
|
|
|
|
|
Tony
|
Posted: Sat Sep 03, 2005 12:40 am Post subject: (No subject) |
|
|
I don't quite get it. How would a pointer look like, and how would I use it?
Other than an array that is |
|
|
|
|
|
wtd
|
Posted: Sat Sep 03, 2005 12:49 am Post subject: (No subject) |
|
|
No pointers.
But you can have an instance variable be either nil or another BTreeNode. |
|
|
|
|
|
Tony
|
Posted: Sat Sep 03, 2005 1:56 am Post subject: (No subject) |
|
|
I suppose I could always just inline C |
|
|
|
|
|
wtd
|
Posted: Sat Sep 03, 2005 2:58 am Post subject: (No subject) |
|
|
Ok... let's look at traversing a Ruby binary tree.
code: | def values
output = [@value]
output.push(*@left.values) unless @left.nil?
output.push(*@right.values) unless @right.nil?
output
end |
|
|
|
|
|
|
Cervantes
|
|
|
|
|
wtd
|
Posted: Mon Sep 05, 2005 5:37 pm Post subject: (No subject) |
|
|
The very short answer...
"nil" is your Ruby equivalent to a NULL pointer. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Mon Sep 05, 2005 9:07 pm Post subject: (No subject) |
|
|
ah, excellent I'll be trying out the examples. |
|
|
|
|
|
|