# TreePlot Vertex Labelling

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

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 :(

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

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


• 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}]] – TraceKira Nov 23 '14 at 14:12
• 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. – Bob Hanlon Nov 23 '14 at 14:39
• I said I want the order kept unchanged , read my question – TraceKira Nov 23 '14 at 18:12

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

The output is

The OP's tree is specified by

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


Code:

ClearAll[addbranch, ramify, twig];

\[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]] :=

tree = {1, {4, 5}, {6, 2}, {8, 3, 7}} /. List -> ramify