Binary tree in Eiffel

675 views Asked by At

I am trying to create a program to create a binary tree using recursive methods, but I'm having a problem.

In my class MYNODE, I'm including us left and right, however they can be null (Void). Here is the code, could someone help me?

class
    MYNODE
create
    make
feature
    name: STRING
    left: MYNODE
    right: MYNODE
    setname(n:STRING) do
        name:= n
    end
    setleft(i:MYNODE) do
        left:=i
    end
    setright(i:MYNODE) do
        right:=i
    end
    make do
        create nameme.make (80)
    end
end

And my Main Class:

class
    MAIN
create
    make
feature
    root : MYNODE
    node: MYNODE
    build_tree() do
        io.put_string ("Name: ")
        io.read_line
        node.setname(io.last_string)
        insert(node)
    end
    insert(no,al:MYNODE) do
        if no.name<al.name then
            if no.left = Void then
                no.setleft(al)
            else
                insert(no.left,al)
            end
        else
            if no.right = Void then
                no.setright(al)
            else
                insert(no.right,al)
            end
        end
    end
    make do
        create root.make()
        create node.make()
        build_tree()
    end
end
1

There are 1 answers

1
Emmanuel Stapf On

I'm assuming you are having trouble compiling this code because the attributes are attached and thus need to be initialized before use. In order to allow Void, you need to declare the attributes left and right detachable, that is to say:

left, right: detachable MYNODE