真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

樹的java實(shí)現(xiàn)代碼實(shí)現(xiàn) java樹形

用Java實(shí)現(xiàn)一個(gè)樹形結(jié)構(gòu),并對其進(jìn)行遍歷

import?java.util.Iterator;

成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),樅陽網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:樅陽等地區(qū)。樅陽做網(wǎng)站價(jià)格咨詢:18980820575

import?java.util.Random;

import?java.util.TreeSet;

public?class?Demo{

public?static?void?main(String[]?args)?throws?Exception?{

TreeSetInteger?ts?=?new?TreeSetInteger();

for(int?i?=?0;?i??10;?i++){

ts.add(new?Random().nextInt(999));

}

for(IteratorInteger?it?=?ts.iterator();?it.hasNext();){

System.out.println(it.next());

}

}

}

//上面是利用TreeSet進(jìn)行簡單的二叉樹實(shí)現(xiàn),另有遍歷,當(dāng)然遍歷是自然順序。

//如有需要請自行修改吧。

關(guān)于java樹結(jié)構(gòu)的實(shí)現(xiàn)

可以用遞歸模擬樹

要求子樹擁有父樹的id;

絕對原創(chuàng);

import java.util.ArrayList;

import java.util.List;

public class Test2 {

public static void main(String[]args){

ListTree trees = new ArrayListTree();

int id = 1;

Tree t1 = new Tree(0,id++,"我是根樹");

Tree t2 = new Tree(0,id++,"我是第二個(gè)根樹");

Tree t3 = new Tree(1,id++,"我是子樹");

trees.add(t1);

trees.add(t2);

trees.add(t3);

Tree t4 = new Tree(1,id++,"樹根你好");

Tree t5 = new Tree(4,id++,"我不是樹根");

Tree t6 = new Tree(5,id++,"我才是樹根");

trees.add(t4);

trees.add(t5);

trees.add(t6);

show(trees);

}

public static void show(ListTree trees){

for(int i=0;itrees.size();i++){

Tree t = trees.get(i);

if(t.parent == 0){

StringBuffer blank = new StringBuffer();

t.show(trees,blank);

}

}

}

}

import java.util.List;

public class Tree {

public Tree(int parent,int id,String str) {

this.parent = parent;

this.id = id;

this.str = str;

}

int parent;//樹的根樹

int id;

String str;

// StringBuffer blank = new StringBuffer();

void show(ListTree trees, StringBuffer blank){

blank.append(" ");

System.out.println(blank + str );

for(int i=0;itrees.size();i++){

Tree t = trees.get(i);

if(t.parent == id){

t.show(trees,blank);

}

}

}

}

建立一個(gè)二叉樹,附帶查詢代碼,JAVA代碼

import java.util.ArrayList;

// 樹的一個(gè)節(jié)點(diǎn)

