Bonjour,

je cherche à dessiner un arbre binaire mais ça ne marche pas
qu'est ce qui ne va pas dans mon code ?

Merci de me répondre.






Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
import java.util.*;
 
public class BinaryTree extends java.util.AbstractCollection
{ protected Object root;
  protected BinaryTree left, right, parent;
  protected int size;
 
  public BinaryTree()
  {
  }
 
  public BinaryTree(Object root)
  { this.root = root;
    size = 1;
  }
 
  public BinaryTree(Object root, BinaryTree left, BinaryTree right)
  { this(root);
    if (left != null)
    { this.left = left;
      left.parent = this;
      size += left.size();
    }
    if (right != null)
    { this.right = right;
      right.parent = this;
      size += right.size();
    }
  }
 
  public boolean equals(Object object)
  { if (!(object instanceof BinaryTree)) return false;
    BinaryTree tree = (BinaryTree)object;
    return ( tree.root.equals(root)
          && tree.left.equals(left)
          && tree.right.equals(right)
          && tree.parent.equals(parent)
          && tree.size == size);
  }
 
  public int hashCode()
  { return root.hashCode() + left.hashCode() + right.hashCode() + size;
  }
 
  public java.util.Iterator iterator()
  { return new java.util.Iterator()  // anonymous inner class
    { private boolean rootDone;
      private java.util.Iterator lit, rit;  // child iterators
      public boolean hasNext()
      { return !rootDone || lit != null && lit.hasNext()
                         || rit != null && rit.hasNext();
      }
      public Object next()
      { if (rootDone)
        { if (lit != null && lit.hasNext()) return lit.next();
          if (rit != null && rit.hasNext()) return rit.next();
          return null;
        }
        if (left != null) lit = left.iterator();
        if (right != null) rit = right.iterator();
        rootDone = true;
        return root;
      }
      public void remove()
      { throw new UnsupportedOperationException(); 
      }
    };
  }
  public int size()
  { return size;
  }
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
 
public interface TreeElem {
public String label();
public TreeElem getLeft();
public TreeElem getRight();
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
 
//interface TreeElem{
 
   //public String label();
   //public TreeElem getLeft();
   //public TreeElem getRight();
//}
 
 
 
public class TreeDraw extends JFrame {
 
    private JPanel treedrawing;
 
           TreeDraw (TreeDrawing td) {
               Container pane;
               setTitle ("Display Trees");
               setSize (800,300);
               pane = getContentPane ();
               pane.setLayout(new GridLayout(1,1));
 
           JPanel treedrawing = new JPanel();
           treedrawing.setLayout(new GridLayout(1,1));
           treedrawing.add(td);
           pane.add(treedrawing);
           setVisible(true);
       }
 
       public static void drawTree(TreeElem tree){
           TreeDrawing td = new TreeDrawing(tree);
           JFrame f = new TreeDraw(td);
       }
   }
   class TreeDrawing extends Canvas{
       TreeElem myTree;
       static final int RADIUS   = 10;
 
       TreeDrawing(TreeElem tree){
           myTree = tree;
       }
 
       public void paint(Graphics g){
           drawSubtree(g,myTree,(int)(getWidth()/2),getWidth(),1);
       }
 
       public void drawSubtree(Graphics g,TreeElem tree, int x, int width,int level){
           g.setColor(Color.black);
           g.fillOval(x-RADIUS,(level*20)-RADIUS,RADIUS*2,RADIUS*2);
           g.setColor(Color.gray);
           int w = g.getFontMetrics().stringWidth(tree.label());
           int h = g.getFontMetrics().getHeight();
           g.drawString(tree.label(),x-(int)(w/2),(level*20)+(int)(h/4));
           if (tree.getLeft()!=null){
              g.drawLine(x-RADIUS,level*20,x-(int)(width/4),(level+1)*20);
              drawSubtree(g,tree.getLeft(),x-(int)(width/4),(int)(width/2),level+1);
           }
           if (tree.getRight()!=null){
              g.drawLine(x+RADIUS,level*20,x+(int)(width/4),(level+1)*20);
              drawSubtree(g, tree.getRight(), x+(int)(width/4), (int)(width/2), level+1);
           }
       }
 
 
   }
 
public class Test
{ static public void main(String[] args)
  { BinaryTree e = new BinaryTree("E");
    BinaryTree g = new BinaryTree("G");
    BinaryTree h = new BinaryTree("H");
    BinaryTree i = new BinaryTree("I");
    BinaryTree d = new BinaryTree("D",null,g);
    BinaryTree f = new BinaryTree("F",h,i);
    BinaryTree b = new BinaryTree("B",d,e);
    BinaryTree c = new BinaryTree("C",f,null);
    BinaryTree tree = new BinaryTree("A",b,c);
    System.out.println("tree = " + tree);
    //TreeDraw.drawTree(tree);
    //c'est là que ça ne marche plus
  }
}