Tôi muốn hiển thị một cây trong giao diện Java, nhưng tôi không biết làm thế nào. Cây đại diện cho một đồ thị các nút được nối, như thế này:Làm thế nào để vẽ một cây đại diện cho một đồ thị của các nút được kết nối?
tôi nên nói rằng tôi có lớp cây của riêng tôi:
public class BinaryTree
{
private BinaryNode root;
public BinaryTree()
{
root = null;
}
public BinaryTree(Object rootItem)
{
root = new BinaryNode(rootItem, null, null);
}
public BinaryTree(Object rootItem,BinaryNode a,BinaryNode b)
{
root = new BinaryNode(rootItem, a, b);
}
public int leavesCount(){
return BinaryNode.leavesCount(root);
}
public boolean equal(BinaryTree a,BinaryTree b){
return BinaryNode.equal(a.root, b.root);
}
public void printPreOrder()
{
if(root != null)
root.printPreOrder();
}
public void printInOrder()
{
if(root != null)
root.printInOrder();
}
public void printPostOrder()
{
if(root != null)
root.printPostOrder();
}
public void makeEmpty()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}
public void merge(Object rootItem, BinaryTree t1, BinaryTree t2) throws MergeAbrot
{
if(t1.root == t2.root && t1.root != null)
{
throw new MergeAbrot("MergeAbrot");
}
root=new BinaryNode(rootItem, t1.root, t2.root);
if(this != t1)
t1.root = null;
if(this != t2)
t2.root = null;
}
public int size()
{
return BinaryNode.size(root);
}
public int height()
{
return BinaryNode.height(root);
}
}
Tôi chỉ muốn vẽ cây. Làm thế nào tôi nên làm gì?
nhưng tôi muốn hiển thị cái cây giống như liên kết này: lcm.csa.iisc.ernet.in/dsa/img151.gif – Oli
Không chắc chắn có thư viện miễn phí để xây dựng cây hình ảnh đó hay không. Bạn luôn có thể tự vẽ nó bằng các công cụ đồ họa cơ bản. –