class TreeNode {

Object _value = null; // 他的值

TreeNode _parent = null; // 他的父節(jié)點(diǎn),根節(jié)點(diǎn)沒有PARENT

ArrayList _childList = new ArrayList(); // 他的孩子節(jié)點(diǎn)

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 {

// 給出寬度優(yōu)先遍歷的值數(shù)組,構(gòu)建出一棵多叉樹

// null 值表示一個(gè)層次的結(jié)束

// "|" 表示一個(gè)層次中一個(gè)父親節(jié)點(diǎn)的孩子輸入結(jié)束

// 如:給定下面的值數(shù)組:

// { "root", null, "left", "right", null }

// 則構(gòu)建出一個(gè)根節(jié)點(diǎn),帶有兩個(gè)孩子("left","right")的樹

public Tree( Object[] values ){

// 創(chuàng)建根

_root = new TreeNode( values[0], null );

// 創(chuàng)建下面的子節(jié)點(diǎn)

TreeNode currentParent = _root; // 用于待創(chuàng)建節(jié)點(diǎn)的父親

//TreeNode nextParent = null;

int currentChildIndex = 0; // 表示 currentParent 是他的父親的第幾個(gè)兒子

//TreeNode lastNode = null; // 最后一個(gè)創(chuàng)建出來的TreeNode,用于找到他的父親

for ( int i = 2; i values.length; i++ ){

// 如果null ,表示下一個(gè)節(jié)點(diǎn)的父親是當(dāng)前節(jié)點(diǎn)的父親的第一個(gè)孩子節(jié)點(diǎn)

if ( values[i] == null ){

currentParent = (TreeNode)currentParent._childList.get(0);

currentChildIndex = 0;

continue;

}

// 表示一個(gè)父節(jié)點(diǎn)的所有孩子輸入完畢

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;

}

/**

// 按寬度優(yōu)先遍歷,打印出parent子樹所有的節(jié)點(diǎn)

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);// 為了避免葉子節(jié)點(diǎn)也會打印null

//打印 parent 同層的節(jié)點(diǎn)的孩子

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++;

}

}

// 遞歸調(diào)用,打印所有節(jié)點(diǎn)

for ( int i = 0; i parent._childList.size(); i++ ){

TreeNode child = (TreeNode)parent._childList.get(i);

printSteps( child, currentDepth+1 );

}

}

// 按寬度優(yōu)先遍歷,打印出parent子樹所有的節(jié)點(diǎn)

public void printSteps(){

System.out.println(""+_root);

System.out.println(""+null);

printSteps(_root, 1 );

}**/

// 將給定的值做為 parent 的孩子,構(gòu)建節(jié)點(diǎn)

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;

// 添加數(shù)據(jù)

public void add(int data) {

// 遞歸調(diào)用

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);

}

}

// 查詢數(shù)據(jù)

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;

}

}

如何用Java實(shí)現(xiàn)樹形結(jié)構(gòu)啊?

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一個(gè)數(shù)組的值存入二叉樹中,然后進(jìn)行3種方式的遍歷

*

* 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴(yán)蔚敏

*

* 參考資料1:

*

* 參考資料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;

/**

* 內(nèi)部類:節(jié)點(diǎn)

*

* @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 = newData;

}

}

public void createBinTree() {

nodeList = new LinkedListNode();

// 將一個(gè)數(shù)組的值依次轉(zhuǎn)換為Node節(jié)點(diǎn)

for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {

nodeList.add(new Node(array[nodeIndex]));

}

// 對前l(fā)astParentIndex-1個(gè)父節(jié)點(diǎn)按照父節(jié)點(diǎn)與孩子節(jié)點(diǎn)的數(shù)字關(guān)系建立二叉樹

for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {

// 左孩子

nodeList.get(parentIndex).leftChild = nodeList

.get(parentIndex * 2 + 1);

// 右孩子

nodeList.get(parentIndex).rightChild = nodeList

.get(parentIndex * 2 + 2);

}

// 最后一個(gè)父節(jié)點(diǎn):因?yàn)樽詈笠粋€(gè)父節(jié)點(diǎn)可能沒有右孩子,所以單獨(dú)拿出來處理

int lastParentIndex = array.length / 2 - 1;

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1);

// 右孩子,如果數(shù)組的長度為奇數(shù)才建立右孩子

if (array.length % 2 == 1) {

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2);

}

}

/**

* 先序遍歷

*

* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已

*

* @param node

* 遍歷的節(jié)點(diǎn)

*/

public static void preOrderTraverse(Node node) {

if (node == null)

return;

System.out.print(node.data + " ");

preOrderTraverse(node.leftChild);

preOrderTraverse(node.rightChild);

}

/**

* 中序遍歷

*

* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已

*

* @param node

* 遍歷的節(jié)點(diǎn)

*/

public static void inOrderTraverse(Node node) {

if (node == null)

return;

inOrderTraverse(node.leftChild);

System.out.print(node.data + " ");

inOrderTraverse(node.rightChild);

}

/**

* 后序遍歷

*

* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已

*

* @param node

* 遍歷的節(jié)點(diǎn)

*/

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個(gè)索引處的值即為根節(jié)點(diǎn)

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多叉樹問題實(shí)現(xiàn)

