樹java實現
① 二叉樹的java實現與幾種遍歷
二叉樹的定義
二叉樹(binary tree)是結點的有限集合,這個集合或者空,或者由一個根及兩個互不相交的稱為這個根的左子樹或右子樹構成.
從定義可以看出,二叉樹包括:1.空樹 2.只有一個根節點 3.只有左子樹 4.只有右子樹 5.左右子樹都存在 有且僅有這5種表現形式
二叉樹的遍歷分為三種:前序遍歷 中序遍歷 後序遍歷
前序遍歷:按照「根左右」,先遍歷根節點,再遍歷左子樹 ,再遍歷右子樹
中序遍歷:按照「左根右「,先遍歷左子樹,再遍歷根節點,最後遍歷右子樹
後續遍歷:按照「左右根」,先遍歷左子樹,再遍歷右子樹,最後遍歷根節點
其中前,後,中指的是每次遍歷時候的根節點被遍歷的順序
具體實現看下圖:
② java 如何實現樹、圖結構
頁面上的話推薦zTree
其他地方就看你需求了。一般情況是遞歸
③ 用Java實現一個樹形結構,並對其進行遍歷
importjava.util.Iterator;
importjava.util.Random;
importjava.util.TreeSet;
publicclassDemo{
publicstaticvoidmain(String[]args)throwsException{
TreeSet<Integer>ts=newTreeSet<Integer>();
for(inti=0;i<10;i++){
ts.add(newRandom().nextInt(999));
}
for(Iterator<Integer>it=ts.iterator();it.hasNext();){
System.out.println(it.next());
}
}
}
//上面是利用TreeSet進行簡單的二叉樹實現,另有遍歷,當然遍歷是自然順序。
//如有需要請自行修改吧。
④ 如何用java代碼實現樹結構
String可以排序的吧..
24個字母是死的吧...
2個字母的就,比了第一個,再比第二個.....
這個思路如何?
⑤ 如何用Java實現樹形結構
光要數據結構、還是要顯示
如果是數據結構,
id name parentId
就可以
~~~~~~~~~
⑥ 如何用java實現二叉樹
import java.util.List;
import java.util.LinkedList;
public class Bintrees {
private int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
private static List<Node> nodeList = null;
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
// 創建二叉樹
public void createBintree() {
nodeList = new LinkedList<Node>();
// 將數組的值轉換為node
for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 對除最後一個父節點按照父節點和孩子節點的數字關系建立二叉樹
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);
}
}
// 前序遍歷
public static void preOrderTraverse(Node node) {
if (node == null) {
return;
}
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
// 中序遍歷
public static void inOrderTraverse(Node node) {
if (node == null) {
return;
}
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
// 後序遍歷
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) {
Bintrees binTree = new Bintrees();
binTree.createBintree();
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);
}
}
輸出結果:
前序遍歷:
1 2 4 8 9 5 3 6 7
中序遍歷:
8 4 9 2 5 1 6 3 7
後序遍歷:
8 9 4 5 2 6 7 3 1
⑦ 關於java樹結構的實現
可以用遞歸模擬樹
要求子樹擁有父樹的id;
絕對原創;
import java.util.ArrayList;
import java.util.List;
public class Test2 {
public static void main(String[]args){
List<Tree> trees = new ArrayList<Tree>();
int id = 1;
Tree t1 = new Tree(0,id++,"我是根樹");
Tree t2 = new Tree(0,id++,"我是第二個根樹");
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(List<Tree> trees){
for(int i=0;i<trees.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(List<Tree> trees, StringBuffer blank){
blank.append(" ");
System.out.println(blank + str );
for(int i=0;i<trees.size();i++){
Tree t = trees.get(i);
if(t.parent == id){
t.show(trees,blank);
}
}
}
}
⑧ 用java實現二叉樹
我有很多個(假設10萬個)數據要保存起來,以後還需要從保存的這些數據中檢索是否存在某
個數據,(我想說出二叉樹的好處,該怎麼說呢?那就是說別人的缺點),假如存在數組中,
那麼,碰巧要找的數字位於99999那個地方,那查找的速度將很慢,因為要從第1個依次往
後取,取出來後進行比較。平衡二叉樹(構建平衡二叉樹需要先排序,我們這里就不作考慮
了)可以很好地解決這個問題,但二叉樹的遍歷(前序,中序,後序)效率要比數組低很多,
public class Node {
public int value;
public Node left;
public Node right;
public void store(intvalue)
right.value=value;
}
else
{
right.store(value);
}
}
}
public boolean find(intvalue)
{
System.out.println("happen" +this.value);
if(value ==this.value)
{
return true;
}
else if(value>this.value)
{
if(right ==null)returnfalse;
return right.find(value);
}else
{
if(left ==null)returnfalse;
return left.find(value);
}
}
public void preList()
{
System.out.print(this.value+ ",");
if(left!=null)left.preList();
if(right!=null) right.preList();
}
public void middleList()
{
if(left!=null)left.preList();
System.out.print(this.value+ ",");
if(right!=null)right.preList();
}
public void afterList()
{
if(left!=null)left.preList();
if(right!=null)right.preList();
System.out.print(this.value+ ",");
}
public static voidmain(String [] args)
{
int [] data =new int[20];
for(inti=0;i<data.length;i++)
{
data[i] = (int)(Math.random()*100)+ 1;
System.out.print(data[i] +",");
}
System.out.println();
Node root = new Node();
root.value = data[0];
for(inti=1;i<data.length;i++)
{
root.store(data[i]);
}
root.find(data[19]);
root.preList();
System.out.println();
root.middleList();
System.out.println();
root.afterList();
}
}
⑨ java如何實現 樹 的模型
可以參考 javax.swing.tree 的思想 給你一點點的提示...
import java.util.*;
/** 樹對象 */
public interface Tree implements Iterable<TreeNode>{
/** 得到樹根 */
TreeNode getRoot();
/** 返回迭代器(比如先根遍歷整顆樹) */
Iterator<TreeNode> iterator();
}
//====
/** 樹節點對象 */
public interface TreeNode {
/** 返回父節點 */
TreeNode getParent();
/** 返回孩子節點的迭代器 */
Iterator<TreeNode> children();
boolean isRoot();
boolean isLeaf();
//...methods
}
⑩ 如何用Java實現樹形結構啊
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一個數組的值存入二叉樹中,然後進行3種方式的遍歷
*
* 參考資料0:數據結構(C語言版)嚴蔚敏
*
* 參考資料1:http://..com/question/81938912.html
*
* 參考資料2:http://cslibrary.stanford.e/110/BinaryTrees.html#java
*
* @author [email protected] @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static List<Node> nodeList = null;
/**
* 內部類:節點
*
* @author [email protected] @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 LinkedList<Node>();
// 將一個數組的值依次轉換為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);
}
}