This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zestix
Zestix wrote ...

2020/3/20

Help with recursion

Zestix Zestix

2020/3/20

#
Hello, I've been trying to use a recursive method to decode morse, and I consistently get some sort of error message. The last time I got a StackOverFlow error.
private String getLetter(String pCode, BinaryTree<String> pTree)
    {
        int loop1 = 0;
        int loop2 = 0;
        BinaryTree<String> tree = morsetree;
        if (!pCode.isEmpty())
        {
            if (loop2 <= pCode.length())
            {
                while (pCode.substring(loop, loop+1) == " ")
                {
                    loop2++;
                }
            }
            if (loop2 > 5)
            {
                return null;
            }
            for (int i=0;i<loop2;i++)
            {
                if (pCode.substring(i, i+1) == "-")
                {
                    tree = tree.getRightTree();
                }
                else
                {
                    tree = tree.getLeftTree();
                }
            }
            System.out.println(tree.getContent());
            return getLetter(pCode.substring(loop2, pCode.length())
                                                           ,morsetree);
        }
        return null;
    }
danpost danpost

2020/3/21

#
Lines 6 and 34 (and 17) do not seem to be consistent with each other. That is -- null is not the same as isEmpty.
danpost danpost

2020/3/21

#
Where can I find the BinaryTree class?
Zestix Zestix

2020/3/21

#
danpost wrote...
Where can I find the BinaryTree class?
It's quite a bit of coding
/**
 * Beschreiben Sie hier die Klasse List.
 * 
 * @author (Ihr Name) 
 * @version (eine Versionsnummer oder ein Datum)
 */
public class List<Zahl>
{
    int rep = 0;
    int help = 0;
    boolean sorted = false;
    ListNode p;
    public class ListNode
    {
        ListNode next;
        private ListNode first;
        private ListNode last;
        private ListNode current;
        public Zahl content;
        public ListNode(Zahl pContent)
        {
            content = pContent;
        }

        public void setNext(ListNode pNode)
        {
            next = pNode;
        }

        public ListNode getNext()
        {
            return next;
        }

        public Zahl getContent()
        {
            return content;
        }

        public void setFirst(ListNode pNode)
        {
            first = pNode;
        }

        public void setLast(ListNode pNode)
        {
            last = pNode;
        }
    }
    private ListNode first;
    private ListNode last;
    private ListNode current;
    public List()
    {
    } 

    public void toFirst()
    {
        current = first;
    }

    public void toLast()
    {
        current = last;
    }


    public void next()
    {
        toFirst();
        if (!isEmpty())
        {
            if (current.getNext() == null || current == null)
            {
                toFirst();
            }
            else
            {
                current = current.getNext();
            }
            if (!hasAccess())
            {
                toFirst();
            }
            else if (hasAccess())
            {
            }
        }
    }

    public Zahl getContent()
    {
        if (!isEmpty())
        {
            if (hasAccess())
            {
                return current.content;
            }
        }
        return null;
    }

    public boolean isEmpty()
    {
        if (first == null)
        {
            return true;
        }
        return false;
    }

    public boolean hasAccess()
    {
        if (current != null)
        {
            return true;
        }
        return false;
    }

    public void fcol()
    {
        if (!hasAccess())
        {
            toFirst();
        }
    }

    public void append(Zahl pContent)
    {
        ListNode pNode = new ListNode(pContent);
        if (!isEmpty())
        {
            last.setNext(pNode);
            last = pNode;
        }
        else
        {
            first = pNode;
            last = pNode;
            current = pNode;
        }
    }

    public void setContent(Zahl pContent)
    {
        Zahl smth = getContent();
        smth = pContent;
    }
    
    public void test()
    {
        System.out.println("AHHHHHHHHHHH");
    }

    public void concat (List<Zahl> pList)
    {
        if (pList.hasAccess())
        {
            if (pList != null)
            {
                while (!pList.isEmpty())
                {
                    pList.toFirst();
                    append(pList.getContent());
                    pList.remove();
                }
            }
        }
    }

    public void aid()
    {
        ListNode aid;
        aid = current;
        toFirst();
        while (hasAccess())
        {
            System.out.println(""+current.getContent());
            next();
        }
    }

    public void swap()
    {
        ListNode p;
        p = current;
        current = current.getNext();
        next();
        current = p;
    }

    public void remove()
    {
        ListNode Maverick;
        Maverick = current;
        if (hasAccess())
        {
            toFirst();
            if (current == Maverick)
            {
                first = null;
                if (current.getNext() == null)
                {
                    last = null;
                    current = null;
                }
                current = null;
            }
            else if (current.getNext() == Maverick)
            {
                first = last;
                current = last;
            }
            else
            {
                while (current.getNext() != Maverick)
                {
                    if (current.getNext() == null)
                    {
                        toFirst();
                    }
                    next();
                }
                Maverick = current;
                next();
                next();
                Maverick.setNext(current);
            }
        }
    }