TreeNode.java

/*

*?Copyright?Walker?Studio

*?All?Rights?Reserved.

*?

*?文件名稱:?TreeNode.java

*?摘?要:

*?作?者:?Walker

*?創(chuàng)建時(shí)間:?2013-03-19

*/

package?com.walker.commons.data.model;

/**

*?樹節(jié)點(diǎn)

*?

*?@author?Walker

*?@version?1.0.0.0

*/

public?class?TreeNode?

{

/**?節(jié)點(diǎn)Id*/

private?String?nodeId;

/**?父節(jié)點(diǎn)Id*/

private?String?parentId;

/**?文本內(nèi)容*/

private?String?text;

/**

?*?構(gòu)造函數(shù)

?*?

?*?@param?nodeId?節(jié)點(diǎn)Id

?*/

public?TreeNode(String?nodeId)

{

this.nodeId?=?nodeId;

}

/**

?*?構(gòu)造函數(shù)

?*?

?*?@param?nodeId?節(jié)點(diǎn)Id

?*?@param?parentId?父節(jié)點(diǎn)Id

?*/

public?TreeNode(String?nodeId,?String?parentId)

{

this.nodeId?=?nodeId;

this.parentId?=?parentId;

}

public?String?getNodeId()?{

return?nodeId;

}

public?void?setNodeId(String?nodeId)?{

this.nodeId?=?nodeId;

}

public?String?getParentId()?{

return?parentId;

}

public?void?setParentId(String?parentId)?{

this.parentId?=?parentId;

}

public?String?getText()?{

return?text;

}

public?void?setText(String?text)?{

this.text?=?text;

}

}

ManyTreeNode.java

/*

*?Copyright?Walker?Studio

*?All?Rights?Reserved.

*?

*?文件名稱:?ManyTreeNode.java

*?摘?要:

*?作?者:?Walker

*?創(chuàng)建時(shí)間:?2013-03-19

*/

package?com.walker.commons.data.model;

import?java.util.ArrayList;

import?java.util.List;

/**

*?多叉樹節(jié)點(diǎn)

*

*?@author?Walker

*?@verion?1.0.0.0

*/

public?class?ManyTreeNode?

{

/**?樹節(jié)點(diǎn)*/

private?TreeNode?data;

/**?子樹集合*/

private?ListManyTreeNode?childList;

/**

?*?構(gòu)造函數(shù)

?*?

?*?@param?data?樹節(jié)點(diǎn)

?*/

public?ManyTreeNode(TreeNode?data)

{

this.data?=?data;

this.childList?=?new?ArrayListManyTreeNode();

}

/**

?*?構(gòu)造函數(shù)

?*?

?*?@param?data?樹節(jié)點(diǎn)

?*?@param?childList?子樹集合

?*/

public?ManyTreeNode(TreeNode?data,?ListManyTreeNode?childList)

{

this.data?=?data;

this.childList?=?childList;

}

public?TreeNode?getData()?{

return?data;

}

public?void?setData(TreeNode?data)?{

this.data?=?data;

}

public?ListManyTreeNode?getChildList()?{

return?childList;

}

public?void?setChildList(ListManyTreeNode?childList)?{

this.childList?=?childList;

}

}

ManyNodeTree.java

/*

*?Copyright?Walker?Studio

*?All?Rights?Reserved.

*?

*?文件名稱:?ManyNodeTree.java

*?摘?要:

*?作?者:?Walker

*?創(chuàng)建時(shí)間:?2013-03-19

*/

package?com.walker.commons.data.model;

import?java.util.ArrayList;

import?java.util.List;

/**

*?多叉樹生成、遍歷工具

*?

*?@author?Walker

*?@version?1.0.0.0

*/

public?class?ManyNodeTree?

