views:

119

answers:

2

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!

+1  A: 

This is a hard question, since it's so vague. I assume you have a text book or something similiar as part of your course material. Even so, it feels as if many the things you're having problems with are explained by a basic resource, such as the Wikipedia entry on binary trees.

This page describes how to do various tree traversals, and how trees can be represented.

unwind
+1  A: 

If you want to add a node to a tree, you can use a recursive function.

I assume the tree is ordered. So you should get something like this:

AddNode(value, tree)

if tree is empty, create a new tree with value as node and no subtrees.
if value lesser than the treenode, call AddNode on the left branch.
else call AddNode on the right branch. (if duplicates are allowed).

Be sure to update the subtrees if they are changed!

You can convert this to a non recursive function by:

if tree is empty, return a new tree with value as node and no subtrees.
if value is lesser than treenode, and there is no left subtree, create a new left subtree with value as node and no subtrees.
if value is lesser that treenode, and there is a left subtree, try again with the left subtree.
if value is greater or equal than treenode, and there is no right subtree, create a new right subtree with value as node and no subtrees.
if value is greater or equal than treenode, and there is a right subtree, try again with the right subtree.

If the tree needs to be balanced. You need to store get a balance weight (which can be -1, 0 or 1). If you need to add a node on a "heavy" site, you need to reshuffle this. For example if the left side has one node more than the right side and you need to add one more node to the left. You need to get the node with the highest value from the left subtree and promote that to the current top. The previous top is added to the right subtree. Be sure to keep the subtrees balanced too.

Example: add the nodes 0,1,2,3,4

Add(0)           0

Add(1)           0
                  \
                   1

Add(2)           0 (2)  =>      1 (2) =>  1
                  \            /         / \
                   1          0         0   2

Add(3)           1
                / \
               0   2
                    \ 
                     3

Add(4)           1 (4)     => 2 (4)  =>      2
                / \          / \            / \
               0   2        1   3          1   3
                    \      /              /     \
                     3    0              0       4
Gamecat