    public ListNode getCurrent()
    {
        return current;
    }

    public void insert(Zahl pContent)
    {
        ListNode Maverick;
        Maverick = current;
        ListNode pNode = new ListNode(pContent);
        if (!hasAccess())
        {
            if (first == null)
            {
                first = pNode;
                last = pNode;
                current = pNode;
            }
        }
        if (hasAccess())
        {
            toFirst();
            if (current == Maverick)
            {
                pNode.setNext(first);
                first = pNode;
            }
            else
            {
                while (current.getNext() != Maverick)
                {
                    if (current.getNext() == null)
                    {
                        toFirst();
                    }
                    next();
                }
                pNode.setNext(Maverick);
                current.setNext(pNode);
                Maverick = current;
                next();
                next();
                Maverick.setNext(current);
            }
        }
    }
}
Zestix Zestix

2020/3/21

#
You can generally disregard the last 200 lines, though
danpost danpost

2020/3/21

#
Zestix wrote...
It's quite a bit of coding << Code Omitted >>
That looks to be a List class, not a BinaryTree class.
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
It's quite a bit of coding << Code Omitted >>
That looks to be a List class, not a BinaryTree class.
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
Zestix Zestix

2020/3/22

#
Zestix wrote...
danpost wrote...
Zestix wrote...
It's quite a bit of coding << Code Omitted >>
That looks to be a List class, not a BinaryTree class.
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
BinaryTree<String> left = new BinaryTree<String>("h");
        BinaryTree<String> right = new BinaryTree<String>("v");
        BinaryTree<String> tree1 = new BinaryTree<String>("s",left,right);
        left = new BinaryTree<String>("f");
        right = new BinaryTree<String>("ü");
        BinaryTree<String> tree2 = new BinaryTree<String>("u",left,right);
        left = new BinaryTree<String>("l");
        right = new BinaryTree<String>("ä");
        BinaryTree<String> tree3 = new BinaryTree<String>("r",left,right);
        left = new BinaryTree<String>("p");
        right = new BinaryTree<String>("j");
        BinaryTree<String> tree4 = new BinaryTree<String>("w",left,right);
        left = new BinaryTree<String>("b");
        right = new BinaryTree<String>("x");
        BinaryTree<String> tree5 = new BinaryTree<String>("d",left,right);
        left = new BinaryTree<String>("c");
        right = new BinaryTree<String>("y");
        BinaryTree<String> tree6 = new BinaryTree<String>("k",left,right);
        left = new BinaryTree<String>("z");
        right = new BinaryTree<String>("q");
        BinaryTree<String> tree7 = new BinaryTree<String>("g",left,right);
        left = new BinaryTree<String>("ö");
        right = new BinaryTree<String>("ch");
        BinaryTree<String> tree8 = new BinaryTree<String>("o",left,right);
        tree1 = new BinaryTree<String>("i",tree1,tree2);
        tree2 = new BinaryTree<String>("a",tree3,tree4);
        tree3 = new BinaryTree<String>("n",tree5,tree6);
        tree4 = new BinaryTree<String>("m",tree7,tree8);
        tree1 = new BinaryTree<String>("e",tree1,tree2);
        tree2 = new BinaryTree<String>("t",tree3,tree4);
        morsetree = new BinaryTree<String>("#",tree1,tree2);
Here's the morsetree itself
danpost danpost

2020/3/22

#
Zestix wrote...
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
Yes -- but you are using BinaryTree as a class name. Where is that class?
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
Yes -- but you are using BinaryTree as a class name. Where is that class?
What I sent was the BinaryTree class. The List class was just renamed to that - it has the same methods and coding
danpost danpost

2020/3/22

#
Zestix wrote...
What I sent was the BinaryTree class. The List class was just renamed to that - it has the same methods and coding
Then, where is the constructor with a String parameter -- with the signature: List(String) or the other one: List(String, List, List) ?
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
What I sent was the BinaryTree class. The List class was just renamed to that - it has the same methods and coding
Then, where is the constructor with a String parameter -- with the signature: List(String) or the other one: List(String, List, List) ?
It's in line 7. I had been using Zahl for a different project, which is why it's there. It's alright, though, I'm currently inept. Thanks for trying to help me despite the trouble, I'll pick up the project some other day. Take care
danpost danpost

2020/3/22

#
Zestix wrote...
It's in line 7. I had been using Zahl for a different project, which is why it's there.
Line 7 is a class header line -- not a constructor header.
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
It's in line 7. I had been using Zahl for a different project, which is why it's there.
Line 7 is a class header line -- not a constructor header.
Pardon, it's in line 20. No need to help, though
danpost danpost

2020/3/22

#
Zestix wrote...
Pardon, it's in line 20. No need to help, though
Oh. So, ListNode is the BinaryTree class -- not List. I still do not see a 2nd constructor, however. Good luck.
You need to login to post a reply.