{

/**?樹根*/

private?ManyTreeNode?root;

/**

?*?構(gòu)造函數(shù)

?*/

public?ManyNodeTree()

{

root?=?new?ManyTreeNode(new?TreeNode("root"));

}

/**

?*?生成一顆多叉樹,根節(jié)點(diǎn)為root

?*?

?*?@param?treeNodes?生成多叉樹的節(jié)點(diǎn)集合

?*?@return?ManyNodeTree

?*/

public?ManyNodeTree?createTree(ListTreeNode?treeNodes)

{

if(treeNodes?==?null?||?treeNodes.size()??0)

return?null;

ManyNodeTree?manyNodeTree?=??new?ManyNodeTree();

//將所有節(jié)點(diǎn)添加到多叉樹中

for(TreeNode?treeNode?:?treeNodes)

{

if(treeNode.getParentId().equals("root"))

{

//向根添加一個(gè)節(jié)點(diǎn)

manyNodeTree.getRoot().getChildList().add(new?ManyTreeNode(treeNode));

}

else

{

addChild(manyNodeTree.getRoot(),?treeNode);

}

}

return?manyNodeTree;

}

/**

?*?向指定多叉樹節(jié)點(diǎn)添加子節(jié)點(diǎn)

?*?

?*?@param?manyTreeNode?多叉樹節(jié)點(diǎn)

?*?@param?child?節(jié)點(diǎn)

?*/

public?void?addChild(ManyTreeNode?manyTreeNode,?TreeNode?child)

{

for(ManyTreeNode?item?:?manyTreeNode.getChildList())

{

if(item.getData().getNodeId().equals(child.getParentId()))

{

//找到對應(yīng)的父親

item.getChildList().add(new?ManyTreeNode(child));

break;

}

else

{

if(item.getChildList()?!=?null??item.getChildList().size()??0)

{

addChild(item,?child);

}

}

}

}

/**

?*?遍歷多叉樹?

?*?

?*?@param?manyTreeNode?多叉樹節(jié)點(diǎn)

?*?@return?

?*/

public?String?iteratorTree(ManyTreeNode?manyTreeNode)

{

StringBuilder?buffer?=?new?StringBuilder();

buffer.append("\n");

if(manyTreeNode?!=?null)?

{

for?(ManyTreeNode?index?:?manyTreeNode.getChildList())?

{

buffer.append(index.getData().getNodeId()+?",");

if?(index.getChildList()?!=?null??index.getChildList().size()??0?)?

{

buffer.append(iteratorTree(index));

}

}

}

buffer.append("\n");

return?buffer.toString();

}

public?ManyTreeNode?getRoot()?{

return?root;

}

public?void?setRoot(ManyTreeNode?root)?{

this.root?=?root;

}

public?static?void?main(String[]?args)

{

ListTreeNode?treeNodes?=?new?ArrayListTreeNode();

treeNodes.add(new?TreeNode("系統(tǒng)權(quán)限管理",?"root"));

treeNodes.add(new?TreeNode("用戶管理",?"系統(tǒng)權(quán)限管理"));

treeNodes.add(new?TreeNode("角色管理",?"系統(tǒng)權(quán)限管理"));

treeNodes.add(new?TreeNode("組管理",?"系統(tǒng)權(quán)限管理"));

treeNodes.add(new?TreeNode("用戶菜單管理",?"系統(tǒng)權(quán)限管理"));

treeNodes.add(new?TreeNode("角色菜單管理",?"系統(tǒng)權(quán)限管理"));

treeNodes.add(new?TreeNode("用戶權(quán)限管理",?"系統(tǒng)權(quán)限管理"));

treeNodes.add(new?TreeNode("站內(nèi)信",?"root"));

treeNodes.add(new?TreeNode("寫信",?"站內(nèi)信"));

treeNodes.add(new?TreeNode("收信",?"站內(nèi)信"));

treeNodes.add(new?TreeNode("草稿",?"站內(nèi)信"));

ManyNodeTree?tree?=?new?ManyNodeTree();

System.out.println(tree.iteratorTree(tree.createTree(treeNodes).getRoot()));

}

}


網(wǎng)站名稱:樹的java實(shí)現(xiàn)代碼實(shí)現(xiàn) java樹形
網(wǎng)頁路徑:http://weahome.cn/article/hjpjds.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部