導航:首頁 > 編程語言 > java樹的層次遍歷

java樹的層次遍歷

發布時間:2025-08-11 21:33:22

『壹』 java網狀結構怎麼遍歷

在構建框架時,需要考慮的是展示的邏輯組織以及它的內容,它們如何才能夠和網頁上能夠看到的一些通常的組織結構相匹配,以下是幾種邏輯組織.
1.布告板,一個單獨的簡單的網頁,它通常描述一個人,小的業務或者簡單的產品,大多數的個人網站都是這種類型,它們通常包含一些鏈接,這些鏈接是指向網路上的相關資源的,但是不能向相同文檔內的任何其他網頁.
2.單頁線性,一個網頁或長或短,它被設計成從頭到尾地進行閱讀,通常使用一些規則將這樣一個網頁分解成虛擬的頁,讀者可以翻閱整個網頁,但是也可以使用內容和目標的表格來快速跳至任何部分,這種類型最適合於比較短的文檔,而且這個文檔中所有的信息很自然地從頭到尾過渡.
3.多頁線性,和單頁線性的基本思想相同,但是它被分解成多個邏輯上連續的,一個接一個的網頁,從開頭到結束,就像一管委會故事一樣,通過旋轉在每一頁底部的一個指向下一頁的鏈接來引導讀者遍歷整個系列的網頁.
4.分層,典型的網站結構,一個首頁,包含到其他網頁的鏈接,每一頁包含一個主要的主題區,每一個這樣的網頁又可以包含指向更多網頁的多個鏈接,將主題分解,這樣的結果但是一個樹型的結構.
5.網狀,一個網狀的結構是一個沒有層次的分級的結構,這樣的文檔中有多個網頁,而在其中的任意一個網頁又都包含有連接到其他網頁的鏈接,可能會有一個有頁,但是從那裡進入之後,讀者就可以在此網中來來去去,且不須沿一個特定的路徑,網狀的結構是鬆散並且自由遊走的,最適宜於娛樂,休閑的主題,或者那些難於進行順序或層次分解的主題.

『貳』 寫一個java層次遍歷二叉樹,簡單點就可以,我要的是代碼,不是純文字說明

public class BinaryNode {
Object element;
BinaryNode left;
BinaryNode right;

}

import java.util.*;

public class Queue {

protected LinkedList list;

// Postcondition: this Queue object has been initialized.
public Queue() {

list = new LinkedList();

} // default constructor

// Postcondition: the number of elements in this Queue object has been
// returned.
public int size() {

return list.size();

} // method size

// Postcondition: true has been returned if this Queue object has no
// elements. Otherwise, false has been returned.
public boolean isEmpty() {

return list.isEmpty();

} // method isEmpty

// Postconditon: A of element has been inserted at the back of this
// Queue object. The averageTime (n) is constant and
// worstTime (n) is O (n).
public void enqueue(Object element) {

list.addLast(element);

} // method enqueue

// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: The element that was at the front of this Queue object -
// just before this method was called -- has been removed
// from this Queue object and returned.
public Object dequeue() {

return list.removeFirst();

} // method dequeue

// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: the element at index 0 in this Queue object has been
// returned.
public Object front() {

return list.getFirst();

} // method front

} // Queue class

import java.io.IOException;

public class BinaryTree {
BinaryNode root;

public BinaryTree() {
super();
// TODO 自動生成構造函數存根
root=this.createPre();
}

public BinaryNode createPre()
//按照先序遍歷的輸入方法,建立二叉樹
{
BinaryNode t=null;
char ch;
try {
ch = (char)System.in.read();

if(ch==' ')
t=null;
else
{
t=new BinaryNode();
t.element=(Object)ch;
t.left=createPre();
t.right=createPre();
}
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return t;
}

public void inOrder()
{
this.inOrder(root);
}

public void inOrder(BinaryNode t)
//中序遍歷二叉樹
{
if(t!=null)
{
inOrder(t.left);
System.out.print(t.element);
inOrder(t.right);
}
}

public void postOrder()
{
this.postOrder(root);
}

public void postOrder(BinaryNode t)
//後序遍歷二叉樹
{
if(t!=null)
{
postOrder(t.left);
System.out.print(t.element);
postOrder(t.right);
}
}

public void preOrder()
{
this.preOrder(root);
}
public void preOrder(BinaryNode t)
//前序遍歷二叉樹
{
if(t!=null)
{
System.out.print(t.element);
preOrder(t.left);
preOrder(t.right);
}
}

public void breadthFirst()
{
Queue treeQueue=new Queue();
BinaryNode p;
if(root!=null)
treeQueue.enqueue(root);
while(!treeQueue.isEmpty())
{
System.out.print(((BinaryNode)(treeQueue.front())).element);
p=(BinaryNode)treeQueue.dequeue();
if(p.left!=null)
treeQueue.enqueue(p.left);
if(p.right!=null)
treeQueue.enqueue(p.right);
}
}
}

public class BinaryTreeTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法存根
BinaryTree tree = new BinaryTree();

System.out.println("先序遍歷:");
tree.preOrder();
System.out.println();

System.out.println("中序遍歷:");
tree.inOrder();
System.out.println();

System.out.println("後序遍歷:");
tree.postOrder();
System.out.println();

System.out.println("層次遍歷:");
tree.breadthFirst();
System.out.println();
}

}

閱讀全文

與java樹的層次遍歷相關的資料

熱點內容
cmd命令行通配符 瀏覽:511
什麼app買菜便宜 瀏覽:804
數控電火花線切割機床編程 瀏覽:355
程序員那麼可愛陸漓扮男裝被發現 瀏覽:165
解壓好的刷機包怎麼安裝 瀏覽:632
pdf怎樣轉換為jpg 瀏覽:114
javaphpnetnodejs 瀏覽:179
愛心命令 瀏覽:431
剪影app菱形圖標是什麼 瀏覽:862
75千瓦空氣壓縮機 瀏覽:540
s20u相冊怎麼加密 瀏覽:431
大專畢業4年可以選擇程序員嗎 瀏覽:856
bitlocker加密其他電腦能打開不 瀏覽:709
如何修改steam中dota伺服器 瀏覽:178
並聯單片機 瀏覽:47
linux搜狗五筆 瀏覽:69
qt高級編程pdf 瀏覽:260
三菱人機編程軟體下載 瀏覽:271
java語法錯會在編譯時報錯嗎 瀏覽:349
用電腦編程怎麼編 瀏覽:614