2
$\begingroup$

I have plot: TreePlot[{1 -> 4, 1 -> 6, 1 -> 8, 2 -> 6, 3 -> 8, 4 -> 5, 7 -> 8}, VertexLabeling -> True]

Tree plot

Can the labeling on the second level be done as 8, 6 ,4 while keeping the command vertex order unchanged (Background :I rotated the tree on the left and when I add element I want to add from top to bottom and not vice versa)

Ps: I know if I Reverse the order of 1 -> 4, 1 -> 6, 1 -> 8 I get what I want but I don't know how to do it automatically :(

Reverse command could work on the elements above but it returns a list and the TreePlot gives an error :(

$\endgroup$
4
$\begingroup$
gr = {1 -> 4, 1 -> 6, 1 -> 8,
   2 -> 6, 3 -> 8, 4 -> 5, 7 -> 8};

TreePlot[SortBy[gr, -Last[#] &],
 VertexLabeling -> True]

enter image description here

$\endgroup$
3
  • $\begingroup$ It doesn't work properly If I want to change the order of the last two connections : gr = {1 -> 4, {1 -> 6, "wtf"}, 1 -> 8, 2 -> 6, 4 -> 5, 8 -> 7, 8 -> 3}; it seems that I cannot :D The tree looks the same. I solved by using Join[{Root},Reverse{culprit nodes in my order},Reverse[{other culprit nodes in my order}]] $\endgroup$ – TraceKira Nov 23 '14 at 14:12
  • 1
    $\begingroup$ I recommend that you edit your question to clarify when you want the order changed or not and what type of structures you need to deal with. $\endgroup$ – Bob Hanlon Nov 23 '14 at 14:39
  • $\begingroup$ I said I want the order kept unchanged , read my question $\endgroup$ – TraceKira Nov 23 '14 at 18:12
3
$\begingroup$

It sounds likely that the problem would be better addressed by constructing the tree in the proper order than by fixing it afterwords. The basic step should have the form

newtree = Join[{newedges}, {oldtree}]

See Join[{troot -> broot}, branch, tree] in addbranch below.

Example:

The basic data structure in the construction consists of the edges of the tree and a distinguished vertex, the root. It is convenient to carry along the root in the computation. I used the head \[FormalT] for the data structure; one could use List, but it can be convenient to have a different head. One slick initialization has more to do with my example than the OP's problem. The OP does not really specify the problem but only alludes to wanting to add from the top. The form of the input is unspecified and the order of the vertices seems random (the 4-5 edge is in the reverse order of the 6-2 edge, etc.). The initialization is done as needed by twig and ramify. Given uninitialized "leaves", ramify first converts them to trees (leaves). I used a simple form of input in which each branch has the form

{root, branch1, branch2, ...}

where each branch may be another such structure or a simple leaf. For instance {8, 3, 7} represents a tree with root 8 and branches/leaves 3 and 5. If we replace List by ramify, we get ramify[8, 3, 7]. The first step in computing ramify[8, 3, 7] is the initialization step

Mathematica graphics

The output is

Mathematica graphics

The OP's tree is specified by

{1, {4, 5}, {6, 2}, {8, 3, 7}}

Code:

ClearAll[addbranch, ramify, twig];

addbranch[\[FormalT][tree, troot_], \[FormalT][branch_, broot_]] :=
  \[FormalT][Join[{troot -> broot}, branch, tree], troot];

twig[t_\[FormalT]] := t;
twig[t_] := \[FormalT][{}, t];

ramify[root_, leaves__] := 
  ramify[root, Sequence @@ Map[twig, {leaves}]];
ramify[root_, branches__\[FormalT]] := 
  Fold[addbranch, \[FormalT][{}, root], {branches}]

Example:

tree = {1, {4, 5}, {6, 2}, {8, 3, 7}} /. List -> ramify
TreePlot[
 First[tree],
 Left,
 Last[tree],
 VertexLabeling -> True]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.