Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C - Possible to create a tree level by level?

Name: Anonymous 2010-05-25 12:39

I'm studying trees right now, and I'd like to find some examples of how to create a binary(or k-ary) tree one level at a time, since I can't find it anywhere and my brain isn't working.

Anyone have any input on this? C's lack of functions doesn't help either. Thanks ahead, /prog/

Name: Anonymous 2010-05-25 13:12

http://webdocs.cs.ualberta.ca/~holte/T26/tree-as-array.html

Create an array long enough to store all the nodes ( 2*(2^depth)-1 or something )

Store first level (the root element) at the 1.st index position.
Store second level at 2 to 3.
Store third level at 4 to 7.
Store fourth level at 8 to 15.
etc.

Traversing the tree in this implementation:
root node index 1
left child index 2*n
right child index 2*n+1

>>``C's lack of functions''
wait what

Name: Anonymous 2010-05-25 13:24

>>2
tree-as-array
Isn't this called a heap? But anyway, it's beside OPs point. AFAICT he wants to know if he can create a tree breadth first, which he can

Name: Anonymous 2010-05-25 13:27

One level at a time?  Keep a running count of the lowest depth of the tree and how many nodes are on that level.  Save the root, it should always be k*x where k is the arbitrary number of leaves any node can have and x is the depth.  If you have added k*x leaves to the given level, reset the counter to zero and then start adding nodes to the next level.

If the method is not of your concern and its only the results in which you are interested, add the new node anywhere, count for the level above the new node, then rebalance the tree if need be.

Name: Anonymous 2010-05-25 14:12

>>2
That's assuming my tree is going to be complete which it isn't, and that I know its length from start, which I don't. I guess I should have mentioned that, sorry.
>>4
Could you elaborate? I think I kind of understand the concept but it's still not very clear to me. Code or pseudo-code example?


Thing is I'm facing an np-complete problem, and I want to build this tree one level at a time, checking if I reach the solution at each pass. I'm doing this because in my case the first solution I find will be the best(looking for the shortest path).

I already determined that a recursive function is probably not what I want for this purpose(could be wrong), but I've never seen an iterative tree creation function(haven't been programming for long), so that's why I'm a little lost.

Name: Anonymous 2010-05-25 14:23

>>5
A lot of people have been asking a similar question lately, and the answer is to read your Xarn.1

______________________________
References
1 http://cairnarvon.rotahall.org/2010/02/28/search-trees-are-easy/

Name: Anonymous 2010-05-25 14:28

>>5
So you you think you want a tree, but you really want to do a breadth-first search?

Name: Anonymous 2010-05-25 14:52

>>7
For a breadth-first search I need a complete tree. My goal was avoiding the complete tree building part, which would be more efficient in my case. Xarn's post doesn't help me with this either, I already know how to build the complete tree and do a breadth-first search.

Name: Anonymous 2010-05-25 15:11

>>8
You're only adding the nodes as you need them, with Xarn's algorithms. You aren't first building the entire tree and then searching breadth-first, that would be retarded.

Name: Anonymous 2010-05-25 15:48

Hello, Xarn. When are you going to realise that Christopher's solution of not allowing comments under your articles but rather using e-mail is superior?

Name: Anonymous 2010-05-25 15:54

>>10
That's great if you want to answer the same question a million times, or, in Christopher's case, want to be able to pretend your blog actually has readers when it doesn't. Not so much when you're interested in actual discussion, though.
The only advantage would be that your pages become perfectly static, which doesn't make up for the downsides, really.

Name: Anonymous 2010-05-25 18:36

>>9
Okay I just realized that, but I can't read python, which leaves me in a shitty position.

Name: Anonymous 2010-05-25 18:47

>>12
Thats like saying you can't read pseudo code

Name: Anonymous 2010-05-25 18:51

>>13
That's saying you can't read pseudo code
FTFY

Name: Anonymous 2010-05-25 19:18

>>12
howdy number 12
welcome to /prog/
lots of people here will tell you to read this 600-page reference book or that massive library of code without offering any help. also, prepare to be ridiculed and ostricized because you're not in the clique. yep, it's exactly like high school - lonely teenagers being jerks to each other. don't mean to get you down, but if you really need programming help, this isn't the place to get it.

