
正文
java中序遍历代码 java树前序遍历 中序遍历 后序遍历
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java 先、中、后序遍历方法的实现
前pre(root)
{ if(root==null)return null;
visit(root);pre(root.left);pre(root.right);
}
中in(root)
{ if(root==null)return null;
in(root.left);visit(root);in(root.right);
}
后post(root)
{ if(root==null)return null;
post(root.left);post(root.right);visit(root);
}
相关问答
Q1: java Map 怎么遍历
java Map 遍历一般有四种方式
方式一: 这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
方式二: 在for-each循环中遍历keys或values。
如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
该方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。
方式三:使用Iterator遍历
使用泛型:
不使用泛型:
你也可以在keySet和values上应用同样的方法。
方法四: 通过键找值遍历(效率低)
作为方法一的替代,这个代码看上去更加干净;但实际上它相当慢且无效率。
因为从键取值是耗时的操作(与方法一相比,在不同的Map实现中该方法慢了20%~200%)。如果安装了FindBugs,它会做出检查并警告你关于哪些是低效率的遍历。所以尽量避免使用。
总结:
如果仅需要键(keys)或值(values)使用方法二。
如果所使用的语言版本低于java 5,或是打算在遍历时删除entries,必须使用方法三。
否则使用方法一(键值都要)。
扩展资料:
类似的遍历算法:
二叉树的遍历算法
1、先(根)序遍历的递归算法定义:
若二叉树非空,则依次执行如下操作:
⑴ 访问根结点;
⑵ 遍历左子树;
⑶ 遍历右子树。
2、中(根)序遍历的递归算法定义:
若二叉树非空,则依次执行如下操作:
⑴遍历左子树;
⑵访问根结点;
⑶遍历右子树。
3、后(根)序遍历得递归算法定义:
若二叉树非空,则依次执行如下操作:
⑴遍历左子树;
⑵遍历右子树;
⑶访问根结点。
参考资料:百度百科——Java
Q2: JAVA 2差数 代码 有人知道怎么写么
你说的是二叉树吧·····
/**
* 二叉树测试二叉树顺序存储在treeLine中,递归前序创建二叉树。另外还有能
* 够前序、中序、后序、按层遍历二叉树的方法以及一个返回遍历结果asString的
* 方法。
*/
public class BitTree {
public static Node2 root;
public static String asString;
//事先存入的数组,符号#表示二叉树结束。
public static final char[] treeLine = {'a','b','c','d','e','f','g',' ',' ','j',' ',' ','i','#'};
//用于标志二叉树节点在数组中的存储位置,以便在创建二叉树时能够找到节点对应的数据。
static int index;
//构造函数
public BitTree() {
System.out.print("测试二叉树的顺序表示为:");
System.out.println(treeLine);
this.index = 0;
root = this.setup(root);
}
//创建二叉树的递归程序
private Node2 setup(Node2 current) {
if (index = treeLine.length) return current;
if (treeLine[index] == '#') return current;
if (treeLine[index] == ' ') return current;
current = new Node2(treeLine[index]);
index = index * 2 + 1;
current.left = setup(current.left);
index ++;
current.right = setup(current.right);
index = index / 2 - 1;
return current;
}
//二叉树是否为空。
public boolean isEmpty() {
if (root == null) return true;
return false;
}
//返回遍历二叉树所得到的字符串。
public String toString(int type) {
if (type == 0) {
asString = "前序遍历:\t";
this.front(root);
}
if (type == 1) {
asString = "中序遍历:\t";
this.middle(root);
}
if (type == 2) {
asString = "后序遍历:\t";
this.rear(root);
}
if (type == 3) {
asString = "按层遍历:\t";
this.level(root);
}
return asString;
}
//前序遍历二叉树的循环算法,每到一个结点先输出,再压栈,然后访问它的左子树,
//出栈,访问其右子树,然后该次循环结束。
private void front(Node2 current) {
StackL stack = new StackL((Object)current);
do {
if (current == null) {
current = (Node2)stack.pop();
current = current.right;
} else {
asString += current.ch;
current = current.left;
}
if (!(current == null)) stack.push((Object)current);
} while (!(stack.isEmpty()));
}
//中序遍历二叉树
private void middle(Node2 current) {
if (current == null) return;
middle(current.left);
asString += current.ch;
middle(current.right);
}
//后序遍历二叉树的递归算法
private void rear(Node2 current) {
if (current == null) return;
rear(current.left);
rear(current.right);
asString += current.ch;
}
}
/**
* 二叉树所使用的节点类。包括一个值域两个链域
*/
public class Node2 {
char ch;
Node2 left;
Node2 right;
//构造函数
public Node2(char c) {
this.ch = c;
this.left = null;
this.right = null;
}
//设置节点的值
public void setChar(char c) {
this.ch = c;
}
//返回节点的值
public char getChar() {
return ch;
}
//设置节点的左孩子
public void setLeft(Node2 left) {
this.left = left;
}
//设置节点的右孩子
public void setRight (Node2 right) {
this.right = right;
}
//如果是叶节点返回true
public boolean isLeaf() {
if ((this.left == null) (this.right == null)) return true;
return false;
}
}
一个作业题,里面有你要的东西。
主函数自己写吧。当然其它地方也有要改的。
Q3: 怎么用Java编写简单的程序,遍历c盘里所有的文件
这个可以使用递归来实现,具体代码如下:
import java.io.File;
public class Demo {
public static void main(String[] args) {
File file = new File("C:\\");// 指定文件目录
method(file);
}
public static void method(File file) {
File[] fs = file.listFiles();// 得到File数组
if(fs!=null) {// 判断fs是否为null
for(File f : fs) {
if(f.isFile()) {// 如果是文件直接输出
System.out.println(f.getName());
} else {
method(f);// 否则递归调用
}
}
}
}
}
Q4: java中输入100个数据再排序后输出的代码
既然在学数据结构,那就用二叉树排序吧,一个根节点,比根节点小的排左边,比根节点大的排右边,以此类推,形成二叉树,再遍历输出即可。这个例子掌握了,你就多学了一种数据结构
class BinaryTree
{
class Node
{
private int data; //保存数据内容
private Node left; //左子树
private Node right;//右子树
public Node(int data){
this.data = data;
}
public void addNode(Node newNode){ //addNode方法用来添加数据
if(newNode.data = this.data){
if(this.left == null){ //左子树为空
this.left = newNode;
}else{
this.left.addNode(newNode);//继续向下判断
}
}
if(newNode.data this.data){
if(this.right == null){ //右子树为空
this.right = newNode;
}else{
this.right.addNode(newNode);//继续向下判断
}
}
} //addNode方法结束
public void printNode(){ //采用中序遍历(左-根-右)
if(this.left != null){
this.left.printNode();
}
System.out.println(this.data); //找到根内容
if(this.right != null){
this.right.printNode();
}
}
} //Node类结束
private Node root; //根节点
public void add(int data){
Node newNode = new Node(data);
if(this.root == null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
}
public void print(){
this.root.printNode();
}
}
public class Demo
{
public static void main(String args[]){
BinaryTree bt = new BinaryTree();
bt.add(3);
bt.add(4);
bt.add(0);
bt.add(1);//可添加100个数据,也可通过args[]手动输入,需略做调整
bt.print();
}
}
关于java中序遍历代码和java树前序遍历 中序遍历 后序遍历的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







