先序中序建立二叉树 Java实现
来源:互联网 发布:windows日志怎么看 编辑:程序博客网 时间:2024/06/16 21:54
package fenshujs;
import java.util.Scanner;
public class bishi {
private static class Node
{
public char s;
public Node left = null;
public Node right = null;
}
public static Node CreateTree(char[] pre,char[] in,int preleft,int preright,int inleft,int inright)
{
Node node = new Node();
node.s = pre[preleft];
if(preleft==preright)
return node;
int inroot = 0,flag = -1;
for(int i = inleft;i<=inright;i++)
{
if(in[i] == node.s)
{
inroot = i;
flag = 0;
break;
}
}
if(-1 == flag)
{
return null;
}
// inroot = pre.length - inroot - 1;
node.left = CreateTree(pre,in,preleft+1,preleft+(inroot-inleft),inleft,inroot-1);
node.right = CreateTree(pre,in,preleft+(inroot-inleft)+1 ,preright,inroot+1 ,preright);
return node;
}
public static void ShowTree(Node node)
{
if(node == null)
return;
System.out.print("[");
ShowTree(node.left);
System.out.print(node.s);
ShowTree(node.right);
System.out.print("]");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
char[] pre = sc.nextLine().toCharArray();
char[] in = sc.nextLine().toCharArray();
Node node = CreateTree(pre,in,0,pre.length-1,0,in.length-1);
ShowTree(node);
}
}
import java.util.Scanner;
public class bishi {
private static class Node
{
public char s;
public Node left = null;
public Node right = null;
}
public static Node CreateTree(char[] pre,char[] in,int preleft,int preright,int inleft,int inright)
{
Node node = new Node();
node.s = pre[preleft];
if(preleft==preright)
return node;
int inroot = 0,flag = -1;
for(int i = inleft;i<=inright;i++)
{
if(in[i] == node.s)
{
inroot = i;
flag = 0;
break;
}
}
if(-1 == flag)
{
return null;
}
// inroot = pre.length - inroot - 1;
node.left = CreateTree(pre,in,preleft+1,preleft+(inroot-inleft),inleft,inroot-1);
node.right = CreateTree(pre,in,preleft+(inroot-inleft)+1 ,preright,inroot+1 ,preright);
return node;
}
public static void ShowTree(Node node)
{
if(node == null)
return;
System.out.print("[");
ShowTree(node.left);
System.out.print(node.s);
ShowTree(node.right);
System.out.print("]");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
char[] pre = sc.nextLine().toCharArray();
char[] in = sc.nextLine().toCharArray();
Node node = CreateTree(pre,in,0,pre.length-1,0,in.length-1);
ShowTree(node);
}
}
阅读全文
0 0
- 先序中序建立二叉树 Java实现
- java实现平衡二叉树的建立
- java实现二叉树的建立及遍历方法
- Java 实现二叉树的建立以及前、中、后序遍利
- java实现二叉树的建立以及前序中序后序遍历
- Java实现二叉树建立以及三种遍历
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 先序中序建立二叉树
- java先序中序建立二叉树的递归算法
- 二叉树的建立及遍历实现
- 递归实现二叉搜索树建立
- 建立二叉树,实现二叉树的层序遍历
- java根据层次遍历建立二叉树
- 第一次用java实现建立二叉树。以前都是C++,居然忘记了
- 二叉树建立以及先序、中序、后序、层次遍历(JAVA 实现)
- 根据前序和中序序列,建立二叉树(java实现)
- java实现二叉树的建立,前中后序遍历,层次遍历,深度,节点个数等
- Java实现——排序二叉树的建立与遍历
- 固定管道软件渲染器
- 关于类加载的奇怪面试题
- 实现颜色渐变的几种方法
- ArcGIS Runtime for .Net Quartz开发探秘(一):ArcGIS Runtime SDK for .Net简介及开发必要准备
- Java中的六大时间类型
- 先序中序建立二叉树 Java实现
- 贝叶斯公式推导及意义
- Android自定义主题样式详解(结合自定义title栏讲解)
- vim的ex模式用法
- calico网络原理及与flannel对比
- 创建fragment实例
- 跟我学NodeJS(二)
- EventBus 了解及应用
- HTML标题前的图标logo