Name: Anonymous 2010-05-25 19:22

>>15
DON'T HELP ME

Name: Anonymous 2010-05-25 19:24

>>16
i didn't ask for help, gosling :) and it's not like you erudite nose-thumbers were going to help him anyway.

Name: Anonymous 2010-05-25 19:27

>>15
It's because we care.

Name: unsage 2010-05-25 19:28

>>18
unsage :)
ps. i don't think you do

Name: Anonymous 2010-05-25 19:33

>>14
I'm all for certain kinds of Grammar Fascism, but even I can't help but feel that you were taking the piss with that one.

Name: Anonymous 2010-05-25 19:35

i love how all the clique-kids sage any post that they recognize is not theirs

Name: Anonymous 2010-05-25 20:05

>>21
I think you are misunderstanding the purpose of sage. It is not some sort of thread voting mechanism, it's a way of saying, "don't bump this thread with this post". There are many good reasons not to bump a thread, for example, when they are not directly related to the topic. Are you saying that those posts should of bumped the thread? Is your quality threshold that low.

Name: Anonymous 2010-05-25 21:11

>>21
Honestly, it's a rare post on /prog/ that's even worth reading, and I prefer not to make pretenses about my own. I'm not saging you, I'm saging me. How could it be any other way? Trite Zen notwithstanding, there's bound to be a relevant shiitchan bug. One I would like to use for evil.

Name: Anonymous 2010-05-26 2:14

>>15,21
Butthurt because people make fun of him for not being able to understand capitalization.

Name: Anonymous 2010-06-02 1:31

I got my program running and running super fucking fast. Thanks /prog/, for what it's worth

Name: Anonymous 2010-06-22 6:33

>>21 insinuating that sage is a negative thing?
>>22,23 having to set >>21 straight?
>>24 saying "butthurt"?
OP apparently not knowing how to build a tree with linked lists?

What the fuck is going on here, /prog/?

Name: Anonymous 2010-11-25 15:30

Name: Anonymous 2010-12-10 8:58

Name: Anonymous 2011-02-03 7:26

