import java.util.ArrayList;
从网站建设到定制行业解决方案,为提供网站设计、成都做网站服务体系,各种行业企业客户提供网站建设解决方案,助力业务快速发展。创新互联将不断加快创新步伐,提供优质的建站服务。
// 树的一个节点
class TreeNode {
Object _value = null; // 他的值
TreeNode _parent = null; // 他的父节点,根节点没有PARENT
ArrayList _childList = new ArrayList(); // 他的孩子节点
public TreeNode( Object value, TreeNode parent ){
this._parent = parent;
this._value = value;
}
public TreeNode getParent(){
return _parent;
}
public String toString() {
return _value.toString();
}
}
public class Tree {
// 给出宽度优先遍历的值数组,构建出一棵多叉树
// null 值表示一个层次的结束
// "|" 表示一个层次中一个父亲节点的孩子输入结束
// 如:给定下面的值数组:
// { "root", null, "left", "right", null }
// 则构建出一个根节点,带有两个孩子("left","right")的树
public Tree( Object[] values ){
// 创建根
_root = new TreeNode( values[0], null );
// 创建下面的子节点
TreeNode currentParent = _root; // 用于待创建节点的父亲
//TreeNode nextParent = null;
int currentChildIndex = 0; // 表示 currentParent 是他的父亲的第几个儿子
//TreeNode lastNode = null; // 最后一个创建出来的TreeNode,用于找到他的父亲
for ( int i = 2; i values.length; i++ ){
// 如果null ,表示下一个节点的父亲是当前节点的父亲的第一个孩子节点
if ( values[i] == null ){
currentParent = (TreeNode)currentParent._childList.get(0);
currentChildIndex = 0;
continue;
}
// 表示一个父节点的所有孩子输入完毕
if ( values[i].equals("|") ){
if ( currentChildIndex+1 currentParent._childList.size() ){
currentChildIndex++;
currentParent = (TreeNode)currentParent._parent._childList.get(currentChildIndex);
}
continue;
}
TreeNode child = createChildNode( currentParent, values[i] );
}
}
TreeNode _root = null;
public TreeNode getRoot(){
return _root;
}
/**
// 按宽度优先遍历,打印出parent子树所有的节点
private void printSteps( TreeNode parent, int currentDepth ){
for ( int i = 0; i parent._childList.size(); i++ ){
TreeNode child = (TreeNode)parent._childList.get(i);
System.out.println(currentDepth+":"+child);
}
if ( parent._childList.size() != 0 ) System.out.println(""+null);// 为了避免叶子节点也会打印null
//打印 parent 同层的节点的孩子
if ( parent._parent != null ){ // 不是root
int i = 1;
while ( i parent._parent._childList.size() ){// parent 的父亲还有孩子
TreeNode current = (TreeNode)parent._parent._childList.get(i);
printSteps( current, currentDepth );
i++;
}
}
// 递归调用,打印所有节点
for ( int i = 0; i parent._childList.size(); i++ ){
TreeNode child = (TreeNode)parent._childList.get(i);
printSteps( child, currentDepth+1 );
}
}
// 按宽度优先遍历,打印出parent子树所有的节点
public void printSteps(){
System.out.println(""+_root);
System.out.println(""+null);
printSteps(_root, 1 );
}**/
// 将给定的值做为 parent 的孩子,构建节点
private TreeNode createChildNode( TreeNode parent, Object value ){
TreeNode child = new TreeNode( value , parent );
parent._childList.add( child );
return child;
}
public static void main(String[] args) {
Tree tree = new Tree( new Object[]{ "root", null,
"left", "right", null,
"l1","l2","l3", "|", "r1","r2",null } );
//tree.printSteps();
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(0) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(1) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(2) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(0) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(1) );
}
}
java:二叉树添加和查询方法
package arrays.myArray;
public class BinaryTree {
private Node root;
// 添加数据
public void add(int data) {
// 递归调用
if (null == root)
root = new Node(data, null, null);
else
addTree(root, data);
}
private void addTree(Node rootNode, int data) {
// 添加到左边
if (rootNode.data data) {
if (rootNode.left == null)
rootNode.left = new Node(data, null, null);
else
addTree(rootNode.left, data);
} else {
// 添加到右边
if (rootNode.right == null)
rootNode.right = new Node(data, null, null);
else
addTree(rootNode.right, data);
}
}
// 查询数据
public void show() {
showTree(root);
}
private void showTree(Node node) {
if (node.left != null) {
showTree(node.left);
}
System.out.println(node.data);
if (node.right != null) {
showTree(node.right);
}
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
你好,递归调用简单的说就是自己无限的调用自己的下一级,也就是说父节点和子节点拥有相同的属性和方法,所以在处理玩当前节点也就是父节点的时候,就调用处理自身下面的子节点,类似于文件夹和文件,文件夹相对于当前目录来说是父节点,而他里面的子文件夹就相当于子节点,但是处理子文件夹的时候,子文件夹就成了父节点,一次递归处理完毕
希望可以帮助到你
//先选中节点才能增加节点
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TreeTest implements ActionListener,TreeModelListener{
JLabel label=null;
JTree tree=null;
DefaultTreeModel treeModel=null;
String nodeName=null;//原有节点名称
public TreeTest(){
JFrame f=new JFrame("TreeTest");
Container contentPane=f.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器");
tree=new JTree(root);
tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
treeModel=(DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);
JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(tree);
JPanel panel=new JPanel();
JButton b=new JButton("新增节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("删除节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("清除所有节点");
b.addActionListener(this);
panel.add(b);
label=new JLabel("Action");
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//本方法运行新增、删除、清除所有节点的程序代码.
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("新增节点")){
DefaultMutableTreeNode parentNode=null;
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");
newNode.setAllowsChildren(true);
TreePath parentPath=tree.getSelectionPath();
//取得新节点的父节点
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());
//由DefaultTreeModel的insertNodeInto()方法增加新节点
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());
//tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点
//会被 包在文件夹中,你必须自行展开文件夹才看得到。
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
label.setText("新增节点成功");
}
if (ae.getActionCommand().equals("删除节点")){
TreePath treepath=tree.getSelectionPath();
if (treepath!=null){
//下面两行取得选取节点的父节点.
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();
TreeNode parent=(TreeNode)selectionNode.getParent();
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点。
treeModel.removeNodeFromParent(selectionNode);
label.setText("删除节点成功");
}
}
}
if (ae.getActionCommand().equals("清除所有节点")){
//下面一行,由DefaultTreeModel的getRoot()方法取得根节点.
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();
//下面一行删除所有子节点.
rootNode.removeAllChildren();
//删除完后务必运行DefaultTreeModel的reload()操作,整个Tree的节点才会真正被删除.
treeModel.reload();
label.setText("清除所有节点成功");
}
}
public void treeNodesChanged(TreeModelEvent e){
TreePath treePath=e.getTreePath();
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
try{
int[] index=e.getChildIndices();
node=(DefaultMutableTreeNode)node.getChildAt(index[0]);
}catch(NullPointerException exc){}
label.setText(nodeName+"更改数据为:"+(String)node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e){
System.out.println("new node insered");
}
public void treeNodesRemoved(TreeModelEvent e){
System.out.println("node deleted");
}
public void treeStructureChanged(TreeModelEvent e){
System.out.println("Structrue changed");
}
public static void main(String[] args){
new TreeTest();
}
class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)e.getSource();
int rowLocation=tree.getRowForLocation(e.getX(),e.getY());
TreePath treepath=tree.getPathForRow(rowLocation);
TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
nodeName=treenode.toString();
}catch(NullPointerException ne){}
}
}
}
我看了一下,知道lz的错误在哪了。
createbintree方法中。以lz的讲的先输入a、再输入三回车,也就是当前只有一个节点a。下面就以楼主的数据来讲一下函数所走流程
1.可是当楼主输入a之后,按完第一个回车
当前取得的数据a,走else分支,接下来就到myTree.data = str;//为节点赋值,假设为父节点1
myTree.lchild = new BintNode();//为myTree建立了左节点
createbintree(myTree.lchild);
//递归调用跳到2
myTree.rchild = new BintNode();//创建右节点
createbintree(myTree.rchild);
//递归调用跳到3
2.输入第二个回车,数据为空,那么走if分支,myTree=null,照理说应该将myTree该节点赋值为空,也就代表没有该节点。
其实这个想法是错误的。在1中,为父节点1分配了左孩子,已经是new出来的对象。不知道lz对函数调用中的形参(也就是函数名所带的参数)的值,是怎么理解。有两种形式的
1)值传递:即使是值传递,在函数体中,改变的也只是一个临时变量的值。并不会影响到实参的值
2)引用传递:对象间的传递,而这传递只是引用关系的传递。就是将该参数指向该类,如果在函数体中设为null,也只是将形参与实际对象间的引用关系给去掉
本程序中,是属于引用传递,在createbintree将myTree=null,也只是断掉myTree与外部对象的关系而已,即父节点1的左孩子间的关系,所以父节点1的左孩子不为null
3.与2同样的解释,也可知道右孩子也不为空。
那么在调用num来计算叶子的个数时候,是不是根结点一开始进来,左右孩子都不为null,所以自然最后一个else。那么往下,lz再计算一下就知道结果为2
建议修改的话:
在createbintree方法中,if ("".equals(str.trim()))为空时,则myTree.data=null
然后在num方法中,计算个数的话,利用myTree.data==null return 0 ; myTree.lchild.data==null myTree.rchild.data==null return 1 ; else 一样
//获得商品分类树形结构
private JTree getTree(){
if(tree ==null)
{
tree = new JTree(getTreeModel());
tree.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
// tree = new JTable(getModel());
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
// 节点选择事件
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
treeSelection();
}
});
tree.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON3)
{
int selRow = tree.getRowForLocation(e.getX(), e.getY());
tree.setSelectionRow(selRow);
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
// myRightClick(selRow, selPath);
selectedNode = (DefaultMutableTreeNode) selPath.getLastPathComponent();
showPopMenu(selRow,selPath,e.getX(),e.getY());
}
}
}
}
});
}
return tree;
}
/**
* 获得当前选择的商品对象
* @return
*
*/
private Goods getSelectedGoods(){
int row = table.getSelectedRow();
if(row!=-1)
{
String code = (String)getModel().getValueAt(row,0);
Goods g= gHandle.getGoodsByCode(code);
return g;
}
return null;
}
/**
* 树形结构上显示右键菜单
* @param selRow
* @param selPath
*
*/
private void showPopMenu(int selRow, TreePath path,int x,int y) {
// tree.add(getPopMenu());
if (path == null)
return;
//点的是根结点
if(selectedNode.isRoot())
{
JPopupMenu pop = getPopMenu(1,path,selRow);
// System.out.println(selRow);
this.getContentPane().add(pop);
pop.show(tree,x,y);
}
//叶结点
if(selectedNode.isLeaf())
{
JPopupMenu pop = getPopMenu(0,path,selRow);
this.getContentPane().add(pop);
pop.show(tree,x,y);
}
}
/**
* 创建右键菜单
* @param action
* @return
*
*/
private JPopupMenu getPopMenu(int action,final TreePath path,final int selRow){
if(pop==null)
{
pop = new JPopupMenu();
//新建类别事件处理
addItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String value = JOptionPane.showInputDialog("请输入新类别的名称:");
if(value==null||value.trim().length()==0)
return;
GoodsCategoryHandle gcHandle = new GoodsCategoryHandle();
if(gcHandle.isExsited(value))
{
JOptionPane.showMessageDialog(null, "该类别名称已经存在!", "提示", JOptionPane.ERROR_MESSAGE);
return ;
}
DefaultMutableTreeNode newNode =new DefaultMutableTreeNode(value);
treeModel.insertNodeInto(newNode,(MutableTreeNode)treeModel.getRoot(),selRow-1);
GoodsCategory gc = new GoodsCategory();
gc.setCategoryName(value);
//更新到数据库中
gcHandle.addCategory(gc);
}
});
//重命名类别名称事件处理
renameItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String value = JOptionPane.showInputDialog(null,"请输入新类别的名称:",selectedNode.toString());
if(value==null||value.trim().length()==0)
return;
GoodsCategoryHandle gcHandle = new GoodsCategoryHandle();
if(gcHandle.isExsited(value))
{
JOptionPane.showMessageDialog(null, "该类别名称已经存在!", "提示", JOptionPane.ERROR_MESSAGE);
return ;
}
GoodsCategory gc =gcHandle.getCategory(selectedNode.toString());
gc.setCategoryName(value);
//更新到数据库中
gcHandle.modifyCategory(gc);
selectedNode.setUserObject(value);
tree.setModel(getTreeModel());
}
});
//移除结点事件处理
delItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i =JOptionPane.showConfirmDialog(null,
"慎重!删除类别后,该类别下的所有商品将全部被删除,确认后继续", "提示", JOptionPane.YES_NO_OPTION);
if(i==JOptionPane.YES_OPTION)
{
GoodsCategoryHandle gcHandle = new GoodsCategoryHandle();
GoodsHandle gHandle = new GoodsHandle();
int id = (gcHandle.getCategory(selectedNode.toString().trim())).getCategoryId();
ArrayList al = gHandle.getGoodsByCategory(id);
boolean b_bak = false;
//对删除商品的备分
try {
FileWriter out = new FileWriter("/"+CommonUtil.getTime()+"商品删除bak.db",true);
out.write("商品名称"+" "+"商品分类"+" "+
"商品别名"+" "+"商品单位"+" "+
"商品厂商"+" "+"商品库存上限"+" "+
"商品库存下限"+" "+
"商品库存下限"+" "+
"商品参考售价"+" "+
"商品实际库存");
out.write('\n');
for(int j=0;jal.size();j++)
{
Goods g = (Goods)al.get(j);
out.write(g.toString());
out.write('\n');
}
out.close();
b_bak=true;
} catch (IOException e1) {
e1.printStackTrace();
b_bak = false;
}
gcHandle.setCommit(false);
boolean g_del=gHandle.deleteByCategoryId(id);
boolean gc_del=gcHandle.deleteById(id);
//只有类别删除成功,该类别下的商品删除成功,并且备分成功后才提交删除
if(g_delgc_delb_bak)
gcHandle.commit();
else
{
// System.out.println("fail");
gcHandle.rollBack();
JOptionPane.showMessageDialog(null, "删除失败,可能是因为该类别下的商品存在销售记录,不能被删除!", "提示", JOptionPane.ERROR_MESSAGE);
}
tree.setModel(getTreeModel());
tree.setSelectionRow(0);
}
else
return;
}
});
pop.add(addItem);
pop.add(delItem);
pop.add(renameItem);
}
if(action ==1)
{
delItem.setEnabled(false);
renameItem.setEnabled(false);
}
if(action ==0)
{
delItem.setEnabled(true);
renameItem.setEnabled(true);
}
return pop;
}
/**
* 用户在商品分类树上的事件处理
*
*
*/
private void treeSelection(){
TreePath path = tree.getSelectionPath();
if (path == null)
return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
.getLastPathComponent();
selectedCategory = cHandle.getCategory(node.toString());
// 若选中的是根节点
if (node.isRoot())
showAllGoods();
if(node.isLeaf()){
model.setRowCount(0);
ArrayList al = gHandle.getGoodsByCategoryName(node
.toString());
for (int i = 0; i al.size(); i++) {
Goods g = (Goods) al.get(i);
model.addRow(getRow(g));
}
}
}
public GoodsCategory getCategory(){
return this.selectedCategory;
}
/**
* 生产树形列表的数据模型
*
* @return
*
*/
private TreeModel getTreeModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(
PropertiesUtil.getString("categoryTree.all"));
ArrayList categoryList = cHandle.getAllCategory();
// 添加子节点
for (int i = 0; i categoryList.size(); i++) {
GoodsCategory gc = (GoodsCategory) categoryList.get(i);
String name = gc.getCategoryName();
DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
root.add(node);
}
// 生产treeModel
treeModel = new DefaultTreeModel(root);
return treeModel;
}