mport java.awt.BorderLayout;
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了施甸免費(fèi)建站歡迎大家使用!
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class Randomtree extends JFrame {
private JTree tree;
public static String[] school = { "初中課程", "高中課程", "大學(xué)課程" };
public static String[] color = { "顏色", "運(yùn)動(dòng)", "食物" };
public static String[] plant = { "植物", "動(dòng)物", "人" };
public static String[][] school2= {
{ "初中一年級(jí)", "初中二年級(jí)", "初中三年級(jí)"}, {"高中一年級(jí)", "高中二年級(jí)",
"高中三年級(jí)"}, {"大學(xué)一年級(jí)", "大學(xué)二年級(jí)", "大學(xué)三年級(jí)", "大學(xué)四年級(jí)"} };
public static String[][] color2 = {
{ "綠色", "白色", "紅色"}, {"足球", "籃球",
"羽毛球"}, {"面包", "牛奶", "披薩", "熱狗"} };
public static String[][] plant2 = {
{ "玫瑰花", "月季花", "海棠花"}, {"豬", "狗",
"貓"}, {"黃種人", "黑種人", "白種人", } };
public static void main(String[] args) {
// TODO 自動(dòng)生成方法存根
new Randomtree();
}
public Randomtree() {
super();
final Random random=new Random();
setVisible(true);
setSize(300,400);
tree = new JTree();
final JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(0, 40));
getContentPane().add(panel, BorderLayout.NORTH);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(300, 350));
getContentPane().add(scrollPane, BorderLayout.CENTER);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int k=random.nextInt(3);
tree=getTree(k);
scrollPane.setViewportView(tree);
}
});
scrollPane.setViewportView(null);
button.setText("隨機(jī)生成樹");
panel.add(button);
pack();
}
protected JTree getTree(int n) {
String[] second=null;
String[][] three=null;
if(n==0){second=school; three=school2;}
if(n==1){second=color; three=color2;}
if(n==2){second=plant; three=plant2;}
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
for(int i=0;isecond.length;i++){
DefaultMutableTreeNode secondNode=new DefaultMutableTreeNode(second[i]);
for (int j=0;jthree[i].length;j++){
DefaultMutableTreeNode threetNode=new DefaultMutableTreeNode(three[i][j]);
secondNode.add(threetNode);
}
root.add(secondNode);
}
JTree tree=new JTree(root);
tree.expandRow(1);
tree.expandRow(5);
tree.expandRow(9);
return tree;
}
}
簡(jiǎn)單的 例子你可以模仿一下
很難嗎?樹結(jié)構(gòu)哦??茨銛?shù)據(jù)結(jié)構(gòu)學(xué)的怎么樣唄。呵呵,我先想到的數(shù)據(jù)結(jié)構(gòu)是雙親孩子表示法,當(dāng)然查找關(guān)系的時(shí)候就要進(jìn)行一些條件設(shè)置,比如祖孫的關(guān)系數(shù)大于父子的關(guān)系數(shù)2,兄弟擁有相同的雙親,堂兄弟的雙親是兄弟,回溯到相同的祖先結(jié)點(diǎn)則有共同的祖先咯。
每一個(gè)節(jié)點(diǎn)有一個(gè)成員變量引用下一個(gè)節(jié)點(diǎn)就行了。
大致實(shí)現(xiàn)了一下單向鏈表?沒有加入異常也沒有仔細(xì)考慮實(shí)現(xiàn)的代碼的效率,可以參考下。
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
public?class?LinkListTest?{
public?static?void?main(String[]?args)?{
LinkListString?ll=new?LinkListString();
ll.add("a");
ll.add("b");
ll.add("c");
ll.add("d");
ll.remove(1);
System.out.println(ll.get(0));
System.out.println(ll.get(1));
System.out.println(ll.get(2));
System.out.println(ll.get(3));
System.out.println(ll.size());
}
}
class?LinkListT{
private?NodeT?frist=null;
private?NodeT?last=null;
private?int?size=0;
public?void?add(T?t){
if(frist==null){
NodeT?node=new?NodeT();
node.setT(t);
size++;
frist=node;
last=node;
}else{????????
NodeT?node=new?NodeT();???????
node.setT(t);
last.setNextNode(node);
size++;
last=node;
}
}
public?T?get(int?i){??????
if(i=0isize){
NodeT?nod=null;
for(int?n=0;n=i;n++){
if(n==0)
nod=frist;
else
nod=nod.getNextNode();?????????????
if(i==n){
return?nod.getT();
}?????????
}?????????
}
return?null;
}?
public?void?remove(int?i){
if(i=0isize){
if(size2){
frist=null;
last=null;
size=0;
}else{
size--;
if(i==0){
frist=frist.getNextNode();????????
}else{??????
NodeT?nod1=null;
NodeT?nod2=null;
for(int?n=0;n=i;n++){
if(n==0){
nod1=frist;
nod2=frist;
}else{
nod2=nod1;
nod1=nod1.getNextNode();??????????????????????
}?
if(i==n){
if(nod1!=null)
nod2.setNextNode(nod1.getNextNode());
else{
nod2.setNextNode(null);
last=nod2;
}
}?????????????
}?
}???
}
}
}
public?int?size(){
return?size;
}
}
class?Node?T{
public?T?getT()?{
return?t;
}
public?void?setT(T?t)?{
this.t?=?t;
}
public?NodeT?getNextNode()?{
return?nextNode;
}
public?void?setNextNode(NodeT?nextNode)?{
this.nextNode?=?nextNode;
}
private?T?t;
private?NodeT?nextNode=null;
}