二叉树的java构建和三种遍历方式
2011-08-31 16:52
483 查看
树
二.代码,两个class
Java代码
public class Node {
/** 名称 */
private String name;
/** 左节点 */
private Node left;
/** 右节点 */
private Node right;
//get.set略
public Node(String n, Node l, Node r) {
name = n;
left = l;
right = r;
}
}
Java代码
public class Admin {
public static void main(String[] args) {
test();
}
private static void test() {
// 先序
xian(init());
System.out.println();
// 中序
zhong(init());
System.out.println();
// 后序
hou(init());
}
/** 初始化 */
private static Node init() {
Node nodeG = new Node("G", null, null);
Node nodeF = new Node("F", null, null);
Node nodeE = new Node("E", null, null);
Node nodeD = new Node("D", null, nodeG);
Node nodeC = new Node("C", nodeF, null);
Node nodeB = new Node("B", nodeD, nodeE);
Node nodeA = new Node("A", nodeB, nodeC);
return nodeA;
}
private static void print(Node node) {
System.out.print(node.getName() + "==>");
}
/** 先序 中->左->右 */
public static void xian(Node node) {
if (node != null) {
print(node);
xian(node.getLeft());
xian(node.getRight());
}
}
/** 中序 左->中->右 */
public static void zhong(Node node) {
if (node != null) {
zhong(node.getLeft());
print(node);
zhong(node.getRight());
}
}
/** 后序 左->右->中 */
public static void hou(Node node) {
if (node != null) {
hou(node.getLeft());
hou(node.getRight());
print(node);
}
}
}
转载地址:http://xiaojianhx.iteye.com/blog/491481
二.代码,两个class
Java代码
public class Node {
/** 名称 */
private String name;
/** 左节点 */
private Node left;
/** 右节点 */
private Node right;
//get.set略
public Node(String n, Node l, Node r) {
name = n;
left = l;
right = r;
}
}
Java代码
public class Admin {
public static void main(String[] args) {
test();
}
private static void test() {
// 先序
xian(init());
System.out.println();
// 中序
zhong(init());
System.out.println();
// 后序
hou(init());
}
/** 初始化 */
private static Node init() {
Node nodeG = new Node("G", null, null);
Node nodeF = new Node("F", null, null);
Node nodeE = new Node("E", null, null);
Node nodeD = new Node("D", null, nodeG);
Node nodeC = new Node("C", nodeF, null);
Node nodeB = new Node("B", nodeD, nodeE);
Node nodeA = new Node("A", nodeB, nodeC);
return nodeA;
}
private static void print(Node node) {
System.out.print(node.getName() + "==>");
}
/** 先序 中->左->右 */
public static void xian(Node node) {
if (node != null) {
print(node);
xian(node.getLeft());
xian(node.getRight());
}
}
/** 中序 左->中->右 */
public static void zhong(Node node) {
if (node != null) {
zhong(node.getLeft());
print(node);
zhong(node.getRight());
}
}
/** 后序 左->右->中 */
public static void hou(Node node) {
if (node != null) {
hou(node.getLeft());
hou(node.getRight());
print(node);
}
}
}
转载地址:http://xiaojianhx.iteye.com/blog/491481
相关文章推荐
- 二叉树的三种遍历方式java实现
- 二叉树的三种遍历方式java实现
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- 图解二叉树的三种遍历方式及java实现代码
- 二叉树的常见方法及三种遍历方式 Java 实现
- Java三种遍历方式打印二叉树(递归实现)
- 二叉树的三种遍历方式java实现
- Java中创建二叉树并实现三种遍历方式
- 二叉树的三种遍历方式java实现
- (学习java)二叉树的链式实现以及三种遍历方式
- java实现二叉树的三种遍历方式,并把二叉树转为双向链表,输出二叉树
- 初识树结构,简单模拟无规律的二叉树,实现二叉树的构建,计算树的深度以及三种遍历方式以及搜索删除,销毁整个树
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- 二叉树的常见方法及三种遍历方式 Java 实现
- java实现二叉树的构建以及三种遍历方法(先序遍历,中序遍历,后续遍历)
- 用Java实现二叉树的三种遍历方式