Wednesday, 15 September 2010

java - Construct a tree from edges -



java - Construct a tree from edges -

if i'm trying build tree given input 1 border per line, border represented 2 vertices connects. possible build tree using node struct/class or should represent adjacency list graph?

the main problem i'm having order of input. if i'm given 2 or more edges not @ connected @ first, have bunch of node objects no connection whereas you're given tree , inserting tree making new node kid (or parent?) of node.

if worry how store many nodes instead of 1 root node, 1 way have array of n pointers node. assume each node indexed 0 n - 1 n number of nodes in tree, can store i-th node pointer in i-th element of array.

this approach more beneficial if instead of using pointer, you'll allocate array of n nodes @ 1 time in stack (in c/c++). huge improvement in memory allocation time.

java algorithm graph tree

No comments:

Post a Comment