Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Programming Talk
This forum is for all programming questions.
The questions do not have to be directly related to Ubuntu and any programming language is allowed.

 
Thread Tools Display Modes
Old July 2nd, 2007   #1
Lster
Dark Roasted Ubuntu
 
Join Date: Jun 2006
Beans: 943
Binary Tree To Array (In C)

Hi everyone

I haven't learnt much on binary trees yet, so I decided to try it out. I understand how to make a binary tree out of an existing (sorted) array. I now want to convert back - how would I do that?

I have had no luck with either Ubuntu Forum, Google or Wikipedia searches and would really really appreciate if someone could show me how to do it... This is my start - as you'll see I haven't got very far!

Code:
...
I want to read the values in the binary tree from smallest to largest (ascending) into array named values. Just ask me if you need any other information.

I really appreciate,
Lster

Last edited by Lster; July 5th, 2007 at 03:24 PM..
Lster is offline   Reply With Quote
Old July 2nd, 2007   #2
Quikee
Ubuntu Extra Shot
 
Quikee's Avatar
 
Join Date: Apr 2006
Location: Slovenia
Beans: 356
Ubuntu Karmic Koala (testing)
Re: Binary Tree To Array (In C)

I don't want to spoil all the coding fun so I will only tell you the general idea in pseudo code:

Code:
void traverse(nodeNumber)

  if (tree[nodeNumber].left != -1 ) 
    traverse(tree[nodeNumber].left) /* recursively go to left because on the left all values are smaller  then current value*/

  addValueToArray(node[nodeNumber].value) /* when we deal with smaller numbers add current value to the array */

  if (tree[nodeNumber].right != -1 ) 
    traverse(tree[nodeNumber].right) /* the rest are bigger then current value */
Quikee is offline   Reply With Quote
Old July 2nd, 2007   #3
hod139
Fresh Brewed Ubuntu
 
Join Date: Jul 2005
Beans: 1,537
Ubuntu 8.04 Hardy Heron
Send a message via AIM to hod139
Re: Binary Tree To Array (In C)

It looks like you are interested in the Binary Heap data structure. In the underlying array representation of the tree, the root is at index 1 (index 0 is not used). If a parent node has index i, then the left child is at index 2*i and the right child is at index 2*i+1. For example, since the root has index 1, the left child of the root is at index 2 (2*1) and the right child is at index 3 (2*1+1).
__________________
When I invented the Web, I didn't have to ask anyone's permission.
~Tim Berners-Lee on Net Neutrality
-------------------------------------
Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).
hod139 is offline   Reply With Quote
Old July 2nd, 2007   #4
Lster
Dark Roasted Ubuntu
 
Join Date: Jun 2006
Beans: 943
Re: Binary Tree To Array (In C)

Thank you loads, Quikee - that was all I need! Ubuntu Forums rock!

@ hod139:

I don't think I understand you - I'm interested, if you will explain again... As far as I can see, my way works - what does that method have over it?

Cheers,
Lster
Lster is offline   Reply With Quote
Old July 2nd, 2007   #5
hod139
Fresh Brewed Ubuntu
 
Join Date: Jul 2005
Beans: 1,537
Ubuntu 8.04 Hardy Heron
Send a message via AIM to hod139
Re: Binary Tree To Array (In C)

A binary heap is an array organized in such a way that you can perform tree like operations on it. It has some properties that your randomly created array representation does not. For example, you can create a binary heap in O(n) time. Removing the max element is O(1). All the leaf nodes are at indexes greater than n/2, etc.
__________________
When I invented the Web, I didn't have to ask anyone's permission.
~Tim Berners-Lee on Net Neutrality
-------------------------------------
Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).
hod139 is offline   Reply With Quote
Old July 3rd, 2007   #6
Lux Perpetua
Dipped in Ubuntu
 
Join Date: Aug 2005
Beans: 522
Ubuntu 7.04 Feisty Fawn
Re: Binary Tree To Array (In C)

I think Lster wanted a sorted array, not a heap.
Lux Perpetua is offline   Reply With Quote
Old July 3rd, 2007   #7
hod139
Fresh Brewed Ubuntu
 
Join Date: Jul 2005
Beans: 1,537
Ubuntu 8.04 Hardy Heron
Send a message via AIM to hod139
Re: Binary Tree To Array (In C)

Quote:
Originally Posted by Lux Perpetua View Post
I think Lster wanted a sorted array, not a heap.
I think you are correct and I really misunderstood his original question.
__________________
When I invented the Web, I didn't have to ask anyone's permission.
~Tim Berners-Lee on Net Neutrality
-------------------------------------
Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).
hod139 is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:37 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry