not understanding return statement


(Igor Maljkovic) #1

Hi all, I need help understanding part of my code. I'm learning about data
structures, at the moment binary search tree.
This code does what I want, but I don't understand return statement in
insert
method. Why @root = root works as it works. If i put last line just root,
global variable root is nil instead of tree filled with nodes.
Here is my code:
class BinarySearchTree

class Node
attr_accessor :value, :left_child, :right_child
def initialize(value)
@value = value
@left_child = nil
@right_child = nil
end
end

attr_accessor :root
def initialize()
@root = nil
end

def insert(root = @root, value)
if root == nil
root = Node.new(value)
elsif value <= root.value
root.left_child = insert(root.left_child, value)
else
root.right_child = insert(root.right_child, value)
end
@root = root #works with this return statement
#root <- doesn't work with this statement, why!!!
end
end

x = BinarySearchTree.new()
x.insert(15)
x.insert(10)
p x.root


(Andy Jones) #2

In your method #insert, root is just a variable. Putting `root` as the last statement in #insert will return the value of that variable from the #insert method.

But it won’t set the @root attribute on the class, which is what you are reading when you say `p x.root`. @root is an attribute on the class BinarySearchTree; root is a variable in the insert method. They are not the same thing.

I suspect you need to go back and learn some more about classes and attributes – sorry.

···

From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Igor Maljkovic
Sent: 12 December 2018 10:08
To: ruby-talk@ruby-lang.org
Subject: not understanding return statement

Hi all, I need help understanding part of my code. I'm learning about data structures, at the moment binary search tree.
This code does what I want, but I don't understand return statement in insert
method. Why @root = root works as it works. If i put last line just root,
global variable root is nil instead of tree filled with nodes.
Here is my code:
class BinarySearchTree

  class Node
    attr_accessor :value, :left_child, :right_child
    def initialize(value)
      @value = value
      @left_child = nil
      @right_child = nil
    end
  end

  attr_accessor :root
  def initialize()
    @root = nil
  end

  def insert(root = @root, value)
    if root == nil
      root = Node.new(value)
    elsif value <= root.value
      root.left_child = insert(root.left_child, value)
    else
      root.right_child = insert(root.right_child, value)
    end
    @root = root #works with this return statement
    #root <- doesn't work with this statement, why!!!
  end
end

x = BinarySearchTree.new()
x.insert(15)
x.insert(10)
p x.root

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.


(Ale Miralles) #3

If you want to see a working implementation of binary search trees in Ruby,
you may want to take a look at this:

Also, for other data structures:

Take a look at those posts first, and let me know if you need some help.

~ Ale Miralles.

···

On Wed, 12 Dec 2018 at 08:16, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

In your method #insert, root is just a variable. Putting `root` as the
last statement in #insert will return the value of that variable *from
the #insert method*.

But it won’t set the @root attribute on the class, which is what you are
reading when you say `p x.root`. @root is an attribute on the class
BinarySearchTree; root is a variable in the insert method. They are not the
same thing.

I suspect you need to go back and learn some more about classes and
attributes – sorry.

*From:* ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] *On Behalf Of *Igor
Maljkovic
*Sent:* 12 December 2018 10:08
*To:* ruby-talk@ruby-lang.org
*Subject:* not understanding return statement

Hi all, I need help understanding part of my code. I'm learning about data
structures, at the moment binary search tree.

This code does what I want, but I don't understand return statement in
insert

method. Why @root = root works as it works. If i put last line just root,

global variable root is nil instead of tree filled with nodes.

Here is my code:

class *BinarySearchTree*

  class *Node*

    attr_accessor :value, :left_child, :right_child

    def initialize(*value*)

      @value = value

      @left_child = nil

      @right_child = nil

    end

  end

  attr_accessor :root

  def initialize()

    @root = nil

  end

  def insert(*root* = @root, *value*)

    if root == nil

      root = *Node*.new(value)

    elsif value <= root.value

      root.left_child = insert(root.left_child, value)

    else

      root.right_child = insert(root.right_child, value)

    end

    @root = root #works with this return statement

    #root <- doesn't work with this statement, why!!!

  end

