"scientific" question: Data structure binary tree with objects

Hi,
I just wanted to find out whether it is possible to implement a binary tree data structure in thunkable x, to find out the possibilities to use thunkable in higher classes in schools for computer science courses.

I constructed nodes for the binary tree with objects that have three components: a content, the link to the left subtree the link to the right subtree.

When I give the app a self constructed tree by “when screen starts…” then the output function works well.
So there seems to be no mistake.

But the input function seems not to work.
My idea: Give the intree function a tree and a content as parameters. If the tree is empty then create a new node with content and two empty subtrees.
If not, call the function recursively to the left subtree (if cont is smaller) or to the right subtree.

But after inserting numbers, the always are attached to the right subtree.

Where is my mistake?

https://x.thunkable.com/copy/6c797fe6a09bcafbb5b4570f453871ee

there are 2 bugs here.

  1. the first is a missing initialization of app temp in buttonOut. try clicking buttonOut twice in a row and you’ll see what i mean.

image

  1. the 2nd bug is not yours - i believe createobject cannot insert new objects into an existing structure after it’s built. you can only insert objects when you’re building it, as in your when_screen_opens block.
    you can observe this after putting the fix above. try inserting a number of new nodes, followed by a buttonOut each time - you’ll see they’re really appending to an empty tree everytime.

the combination of these 2 issues made it appear that you were appending to the right all the time!

what a bummer - you can’t use x-thunkable to demonstrate classic computer science concepts.

Thank you @manyone!
The first bug is obvious, but it was only a test version.
But the second seems not to be the answer because I also used list of lists (the note as a list of contents and two lists for the left and right subtree) in a different version of my test.
I think lists can be createt during execution of an app. But it was the same result.
What I think is: For functions we have no parameters of type „call by reference“. They are type „call by value“.
That was the reason why I used a resut (the modified tree). Here can be the problem.

i found out how to code it so it works!


So you can use x-thunkable to teach classic computer science concepts after all!

1 Like

Hi @manyone,
great!
Sometimes you look on your code for hours and do not find the solution. But after seen your solution it is obvious.
Thank you!

Now the app with three types of traversing the binary tree: inorder, preorder and postorder.

Data has to be numbers, for texts only delete the “round” block.

https://x.thunkable.com/copy/c5c0353c3f97ed5140dda1c051635257

1 Like

thanks for sharing! classic!
(can you please double check the order of the left and right nodes in the pre- and post- traversals again? thanks)

Oh, there was a small mistake. I corrected the project. Same link.
I checked the correct output by hand.
Thank you @manyone

1 Like

Someone can correct me if I’m wrong but I believe that any changes to the project require a new share link.

1 Like

I think you’re right

Done:

https://x.thunkable.com/copy/a286746e739b51cd87e6dfb8228ec93c

2 Likes