Name: Anonymous 2013-09-01 14:36



         ト、.,__        /7'iヽ.   八雲 紫  十七歳(自称)
        |:::::! `ヽ、__,,....,,___!::::! l:::|
        ';::::l‐ ''´|::|     /:::/-、|/     ┬ l二l ノ ノ ̄i_,
      / \'、   V   // / `ヽ.   土 l二l イ -‐ァ
     /  、__r-ヽ>' ̄`r‐'"ヽ!-'、、,__   ':,    ノ !_, | _メ、__
     ! r''「>'-‐'"´ ̄ ̄ `ヽ、7_ゝ-、ノ , .i
    r'Y´ / _/l_ ;    ; __!_ `ヽ、/ー、k_    __l_____,!__ -‐ァ  --'--  ノ-┼‐
   rく/  ,' ´/ _」_,ハ   ,ハ_」__` ',  ヽ/ _」_    !  /  --┼‐‐ __'三'__   -┼‐
   「´7   i  ァ'i´ '`!` ',  /' i´ 'ハ`i !  Y、___〉  _,>く..__  、ノ  | 口 」 ____|____
   `イ   ノレ,ハ. '、_,r!  レ'  !,__り.ノ`ハ ハ,.イ                       (自称)
    ノ  )へ/ !"´     .    `"7ノレ' i  `ヽ.
   ´⌒レ〉 ! ヘ.     ._ _,.    ,イ  ',.  '、   `':,
     ,./ ノ   ,!` :.、,       ,.イ /i,. -'‐'"`i `ヽ、  ':,
    ノ,' 〈  r'__,,.イ i`''= '"i. ト、/     i〉ヽ, !く{  i
  ,:'  !   !ヽ(ヘ!:::::'、`ヽ.__,. イ ./:::i    / く」ヽ  i
 /  ノ Y´:::::くンゝ:::::':、/ムヽ/:::;「ト、,____,ハト, レ'  i ハ
 !  〈  i、:;_::::/V':;::::::::Yl+lY::::::/:::!:ヽ、___i_ン7    ノ!〈 i
 ',  .) ハ:::::'':i:::::::::>'::ヽ+l+ン'::/!:::::::::::::!。i   ,:'´ !/_/
  )' (   ';:::::::ヽ;:::::\::::::Y/:/:::::!::::::::::::::::::! / }>!ヘ_」
 くンく{ ヽ. .';::::::::::i`::::::::ヽ:::!':::::::::::::!::::::::::::::::,' .,'  ノ i
    〉 !/i:::::::/::::::::::::::::Yo::::::::::::';:::::::::::::::i iヽ. 〈/
     (l |::::;:'`'':::::::::::::::l;o:::::::::::::ゝ、:;___ノ ,'  '(
       ,ァ'::::::::::::::::::::::;':::::::::::::::::::::::::!/`'(
    _,,..;:'く::::::::::::::::::::::/i::::::::::::::::::::::::::;ゝ,
 r‐''"/ /ヽ、:;______/TL__________;;::イ .ハ
 |::::,:'  ,'    i   !    i   ',   ':,
 |:,'   /     |    ',    :    ':,  ヽ.

Name: Anonymous 2013-09-01 16:09



:::::::::: ::: : : : : :   ___,,,... -‐- 、..,,__
:::: ::: : : : _,,.. - ''"´        _,,...,,_`ヽ、  T7
::: : : : /             `ヽ. `':, Y 'ァ'"´ ハ
   ,:'   ヽ、     ノ__  ト、.,__ノ   ! |ノ  /! |
く\ヘ_ゝ-、_ノ´`iコ二ハ、__ハ_,ヘ.    ,.ム/  . | !    ,,.. --────
 \ハ、_>‐''"´:.;:.:.:.:.:.:.:.:.:.';:.:.:.:.`'' ー''7-、_!__∠___!_/   /
_ノ´Y´/:.:./:./:.:.;':.:.:.i:.:.:.:.:.:.:.!:.:.:.ハ:.:.:.:ヽ.__,.ム、_ハ.、   ,'  あ 特 喘 今
L___,':.:!:.:.:.!:.:!、:;_!_;.:;ハ:.:.!、:_/;;::イ:.!:.:.:!:.:.';.:.:.:.:.キ `ヽ!  |.  げ 別 息 日
..└i:.:.:'、:.:.!.ハ__」__ハ! レ' ァーr-‐ァ'i7:.:.!:.:.:!:.:.:.:.:.!ュノ」   !.  る に. の は
  !:.:.:.:.ヽ:!〈'ヽ,.ハ`  '   i  r;ンi__;ハ__」:.:.:.:.:|/´    |.   わ 見 調
  |ハ___|:,ハ. 。 ゝ' :.:.:.:.:.. `"´,.,.,!:. o :!:.:.:.:.:.i    ∠.     逃 子
 ゼェ ';.:.:.:.! '"'"       u .,'.:.:.:.!:.|:.:.:.;.:;:'     |      し. が
. ,.、  ノ:i:.:..':、    i´ ̄ヽ.   ,/:.:.:.:;':.:!:.:.:.i/| ゼェ   .!      て 悪
( _) ,':.:.!:. ( )`>.、.,` -‐ ',,.イ/:.:.:.:.:.:.:! ,.-'-、     |        い
   i:.: ( ) :.:.:.ノ:.:.:.!`T'´ __,/:.:;':.:/'‐ /   ソ、.    !         か
   !:.::/.:.:.:/ ';!イ `7i´ ,':.:.:/:./!:: / `ーァ'´、 ヽ.  ',       ら
   !:/:.r'" ,.'´ /ヽ/ムレ'i.:.:.;':.:i rノ   /ー-、 ヽ、!,_ ヽ、.,_____________________
[__>「7」 /   ,.-、  |  !:.:.:! ,.:´ __  /  /`ヽrァヘ
 くノ:.:.:!rく_  ,./   'ー-、 [iコ' ! ´ ヽ. i  ./  /`〉 /i
 〈:.:.:.:.:iヽヘ>i/      ノi:.:.:,ハヽ、    ソ- '、_ノ_,.:'イン
  ヽヘ:.」 i7´iヽ、_____,.イi Lハ_」、 i `二´7'' ー-‐' i/
      i'  `'ー-'-- ' |'   ハ !   ` '' ー-‐'Y

Don't change these.
Name: Email:
Entire Thread Thread List