导航:首页 > 编程语言 > 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