We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed last year.
Improve this questionWhen I have implemented binary trees, one of the first utilities one writes is a visualization function that given a tree prints it to the screen.
Using cout function to print it every time like a stack is a problem 开发者_如何学编程for me, I can't represent the value of my job.
Printing trees properly in ASCII is quite difficult to understand. Meanwhile, it can't represent your tree and your operations in clear view.
With some research I found a tool named Graphviz – Graph Visualization Software – which uses a language (called DOT) and a set of tools for automatically generating visualizations of graphs. Graphviz is a tool for drawing graphs, not trees, so I can't use it; and implementing the C++ code for this is very difficult.
I'm searching for some code, algorithm or method to show my tree. I'm thinking to use some libraries like GTK, QT, STL or WPF, becase I'm working with Visual Studio C++.
Is it possible to use them? Which is best suited?
A tree is also a graph; you can use Graphviz just fine. And the DOT format is very simple to output. See the Graphviz gallery for examples, including trees.
A very simple solution printing tree in horizontal direction:
5
1
5
9
7
14
Code (Node::print()
function is what matters):
#include<iostream>
using namespace std;
class Tree;
class Node{
public:
Node(int val): _val(val){}
int val(){ return _val; }
void add(Node *temp)
{
if (temp->val() > _val)
{
if (_rchild)
_rchild->add(temp);
else
{
_rchild = temp;
}
}
else
{
if (_lchild)
_lchild->add(temp);
else
{
_lchild = temp;
}
}
}
void print()
{
for (int ix = 0; ix < _level; ++ix) cout << ' ';
cout << _val << endl;
++_level;
if (_lchild)
{
_lchild->print();
--_level;
}
if (_rchild)
{
_rchild->print();
--_level;
}
}
private:
int _val;
Node *_lchild;
Node *_rchild;
static int _level;
};
int Node::_level = 0;
class Tree{
public:
Tree(): _root(0){}
void add(int val)
{
Node *temp = new Node(val);
if (!_root)
_root = temp;
else
_root->add(temp);
}
void print()
{
if (!_root)
return;
_root->print();
}
private:
Node *_root;
};
int main()
{
Tree tree;
tree.add(5);
tree.add(9);
tree.add(1);
tree.add(7);
tree.add(5);
tree.add(14);
tree.print();
}
You can also generate latex code and compile the tex file to get pdf/eps. Check this: http://hstuart.dk/2007/02/21/drawing-trees-in-latex/
For the simplicity of the format, I'm using a combination of the yEd Graph Editor and the Trivial Graph Format.
Check this algorithm here.
Prints ASCII visualization for any binary tree.
0X0
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
0X0 0X0
/ \ / \
/ \ / \
/ \ / \
/ \ / \
/ \ / \
0X0 0X0 0X0 0X0
/ \ / \ / \ / \
/ \ / \ / \ / \
0X0 0X0 0X0 0X0 0X0 0X0 0X0 0X0
First of all, for visualisation of trees, Graphviz is not too difficult to implement if you know how to fill the dot file script into an empty file using C/C++.
Secondly, Graphviz's dot is not only used to represent graphs but also for trees representation (since there is no much difference for graph and tree except cyclicity and interconnection of leaf nodes).
So, if you are inconvenient with DOT, Graphviz also provides other tools like neato, fdp, sfdp, twopi, circo. Select one which best serves your purpose. Explore the more out of Graphviz here. Have a look at this image for how data orientation happens in different layouts.
The other way is just simply coding the tree in the command line, which is quite casual.
精彩评论