   How to draw n-ary trees?
  Hi there :)I want to draw a n-ary tree (a node can have n-childs).The datastructure is a linked list with std::vector:typedef struct node{char szName[30];//String.vector<baum*>child;//Pointer to childs   node   *pre;    // Pointer to his parent.}node;Now iam searching for a algorithm, how to draw such a tree. Is there any way?Greedings from Bremen   
  Draw how?Generally these sort of trees go:class n_tree{    void  function(){   // stuff like drawing   vector<n_tree>   child;   for (child=children.begin(); child!=children.end(); ++child){  child->function();}    }};Simple recursion down the tree.Some more brave/creative/skilled souls make iterators for their trees so they can specify a range to act upon.   ;  Hi :) I already used recursion. My Problem is to get the coordinates, where each node should be drawed. I found this on the web: As a general formula,Yk = k * HEIGHT / (n-1)Xijk = (2*j+1) / (2*mki) * WIDTHkwhereYk = vertical coordinate of the k-th levelHEIGHT = height of the drawing arean = number of vertical levels (that is, depth of the tree)Xi = horizontal coordinate of the actual parent nodeXijk = horizontal coordinate of its j-th child nodeWIDTHk = width of the drawing area for the k-th level (that is, the total width divided be the product of the number of nodes above "k").mki = number of child nodes of the actual parent nodeBut this formula is wrong, now iam searching for another one.   ;  By "draw" do you mean display some kind of representation of the tree contents?I would recommend showing it horizontally, rather than vertically. To output a given subtree, you indent by space proportional to the tree depth, then output the representation of the node, then go to the next line, then recursively output the children. To determine tree depth you can pass it as a parameter in the recursion.By the way, if you're smart enough to use std::vector, you should be smart enough to use std::string as well. Oh, and you should read about the evils of Hungarian notation as it has become.So it would look like:struct node {  string name;  vector<node> children; // actually you don't need to store pointers here  // and it only adds extra complexity for you to manage (unless you need  // polymorphism or the ability to make backreferences or otherwise "share"  // nodes).  node *pre; // pointer to parent. This one should be a pointer, yes :)  // Oh, but there's one thing you'll want to implement in order to handle  // accessing children of a given node - this will give you a nicer interface  // and avoid dealing with pointers too much  node& child(int which) {    return children[which];  }  const node& child(int which) const {    return children[which];  }  node& parent() { return *pre; }  const node& parent() const { return *pre; }  // Yes, there are two separate versions for a reason. See here:  // http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.12}; // In C++ the "typedef struct idiom" is useless and should not be used.// I will implement the drawing function using an operator overload for the node// structure, with a recursive helper function.ostream& operator<<(ostream& os, const node& root) {  printNode(os, root, 0);  return os;}void printNode(ostream& os, const node& root, int depth) {  // indent to the current depth  for (int i = 0; i < depth; ++i) {    os << "  ";  }  // Show the node and go to the next line  os << root.name << endl;  // Recursively show the children, at the next level deeper.  // This is actually quite beautiful because you don't have to handle the  // base case explicitly: if there are no children, then this iteration just  // won't activate.  for (vector<node>::iterator child = root.children.begin();  child != root.children.end();  ++child) {    printNode(os, *child, depth + 1);  }}