组织树java代码 什么是组织java程序中类的一种手段

建立一个二叉树 , 附带查询代码 , JAVA代码import java.util.ArrayList;
// 树的一个节点
class TreeNode {
Object _value = https://www.04ip.com/post/null; // 组织树java代码他的值
TreeNode _parent = null; // 他的父节点组织树java代码,根节点没有PARENT
ArrayList _childList = new ArrayList(); // 他的孩子节点
public TreeNode( Object value, TreeNode parent ){
this._parent = parent;
this._value = https://www.04ip.com/post/value;
}
public TreeNode getParent(){
return _parent;
}
public String toString() {
return _value.toString();
}
}
public class Tree {
// 给出宽度优先遍历的值数组组织树java代码,构建出一棵多叉树
// null 值表示一个层次的结束
// "|" 表示一个层次中一个父亲节点的孩子输入结束
// 如:给定下面的值数组:
// { "root", null, "left", "right", null }
// 则构建出一个根节点组织树java代码,带有两个孩子("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; ivalues.length; i){
// 如果null ,表示下一个节点的父亲是当前节点的父亲的第一个孩子节点
if ( values[i] == null ){
currentParent = (TreeNode)currentParent._childList.get(0);
currentChildIndex = 0;
continue;
}
// 表示一个父节点的所有孩子输入完毕
if ( values[i].equals("|") ){
if ( currentChildIndex 1currentParent._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; iparent._childList.size(); i){
TreeNode child = (TreeNode)parent._childList.get(i);
System.out.println(currentDepth ":" child);
}
if ( parent._childList.size() != 0 )System.out.println("" null);// 为组织树java代码了避免叶子节点也会打印null
//打印 parent 同层的节点的孩子
if ( parent._parent != null ){ // 不是root
int i = 1;
while ( iparent._parent._childList.size() ){// parent 的父亲还有孩子
TreeNode current = (TreeNode)parent._parent._childList.get(i);
printSteps( current, currentDepth );
i;
}
}
// 递归调用,打印所有节点
for ( int i = 0; iparent._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.datadata) {
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 = https://www.04ip.com/post/data;
this.left = left;
this.right = right;
}
}
如何用Java实现树形结构?。?/h2>package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能组织树java代码:把一个数组组织树java代码的值存入二叉树中组织树java代码 , 然后进行3种方式的遍历
*
* 参考资料0:数据结构(C语言版)严蔚敏
*
* 参考资料1组织树java代码:
*
* 参考资料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 内部类:节点
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = https://www.04ip.com/post/newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndexarray.length; nodeIndex) {
nodeList.add(new Node(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndexarray.length / 2 - 1; parentIndex) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 21);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 22);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子组织树java代码,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 21);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 22);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
*遍历的节点
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data" ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍历
*
* 这三种不同的遍历结构都是一样的 , 只是先后顺序不一样而已
*
* @param node
*遍历的节点
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data" ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
*遍历的节点
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data" ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0个索引处的值即为根节点
Node root = nodeList.get(0);
System.out.println("先序遍历:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
postOrderTraverse(root);
}
}
java,jsp实现组织树部门jeesite吧
views/models/sys/officeIndex.jsp
里面的js代码你可以学习学习,直接拿来用就好
求java树形目录代码范例给你一个 。一共三个类 。是个资源管理器的代码
// FileList.java
package tl.exercise.swing;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
public class FileList
extends JList {
【组织树java代码 什么是组织java程序中类的一种手段】 // PathNode theNode;
FileListModel dataModel;
static final long serialVersionUID = 10;
public FileList() {
dataModel = new FileListModel();
setModel(dataModel);
this.setCellRenderer(new MyCellRenderer());
}
public void fireTreeSelectionChanged(I_fileSystem node) {
// Vector files = node.getFiles();
// theNode = node;
dataModel.setNode(node);
updateUI();
}
}
class FileListModel implements ListModel {
FileList theList;
I_fileSystem node;
char fileType = I_fileSystem.ALL;
public void setNode(I_fileSystem node) {
this.node = node;
}
public Object getElementAt(int index) {
if (node != null) {
return ((I_fileSystem) node).getChild(fileType, index);
} else {
return null;
}
}
public int getSize() {
if (node != null) {
return ((I_fileSystem) node).getChildCount(fileType);
} else {
return 0;
}
}
public void addListDataListener(ListDataListener l) {
}
public void removeListDataListener(ListDataListener l) {
}
}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
FolderNode node = (FolderNode) value;
setIcon(node.getIcon());
setText(value.toString());
setBackground(isSelected ? Color.BLUE.darker().darker() : Color.WHITE);
setForeground(isSelected ? Color.WHITE : Color.BLACK);
return this;
}
}
package tl.exercise.swing;
//JExplorer.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.BevelBorder;
public class JExplorer {
public static void main(String[] args) {
// JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new UI(frame));
frame.pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int left = (screen.width - frame.getWidth()) / 2;
int top = (screen.height - frame.getHeight()) / 2;
frame.setLocation(left, top);
frame.setVisible(true);
}
}
class UI extends JPanel {
// implements I_menuHandler{
static final long serialVersionUID = 0l;
static int LEFT_WIDTH = 200;
static int RIGHT_WIDTH = 300;
static int WINDOW_HEIGHT = 300;
JFrame frame = null;
public UI(JFrame frame) {
// EmptyBorder eb = new EmptyBorder(1,1,1,1);
this.frame = frame;
setPreferredSize(new Dimension(800, 600));
setBorder(new BevelBorder(BevelBorder.LOWERED));
setLayout(new BorderLayout());
FileList list = new FileList();
FileTree tree = new FileTree(list);
tree.setDoubleBuffered(true);
list.setDoubleBuffered(true);
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(
new Dimension(LEFT_WIDTH, WINDOW_HEIGHT));
JScrollPane listView = new JScrollPane(list);
listView.setPreferredSize(
new Dimension(RIGHT_WIDTH, WINDOW_HEIGHT));
JSplitPane pane =
new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,
listView);
pane.setDividerLocation(300);
pane.setDividerSize(4);
// pane.setDoubleBuffered(true);
add(pane);
}
}
package tl.exercise.swing;
//FileTree.java
/***********************************************************
*Author: Jason
*email: tl21cen@hotmail.com
* CSDN blog:
***********************************************************/
import java.awt.Component;
import java.io.File;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class FileTree extends JTree {
static final long serialVersionUID = 0;
private FileList theList;
public FileTree(FileList list) {
theList = list;
setModel(new FileSystemModel(new FolderNode()));
this.setCellRenderer(new FolderRenderer());
addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse) {
}
});
this.setSelectionRow(0);
}
public void fireValueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getNewLeadSelectionPath();
Object o = tp.getLastPathComponent();
// theList.fireTreeSelectionChanged((PathNode)o);
theList.fireTreeSelectionChanged((FolderNode) o);
}
public void fireTreeCollapsed(TreePath path) {
super.fireTreeCollapsed(path);
TreePath curpath = getSelectionPath();
if (path.isDescendant(curpath)) {
setSelectionPath(path);
}
}
public void fireTreeWillExpand(TreePath path) {
System.out.println("Path will expand is "path);
}
public void fireTreeWillCollapse(TreePath path) {
System.out.println("Path will collapse is "path);
}
class ExpansionListener implements TreeExpansionListener {
FileTree tree;
public ExpansionListener(FileTree ft) {
tree = ft;
}
public void treeCollapsed(TreeExpansionEvent tee) {
}
public void treeExpanded(TreeExpansionEvent tee) {
}
}
}
class FileSystemModel implements TreeModel {
I_fileSystem theRoot;
char fileType = I_fileSystem.DIRECTORY;
public FileSystemModel(I_fileSystem fs) {
theRoot = fs;
}
public Object getRoot() {
return theRoot;
}
public Object getChild(Object parent, int index) {
return ((I_fileSystem) parent).getChild(fileType, index);
}
public int getChildCount(Object parent) {
return ((I_fileSystem) parent).getChildCount(fileType);
}
public boolean isLeaf(Object node) {
return ((I_fileSystem) node).isLeaf(fileType);
}
public int getIndexOfChild(Object parent, Object child) {
return ((I_fileSystem) parent).getIndexOfChild(fileType, child);
}
public void valueForPathChanged(TreePath path, Object newValue) {
}
public void addTreeModelListener(TreeModelListener l) {
}
public void removeTreeModelListener(TreeModelListener l) {
}
}
interface I_fileSystem {
final public static char DIRECTORY = 'D';
final public static char FILE = 'F';
final public static char ALL = 'A';
public Icon getIcon();
public I_fileSystem getChild(char fileType, int index);
public int getChildCount(char fileType);
public boolean isLeaf(char fileType);
public int getIndexOfChild(char fileType, Object child);
}
/**
* A data model for a JTree. This model explorer windows file system directly.
*
* p
* Perhaps there is a fatal bug with this design. For speed, each of instances
* of this model contains file objects of subdirectory, up to now, there isn't
* any method to release them until program be end. I'm afraid that the memory
* would be full of if the file system is large enough and JVM memery size
* setted too small.
*
* p
* I won't pay more attention to solve it. it isn't goal of current a exercise.
*
* @author Jason
*/
class FolderNode implements I_fileSystem {
// private static FolderNode theRoot;
private static FileSystemView fsView;
private static boolean showHiden = true;;
private File theFile;
private VectorFile all = new VectorFile();
private VectorFile folder = new VectorFile();
/**
* set that whether apply hiden file.
*
* @param ifshow
*/
public void setShowHiden(boolean ifshow) {
showHiden = ifshow;
}
public Icon getIcon() {
return fsView.getSystemIcon(theFile);
}
public String toString() {
// return fsView.
return fsView.getSystemDisplayName(theFile);
}
/**
* create a root node. by default, it should be the DeskTop in window file
* system.
*
*/
public FolderNode() {
fsView = FileSystemView.getFileSystemView();
theFile = fsView.getHomeDirectory();
prepareChildren();
}
private void prepareChildren() {
File[] files = fsView.getFiles(theFile, showHiden);
for (int i = 0; ifiles.length; i) {
all.add(files[i]);
if (files[i].isDirectory()
!files[i].toString().toLowerCase().endsWith(".lnk")) {
folder.add(files[i]);
}
}
}
private FolderNode(File file) {
theFile = file;
prepareChildren();
}
public FolderNode getChild(char fileType, int index) {
if (I_fileSystem.DIRECTORY == fileType) {
return new FolderNode(folder.get(index));
} else if (I_fileSystem.ALL == fileType) {
return new FolderNode(all.get(index));
} else if (I_fileSystem.FILE == fileType) {
return null;
} else {
return null;
}
}
public int getChildCount(char fileType) {
if (I_fileSystem.DIRECTORY == fileType) {
return folder.size();
} else if (I_fileSystem.ALL == fileType) {
return all.size();
} else if (I_fileSystem.FILE == fileType) {
return -1;
} else {
return -1;
}
}
public boolean isLeaf(char fileType) {
if (I_fileSystem.DIRECTORY == fileType) {
return folder.size() == 0;
} else if (I_fileSystem.ALL == fileType) {
return all.size() == 0;
} else if (I_fileSystem.FILE == fileType) {
return true;
} else {
return true;
}
}
public int getIndexOfChild(char fileType, Object child) {
if (child instanceof FolderNode) {
if (I_fileSystem.DIRECTORY == fileType) {
return folder.indexOf(((FolderNode) child).theFile);
} else if (I_fileSystem.ALL == fileType) {
return all.indexOf(((FolderNode) child).theFile);
} else if (I_fileSystem.FILE == fileType) {
return -1;
} else {
return -1;
}
} else {
return -1;
}
}
}
class FolderRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
I_fileSystem node = (I_fileSystem) value;
Icon icon = node.getIcon();
setLeafIcon(icon);
setOpenIcon(icon);
setClosedIcon(icon);
return super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
}
}
用java怎么构造一个二叉树呢?java构造二叉树,可以通过链表来构造,如下代码:
public class BinTree {
public final static int MAX=40;
BinTree []elements = new BinTree[MAX];//层次遍历时保存各个节点
int front;//层次遍历时队首
int rear;//层次遍历时队尾
private Object data; //数据元数
private BinTree left,right; //指向左,右孩子结点的链
public BinTree()
{
}
public BinTree(Object data)
{ //构造有值结点
this.data = https://www.04ip.com/post/data;
left = right = null;
}
public BinTree(Object data,BinTree left,BinTree right)
{ //构造有值结点
this.data = https://www.04ip.com/post/data;
this.left = left;
this.right = right;
}
public String toString()
{
return data.toString();
}
//前序遍历二叉树
public static void preOrder(BinTree parent){
if(parent == null)
return;
System.out.print(parent.data " ");
preOrder(parent.left);
preOrder(parent.right);
}
//中序遍历二叉树
public void inOrder(BinTree parent){
if(parent == null)
return;
inOrder(parent.left);
System.out.print(parent.data " ");
inOrder(parent.right);
}
//后序遍历二叉树
public void postOrder(BinTree parent){
if(parent == null)
return;
postOrder(parent.left);
postOrder(parent.right);
System.out.print(parent.data " ");
}
// 层次遍历二叉树
public void LayerOrder(BinTree parent)
{
elements[0]=parent;
front=0;rear=1;
while(frontrear)
{
try
{
if(elements[front].data!=null)
{
System.out.print(elements[front].data" ");
if(elements[front].left!=null)
elements[rear]=elements[front].left;
if(elements[front].right!=null)
elements[rear]=elements[front].right;
front;
}
}catch(Exception e){break;}
}
}
//返回树的叶节点个数
public int leaves()
{
if(this == null)
return 0;
if(left == nullright == null)
return 1;
return (left == null ? 0 : left.leaves()) (right == null ? 0 : right.leaves());
}
//结果返回树的高度
public int height()
{
int heightOfTree;
if(this == null)
return -1;
int leftHeight = (left == null ? 0 : left.height());
int rightHeight = (right == null ? 0 : right.height());
heightOfTree = leftHeightrightHeight?rightHeight:leftHeight;
return 1heightOfTree;
}
//如果对象不在树中,结果返回-1;否则结果返回该对象在树中所处的层次,规定根节点为第一层
public int level(Object object)
{
int levelInTree;
if(this == null)
return -1;
if(object == data)
return 1;//规定根节点为第一层
int leftLevel = (left == null?-1:left.level(object));
int rightLevel = (right == null?-1:right.level(object));
if(leftLevel0rightLevel0)
return -1;
levelInTree = leftLevelrightLevel?rightLevel:leftLevel;
return 1 levelInTree;
}
//将树中的每个节点的孩子对换位置
public void reflect()
{
if(this == null)
return;
if(left != null)
left.reflect();
if(right != null)
right.reflect();
BinTree temp = left;
left = right;
right = temp;
}
// 将树中的所有节点移走,并输出移走的节点
public void defoliate()
{
if(this == null)
return;
//若本节点是叶节点,则将其移走
if(left==nullright == null)
{
System.out.print(this" ");
data = https://www.04ip.com/post/null;
return;
}
//移走左子树若其存在
if(left!=null){
left.defoliate();
left = null;
}
//移走本节点,放在中间表示中跟移走...
String innerNode= this" ";
data = https://www.04ip.com/post/null;
//移走右子树若其存在
if(right!=null){
right.defoliate();
right = null;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinTree e = new BinTree("E");
BinTree g = new BinTree("G");
BinTree h = new BinTree("H");
BinTree i = new BinTree("I");
BinTree d = new BinTree("D",null,g);
BinTree f = new BinTree("F",h,i);
BinTree b = new BinTree("B",d,e);
BinTree c = new BinTree("C",f,null);
BinTree tree = new BinTree("A",b,c);
System.out.println("前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍历二叉树结果: ");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍历二叉树结果: ");
tree.postOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的层次: " tree.level("F"));
System.out.println("这棵二叉树的高度: " tree.height());
System.out.println("--------------------------------------");
tree.reflect();
System.out.println("交换每个节点的孩子节点后......");
System.out.println("前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍历二叉树结果: ");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍历二叉树结果: ");
tree.postOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的层次: " tree.level("F"));
System.out.println("这棵二叉树的高度: " tree.height());
}
关于组织树java代码和什么是组织java程序中类的一种手段的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读