- 浏览: 443630 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
g_man1990:
update config 不成功啊
build-helper-maven-plugin 配置多 source resource 文件 -
netwelfare:
文章很详细,就是太长了,读起来有点困难,倒不如写精练点,像这篇 ...
Java 基本类型 -
huyuancai1010:
function commitForm() {
var ...
加时间戳或者随机数去除js缓存 -
Smile__:
不过这个东西以前还真没研究过 。
hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size -
Smile__:
想不到你也是北大青鸟的 。哈
hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size
Binary.java
import java.util.Stack;
public class BinaryTree {
protected Node root;
public BinaryTree(Node root) {
this.root = root;
}
public Node getRoot() {
return root;
}
/** 构造树 */
public static Node init() {
Node a = new Node('A');
Node b = new Node('B', null, a);
Node c = new Node('C');
Node d = new Node('D', b, c);
Node e = new Node('E');
Node f = new Node('F', e, null);
Node g = new Node('G', null, f);
Node h = new Node('H', d, g);
return h;// root
}
/** 访问节点 */
public static void visit(Node p) {
System.out.print(p.getKey() + " ");
}
/** 递归实现前序遍历 */
protected static void preorder(Node p) {
if (p != null) {
visit(p);
preorder(p.getLeft());
preorder(p.getRight());
}
}
/** 递归实现中序遍历 */
protected static void inorder(Node p) {
if (p != null) {
inorder(p.getLeft());
visit(p);
inorder(p.getRight());
}
}
/** 递归实现后序遍历 */
protected static void postorder(Node p) {
if (p != null) {
postorder(p.getLeft());
postorder(p.getRight());
visit(p);
}
}
/** 非递归实现前序遍历 */
protected static void iterativePreorder(Node p) {
Stack<Node> stack = new Stack<Node>();
if (p != null) {
stack.push(p);
while (!stack.empty()) {
p = stack.pop();
visit(p);
if (p.getRight() != null)
stack.push(p.getRight());
if (p.getLeft() != null)
stack.push(p.getLeft());
}
}
}
/** 非递归实现前序遍历2 */
protected static void iterativePreorder2(Node p) {
Stack<Node> stack = new Stack<Node>();
Node node = p;
while (node != null || stack.size() > 0) {
while (node != null) {//压入所有的左节点,压入前访问它
visit(node);
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {//
node = stack.pop();
node = node.getRight();
}
}
}
/** 非递归实现后序遍历 */
protected static void iterativePostorder(Node p) {
Node q = p;
Stack<Node> stack = new Stack<Node>();
while (p != null) {
// 左子树入栈
for (; p.getLeft() != null; p = p.getLeft())
stack.push(p);
// 当前节点无右子或右子已经输出
while (p != null && (p.getRight() == null || p.getRight() == q)) {
visit(p);
q = p;// 记录上一个已输出节点
if (stack.empty())
return;
p = stack.pop();
}
// 处理右子
stack.push(p);
p = p.getRight();
}
}
/** 非递归实现后序遍历 双栈法 */
protected static void iterativePostorder2(Node p) {
Stack<Node> lstack = new Stack<Node>();
Stack<Node> rstack = new Stack<Node>();
Node node = p, right;
do {
while (node != null) {
right = node.getRight();
lstack.push(node);
rstack.push(right);
node = node.getLeft();
}
node = lstack.pop();
right = rstack.pop();
if (right == null) {
visit(node);
} else {
lstack.push(node);
rstack.push(null);
}
node = right;
} while (lstack.size() > 0 || rstack.size() > 0);
}
/** 非递归实现后序遍历 单栈法*/
protected static void iterativePostorder3(Node p) {
Stack<Node> stack = new Stack<Node>();
Node node = p, prev = p;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
Node temp = stack.peek().getRight();
if (temp == null || temp == prev) {
node = stack.pop();
visit(node);
prev = node;
node = null;
} else {
node = temp;
}
}
}
}
/** 非递归实现后序遍历4 双栈法*/
protected static void iterativePostorder4(Node p) {
Stack<Node> stack = new Stack<Node>();
Stack<Node> temp = new Stack<Node>();
Node node = p;
while (node != null || stack.size() > 0) {
while (node != null) {
temp.push(node);
stack.push(node);
node = node.getRight();
}
if (stack.size() > 0) {
node = stack.pop();
node = node.getLeft();
}
}
while (temp.size() > 0) {
node = temp.pop();
visit(node);
}
}
/** 非递归实现中序遍历 */
protected static void iterativeInorder(Node p) {
Stack<Node> stack = new Stack<Node>();
while (p != null) {
while (p != null) {
if (p.getRight() != null)
stack.push(p.getRight());// 当前节点右子入栈
stack.push(p);// 当前节点入栈
p = p.getLeft();
}
p = stack.pop();
while (!stack.empty() && p.getRight() == null) {
visit(p);
p = stack.pop();
}
visit(p);
if (!stack.empty())
p = stack.pop();
else
p = null;
}
}
/** 非递归实现中序遍历2 */
protected static void iterativeInorder2(Node p) {
Stack<Node> stack = new Stack<Node>();
Node node = p;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
node = stack.pop();
visit(node);
node = node.getRight();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
BinaryTree tree = new BinaryTree(init());
System.out.print(" Pre-Order:");
preorder(tree.getRoot());
System.out.println();
System.out.print(" In-Order:");
inorder(tree.getRoot());
System.out.println();
System.out.print("Post-Order:");
postorder(tree.getRoot());
System.out.println();
System.out.print(" Pre-Order:");
iterativePreorder(tree.getRoot());
System.out.println();
System.out.print("Pre-Order2:");
iterativePreorder2(tree.getRoot());
System.out.println();
System.out.print(" In-Order:");
iterativeInorder(tree.getRoot());
System.out.println();
System.out.print(" In-Order2:");
iterativeInorder2(tree.getRoot());
System.out.println();
System.out.print(" Post-Order:");
iterativePostorder(tree.getRoot());
System.out.println();
System.out.print("Post-Order2:");
iterativePostorder2(tree.getRoot());
System.out.println();
System.out.print("Post-Order3:");
iterativePostorder3(tree.getRoot());
System.out.println();
System.out.print("Post-Order4:");
iterativePostorder4(tree.getRoot());
System.out.println();
}
}
Node.java
public class Node {
private char key;
private Node left, right;
public Node(char key) {
this(key, null, null);
}
public Node(char key, Node left, Node right) {
this.key = key;
this.left = left;
this.right = right;
}
public char getKey() {
return key;
}
public void setKey(char key) {
this.key = key;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
发表评论
-
StringBuStringffer 和 StringBuilder 区别
2010-02-05 17:37 1388String 字符串常量 StringBuffer 字符串变量 ... -
Java timezone
2010-01-13 20:57 1679转自:行者买刀 获得java时间时,突然获得的时间不是当前 ... -
Javap
2010-01-04 10:07 3737javap是jdk自带的一个工具,可以反编译,也可以查看jav ... -
JAVA 主动调用与被动调用
2009-12-31 13:54 2293public class Test { stati ... -
Java解析XML文件的方法比较
2009-12-24 17:14 13401)DOM(JAXP Crimson解 ... -
Java protected
2009-12-16 15:17 4741(1)除了在(2)中表 ... -
ConcurrentHashMap
2009-12-15 11:40 1843关键字: concurrenthashmap ... -
SimpleDateFormat 参数 Z
2009-12-11 17:20 1683import java.text.SimpleDate ... -
HashMap 详解
2009-12-04 14:30 1413Hashmap是一种非常常用的、应用广泛的数据类型,最近研究到 ... -
JAVA自动装箱与拆箱
2009-11-24 17:15 1644自动装箱与拆箱的功能事实上是编译器来帮您的忙,编译器在编译时期 ... -
String java
2009-11-24 16:29 1439众所周知,String是由字符组成的串,在程序中使用频率很高。 ... -
Java 动态绑定
2009-09-08 11:45 1497代码: public class Par ... -
JAVA虚拟机命令行参数
2009-08-18 15:51 1983一、运行class文件执行带main方法的class文件,命令 ... -
Collections.sort
2009-07-03 16:59 1690第一种方法:容器内要排序的类必须时下Comparable接口: ... -
java 多态 例子
2009-06-30 21:58 2673其实也就是两种绑定状态:动态绑定(也称后期绑定),静态绑定(也 ... -
Java 字节 字符 字符串
2009-06-23 10:07 5999String的length()方法和数组的length属性 S ... -
Java 运算符 详解
2009-06-23 09:59 2137有些运算符在JAVA语言中存在着,但是在实际开发中我们或许很少 ... -
Java 面试题 经典
2009-06-22 16:42 1676请下载见附件~~~ -
Java 基本类型
2009-06-22 16:39 32279基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型。它 ... -
java 传值 传引用
2009-06-22 16:18 1767JAVA中的传递都是值传递吗?有没有引用传递呢? 在回答这两个 ...
相关推荐
java实现创建二叉树,并且遍历二叉树(此处使用递归方式遍历); 创建二叉树的方式有很多,此处使用线性的链表转化成二叉树,链表节点的顺序就是前序遍历的顺序,链表中的null值,代表二叉树左节点或者右节点为null...
java实现二叉树非递归前序中序后序遍历
该算法采用欧拉路径遍历二叉树,使用java语言实现的
遍历二叉树java代码
一个简单的课程设计,使用Java来实现二叉树的中序遍历
用java实现二叉树遍历 包括先序,中序,后序 后续是自己写的算法 可以运行
在Java中,实现二叉树的先序遍历可以通过递归来完成。先序遍历的顺序是:首先访问根节点,然后递归地先序遍历左子树,最后递归地先序遍历右子树。 在这段代码中,Node类定义了二叉树的节点,包含数据域和指向左右子...
建立二叉树,前后中序遍历二叉树,求二叉树的深度
java编程,二叉树的中序遍历,递归实现
java实现创建二叉树,并且遍历二叉树(此处使用非递归方式遍历); 用出栈入栈的方式遍历二叉树。
使用java语言,对数据结构中的二叉树进行遍历,包含算法逻辑。
非递归中序遍历二叉树
编写先序遍历二叉树的非递归算法程序,要求: (1)以二叉链表建立二叉树。 (2)输出遍历的结点序列。 (3)有实例验算。
java实现 二叉树的遍历 前序遍历用到递归, 中序和后序遍历用到栈, 其实还是有一定难度的
分层遍历二叉树
java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
在Java中,实现二叉树的中序遍历同样可以通过递归来完成。中序遍历的顺序是:首先递归地中序遍历左子树,然后访问根节点,最后递归地中序遍历右子树。 在这段代码中,Node类定义了二叉树的节点,BinaryTree类包含一...
在Java中,实现二叉树的后序遍历可以通过递归来完成。后序遍历的顺序是:首先递归地遍历左子树,然后递归地遍历右子树,最后访问根节点。 在这段代码中,Node类定义了二叉树的节点,BinaryTree类包含一个指向根节点...
Java版二叉树遍历非递归程序,里面写的一般,希望大家喜欢!
NULL 博文链接:https://sun810.iteye.com/blog/1261373