java实现二叉树的中序遍历和后续遍历
2015-11-28 14:58
435 查看
public class NodeTree {
String value;
NodeTree left;
NodeTree right;
public NodeTree(String value, NodeTree left, NodeTree right) {
this.value = value;
this.left = left;
this.right = right;
}
}
public class MidTreeSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
NodeTree root = creatTree();
LookMid(root);
NodeTree aim=LookElement(root, "C");
if(aim.left!=null)
{
System.out.println(aim.left.value+"===");
}
}
//创建二叉树
/*
*
* A
*
* B C
*
* D G L F
*
* K M
*/
public static NodeTree creatTree() {
NodeTree root = null;
NodeTree K = new NodeTree("K", null, null);
NodeTree L = new NodeTree("L", null, null);
NodeTree M = new NodeTree("M", null, null);
NodeTree G = new NodeTree("G", null, M);
NodeTree D = new NodeTree("D", null, K);
NodeTree F = new NodeTree("F", null, null);
NodeTree B = new NodeTree("B", D, G);
NodeTree C = new NodeTree("C", L, F);
NodeTree A = new NodeTree("A", B, C);
root = A;
return root;
}
//中序遍历
public static void LookMid(NodeTree root) {
if (root != null) {
NodeTree nodeLeft=root.left;
LookMid(nodeLeft);
System.out.print(root.value+" ");
NodeTree nodeRight=root.right;
LookMid(nodeRight);
}
}
//后序遍历
public static void LookRear(NodeTree root) {
if(root!=null)
{
NodeTree nodeLeft=root.left;
LookRear(nodeLeft);
NodeTree nodeRight=root.right;
LookRear(nodeRight);
System.out.print(root.value+" ");
}
}
//查找
public static NodeTree LookElement(NodeTree root, String e) {
NodeTree result=null;
if (root != null) {
if (e.equals(root.value)) {
System.out.println("找到了");
result=root;
return result;
} else {
NodeTree nodeLeft = root.left;
result=LookElement(nodeLeft, e);
NodeTree nodeRight = root.right;
result=LookElement(nodeRight, e);
}
}
return result;
}
}
String value;
NodeTree left;
NodeTree right;
public NodeTree(String value, NodeTree left, NodeTree right) {
this.value = value;
this.left = left;
this.right = right;
}
}
public class MidTreeSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
NodeTree root = creatTree();
LookMid(root);
NodeTree aim=LookElement(root, "C");
if(aim.left!=null)
{
System.out.println(aim.left.value+"===");
}
}
//创建二叉树
/*
*
* A
*
* B C
*
* D G L F
*
* K M
*/
public static NodeTree creatTree() {
NodeTree root = null;
NodeTree K = new NodeTree("K", null, null);
NodeTree L = new NodeTree("L", null, null);
NodeTree M = new NodeTree("M", null, null);
NodeTree G = new NodeTree("G", null, M);
NodeTree D = new NodeTree("D", null, K);
NodeTree F = new NodeTree("F", null, null);
NodeTree B = new NodeTree("B", D, G);
NodeTree C = new NodeTree("C", L, F);
NodeTree A = new NodeTree("A", B, C);
root = A;
return root;
}
//中序遍历
public static void LookMid(NodeTree root) {
if (root != null) {
NodeTree nodeLeft=root.left;
LookMid(nodeLeft);
System.out.print(root.value+" ");
NodeTree nodeRight=root.right;
LookMid(nodeRight);
}
}
//后序遍历
public static void LookRear(NodeTree root) {
if(root!=null)
{
NodeTree nodeLeft=root.left;
LookRear(nodeLeft);
NodeTree nodeRight=root.right;
LookRear(nodeRight);
System.out.print(root.value+" ");
}
}
//查找
public static NodeTree LookElement(NodeTree root, String e) {
NodeTree result=null;
if (root != null) {
if (e.equals(root.value)) {
System.out.println("找到了");
result=root;
return result;
} else {
NodeTree nodeLeft = root.left;
result=LookElement(nodeLeft, e);
NodeTree nodeRight = root.right;
result=LookElement(nodeRight, e);
}
}
return result;
}
}
相关文章推荐
- spring properties 资源文件 方式一
- Java按照某一时区格式化时间
- 对RmiJdbc的二次开发
- Java模拟HTTP的Get和Post请求
- Myeclipse2014黑色主题风格设置
- Java 字符串全面解析
- JAVA通过JDBC连接MySQL数据库提示连接拒绝问题处理
- VS,eclipse联调jni代码
- MyEclipse 使用快捷键
- Eclipse工具使用问题汇总
- Java锁(一)之内存模型
- java实现文件单词频率统计 topN top K
- SpringMVC中HandlerMethod的请求参数解析过程
- 【Java小项目】电子音效器
- java实现网上下载文件到本地
- Struts2类型转换器
- Struts2类型转换器
- Java基础知识总结(我常犯的错误)
- JAVA多线程--信号量(Semaphore)
- JAVA中socket编程println()和 write()方法