java树形结构便利代码,java树形数据结构

java怎么对树形结构进行遍历

java"import java.util.Iterator;

创新互联是一家专业提供恭城企业网站建设,专注与网站设计、成都网站建设、html5、小程序制作等业务。10年已为恭城众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

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

}

}

}

怎样使用java对二叉树进行层次遍历

public class BinaryTree {

int data;      //根节点数据

BinaryTree left;    //左子树

BinaryTree right;   //右子树

public BinaryTree(int data)    //实例化二叉树类

{

this.data = data;

left = null;

right = null;

}

public void insert(BinaryTree root,int data){     //向二叉树中插入子节点

if(dataroot.data)                               //二叉树的左节点都比根节点小

{

if(root.right==null){

root.right = new BinaryTree(data);

}else{

this.insert(root.right, data);

}

}else{                                          //二叉树的右节点都比根节点大

if(root.left==null){

root.left = new BinaryTree(data);

}else{

this.insert(root.left, data);

}

}

}

}

当建立好二叉树类后可以创建二叉树实例,并实现二叉树的先根遍历,中根遍历,后根遍历,代码如下:

package package2;

public class BinaryTreePreorder {

public static void preOrder(BinaryTree root){  //先根遍历

if(root!=null){

System.out.print(root.data+"-");

preOrder(root.left);

preOrder(root.right);

}

}

public static void inOrder(BinaryTree root){     //中根遍历

if(root!=null){

inOrder(root.left);

System.out.print(root.data+"--");

inOrder(root.right);

}

}

public static void postOrder(BinaryTree root){    //后根遍历

if(root!=null){

postOrder(root.left);

postOrder(root.right);

System.out.print(root.data+"---");

}

}

public static void main(String[] str){

int[] array = {12,76,35,22,16,48,90,46,9,40};

BinaryTree root = new BinaryTree(array[0]);   //创建二叉树

for(int i=1;iarray.length;i++){

root.insert(root, array[i]);       //向二叉树中插入数据

}

System.out.println("先根遍历:");

preOrder(root);

System.out.println();

System.out.println("中根遍历:");

inOrder(root);

System.out.println();

System.out.println("后根遍历:");

postOrder(root);

用Java实现一个树形结构,并对其进行遍历

import java.util.Iterator;

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进行简单的二叉树实现,另有遍历,当然遍历是自然顺序。

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

如何用Java实现树形结构啊?

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历

*

* 参考资料0:数据结构(C语言版)严蔚敏

*

* 参考资料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;

/**

* 内部类:节点

*

* @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();

// 将一个数组的值依次转换为Node节点

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

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

}

// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树

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

}

// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理

int lastParentIndex = array.length / 2 - 1;

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1);

// 右孩子,如果数组的长度为奇数才建立右孩子

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

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2);

}

}

/**

* 先序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @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树形结构便利代码,java树形数据结构
链接地址:http://ybzwz.com/article/dscjeih.html