I'm doing a unit called Data Structures and Algorithms. We've just started and my professor has just taught us the basics of what Algebraic Semantics is and what Axioms are etc. Till now, I've just used Trees in the form of arrays. Not using the signature for pre-ordered tree as tree(value, tree, tree) where value is the value in the node, left node is the first tree and right node is the second tree.
Now that i'm defining my tree as either tree(value, tree, tree) or Nil, I can't figure out how to go about defining the algebra for addNode(value, tree).
It just gets more and more complicated with each level, plus, I can't possibly think of anyway to scan through one level as once, been trying since like an hour now. The algebra just branches out into more and more if-elses as we go down the tree. Am I doing it right? Can you point me in the right direction? Or trees cannot be implemented as tree(value, tree, tree)?
It's a part of my tutorial(not worth any marks, in the additional questions), but I'm not looking for an instant answer, I like the subject, and would like to learn more.
Edit 1: I checked out Wikipedia, and I don't want to use the textbook for the clear answer, I'm just looking for a hint towards the right direction, whether my approach is right or it's completely impossible to define a tree as tree(value, tree, tree). I know you can represent a tree ADT in form a list. But I wanted to really think about it. Hope it makes sense. Thanks a lot guys!
Edit 2: Uhmm, it's hard to explain over the internet. Let's say I'm defining a new data structure called "tree". I can define it any way I want, and it must behave like a balanced binary tree(though, values of parents and children dont matter) So I define it as tree : tree(value, tree, tree) OR nil It's not programming code, it's just how I define it. Tree is a value + 2 other trees or Tree is nil. Now addNode(value, tree) adds a node to tree while still keeping it balanced. It's not programming code, it's just algebraic semantics. I don't know if I can explain it properly. But I got to a solution that I can achieve using Queues or Stacks, but that's another ADT I'll have to define, which is not valid.
Edit 3: Seems like I had assumed many things that made the problem harder than it really was supposed to be. First of all, from the little explanation I gave, Gamecat's answer was perfect. But I agree with the comments, it's perfectly valid to include other ADTs. In fact, when we build anything that uses an Int, we're using an ADT for that structure. I thought each ADT had to be unique. Anyways, thanks a lot guys for your answers!