end

x = *BinarySearchTree*.new()

x.insert(15)

x.insert(10)

p x.root

Click here to view Company Information and Confidentiality Notice.
<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data
protection regulations. Please refer to our website to view the ways in
which we handle your data.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


(Ken D'Ambrosio) #4

To be fair, having @root (from class Foo) == foo.root, where "root" in
class Foo does *not*, is somewhat counterintuitive. I think it's a most
excellent thing, but it does lead to confusion among those learning
about attr_* useage.

$.02,

-Ken

···

On 2018-12-12 06:15, Andy Jones wrote:

But it won't set the @root attribute on the class, which is what you are reading when you say `p x.root`. @root is an attribute on the class BinarySearchTree; root is a variable in the insert method. They are not the same thing.


(Alexander) #5

@root is the same as BinarySearchTree.root
In your insert method you are not returning something but assigning the
instance of your BinarySearchTree.root the value of the internal variable
root.

If you want to use a return statement you can just use ‘root’ at the end
but you have to change the line where you call the insert method.

x.root = x.insert(15)

I don’t recommend this but I hope you understand a little better what is
happening in your code.

PS: Read about classes, instances and attributes. Especially attributes.

···

On Wed, Dec 12, 2018 at 13:33 Ale Miralles <amiralles.net@gmail.com> wrote:

If you want to see a working implementation of binary search trees in
Ruby, you may want to take a look at this:

https://medium.com/amiralles/mastering-data-structures-in-ruby-avl-trees-6206bf2035e0

Also, for other data structures:
https://medium.com/amiralles

Take a look at those posts first, and let me know if you need some help.

~ Ale Miralles.

On Wed, 12 Dec 2018 at 08:16, Andy Jones <Andy.Jones@jameshall.co.uk> > wrote:

In your method #insert, root is just a variable. Putting `root` as the
last statement in #insert will return the value of that variable *from
the #insert method*.

But it won’t set the @root attribute on the class, which is what you are
reading when you say `p x.root`. @root is an attribute on the class
BinarySearchTree; root is a variable in the insert method. They are not the
same thing.

I suspect you need to go back and learn some more about classes and
attributes – sorry.

*From:* ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] *On Behalf Of
*Igor Maljkovic
*Sent:* 12 December 2018 10:08
*To:* ruby-talk@ruby-lang.org
*Subject:* not understanding return statement

Hi all, I need help understanding part of my code. I'm learning about
data structures, at the moment binary search tree.

This code does what I want, but I don't understand return statement in
insert

method. Why @root = root works as it works. If i put last line just root,

global variable root is nil instead of tree filled with nodes.

Here is my code:

class *BinarySearchTree*

  class *Node*

    attr_accessor :value, :left_child, :right_child

    def initialize(*value*)

      @value = value

      @left_child = nil

      @right_child = nil

    end

  end

  attr_accessor :root

  def initialize()

    @root = nil

  end

  def insert(*root* = @root, *value*)

    if root == nil

      root = *Node*.new(value)

    elsif value <= root.value

      root.left_child = insert(root.left_child, value)

    else

      root.right_child = insert(root.right_child, value)

    end

    @root = root #works with this return statement

    #root <- doesn't work with this statement, why!!!

  end

end

x = *BinarySearchTree*.new()

x.insert(15)

x.insert(10)

p x.root

Click here to view Company Information and Confidentiality Notice.
<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data
protection regulations. Please refer to our website to view the ways in
which we handle your data.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


(Andy Jones) #6

To be fair, having @root (from class Foo) == foo.root, where "root" in class Foo does *not*, is somewhat counterintuitive. I think it's a most excellent thing, but it does lead to confusion among those learning about attr_* useage.

Honestly, it’s not really counterintuitive unless you expect a variable to magically become an attribute, and frankly if they did, that would be _more_ confusing.

Again: `root` is just a variable. It’s private to the object. `@root` is an attribute, which _can_ be public to the object.

If you have a basic understanding of what a class is and what an attribute is, then this is the easy bit.

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.