新聞中心
java中如何建立一個java樹,請?jiān)斀猓?/h2>
import?java.awt.*;
站在用戶的角度思考問題,與客戶深入溝通,找到瀘水網(wǎng)站設(shè)計(jì)與瀘水網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋瀘水地區(qū)。
import?javax.swing.*;
class?TreeDemo?extends?JFrame
{
public?TreeDemo()
{
setSize(400,300);
setTitle("演示怎樣使用JTree");
show();
JScrollPane?jPanel=new?JScrollPane();
getContentPane().add(jPanel);
JTree?jtree=new?JTree();
jPanel.getViewport().add(jtree,null);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public?class?Example5_25
{
public?static?void?main(String[]?args)
{
TreeDemo?frame=new?TreeDemo();
}
}
其中JScrollPane是一個帶滾動條的面板類。
將對象加入到帶滾動條的面板類中,在將已建的數(shù)放入到其中。
就可建立一個系統(tǒng)默認(rèn)的樹結(jié)構(gòu)。
如何在java構(gòu)造函數(shù)中創(chuàng)建一棵樹
import?java.util.Stack;//導(dǎo)入棧包
public?class?newtree?{
private?newtree?lchild;//?聲明數(shù)據(jù)成員
private?newtree?rchild;
private?char?data;
private?newtree?root;
public?newtree(newtree?l,?newtree?r,?char?data)?{//?有參構(gòu)造函數(shù)進(jìn)行成員賦值
lchild?=?l;
rchild?=?r;
this.data?=?data;
}
public?newtree()?{//?無參構(gòu)造函數(shù)創(chuàng)建樹
newtree?f?=?new?newtree(null,?null,?'f');
newtree?g?=?new?newtree(null,?null,?'g');
newtree?d?=?new?newtree(null,?null,?'d');
newtree?e?=?new?newtree(null,?null,?'e');
newtree?b?=?new?newtree(d,?e,?'b');
newtree?c?=?new?newtree(f,?g,?'c');
newtree?a?=?new?newtree(b,?c,?'a');
this.root=a;
}
public?void?visit(newtree?p)?{/*?輸出數(shù)據(jù)?*/
System.out.print(p.data);//?訪問結(jié)點(diǎn)
}
@SuppressWarnings("unchecked")
public?void?InOrder()?{/*?輸入數(shù)據(jù)?*/
newtree?p=this.root;//你建了一棵樹要把根節(jié)點(diǎn)賦值進(jìn)去啊
Stack?s?=?new?Stack();
while?(p?!=?null?||?!s.isEmpty())?/*?處理數(shù)據(jù):進(jìn)行中序遍歷?*/
{
if?(p?!=?null)?{
s.push(p);
p?=?p.lchild;
}?else?{
p?=?(newtree)?s.pop();
p.visit(p);//this指的是當(dāng)前的類對象
p?=?p.rchild;
}
}
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
newtree?h?=?new?newtree();//?聲明變量,變量賦值
h.InOrder();
}
}
//根據(jù)你的代碼改了一個
import?java.util.Stack;//導(dǎo)入棧包
public?class?newtree?{
public?Tree?createTree()?{//?無參構(gòu)造函數(shù)創(chuàng)建樹
Tree?f?=?new?Tree(null,?null,?'f');
Tree?g?=?new?Tree(null,?null,?'g');
Tree?d?=?new?Tree(null,?null,?'d');
Tree?e?=?new?Tree(null,?null,?'e');
Tree?b?=?new?Tree(d,?e,?'b');
Tree?c?=?new?Tree(f,?g,?'c');
Tree?a?=?new?Tree(b,?c,?'a');
return?a;
}
public?void?InOrder(Tree?p)?{/*?輸入數(shù)據(jù)?*/
StackTree?s?=?new?StackTree();
while?(p?!=?null?||?!s.isEmpty())?{?/*?處理數(shù)據(jù):進(jìn)行中序遍歷?*/
if?(p?!=?null)?{
s.push(p);
p?=?p.lchild;
}?else?{
p?=?s.pop();
System.out.print(p.data);
p?=?p.rchild;
}
}
}
public?void?inOrder1(Tree?p)?{
if?(p?==?null)
return;
inOrder1(p.lchild);
System.out.print(p.data);
inOrder1(p.rchild);
}
public?static?void?main(String[]?args)?{
newtree?h?=?new?newtree();//?聲明變量,變量賦值
h.InOrder(h.createTree());
System.out.println();
h.inOrder1(h.createTree());
}
}
class?Tree?{
Tree?lchild;//?聲明數(shù)據(jù)成員
Tree?rchild;
char?data;
Tree(Tree?lchild,?Tree?rchild,?char?data)?{
this.lchild?=?lchild;
this.rchild?=?rchild;
this.data?=?data;
}
}
用JAVA語言編寫一個種樹的項(xiàng)目
public class Tree {
private int treeId;
private String treeType;// 樹種類型
private int count; //種植數(shù)量
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTreeId() {
return treeId;
}
public void setTreeId(int treeId) {
this.treeId = treeId;
}
public String getTreeType() {
return treeType;
}
public void setTreeType(String treeType) {
this.treeType = treeType;
}
}
public class Address {
private int addCode;//地區(qū)編碼
private String area;//地名
public int getAddCode() {
return addCode;
}
public void setAddCode(int addCode) {
this.addCode = addCode;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
import java.util.HashMap;
import java.util.Map;
public class People {
private int userId;
private String username;
private MapString,MapString,Integer map;
/**
* 傳入地區(qū)和樹種,種樹成功。保存到map中。
* @param address
* @param tree
*/
public void plantingTrees(String address,Tree tree){
Map map = new HashMap();
map.put(tree.getTreeType(),tree.getCount());
this.map.put(address,map);
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public MapString, MapString, Integer getMap() {
return map;
}
public void setMap(MapString, MapString, Integer map) {
this.map = map;
}
}
本文標(biāo)題:java創(chuàng)建樹代碼,java如何構(gòu)造一棵樹
文章位置:http://fisionsoft.com.cn/article/dscjpss.html