Escribir árbol binario usando JAVA
/**
* [Tree2.java] Creado el 2008-10-20 15:03:24
* Copyright (c) 2008 por iTrusChina
*/
/**
* @autor WangXuanmin
* @versión 0.10
*/
public class Tree2Bef {
private StringBuffer bef=new StringBuffer();
// Pasar recorrido en orden y recorrido posterior al pedido, volver al recorrido ordenado anterior de cadenas
public String getBef(String mid, String beh) {
//Si el nodo existe, agregue el nodo a bef y continúe consultando el subárbol izquierdo del nodo y subárbol derecho
if (root(mid, beh) != -1) {
int rootindex=root(mid, beh
);char root=mid.charAt(rootindex);
bef.append(root
System.out.println(bef.toString()); >
Cadena mleft, mright;
mleft = mid.substring(0, rootindex
mright = mid.substring(rootindex 1
getBef(mleft, beh);
getBef(mright, beh);
}
//Después de consultar todos los nodos, el valor transversal del pedido previo es devuelto
return bef.toString();
}
// Encuentre el índice del valor del índice de nodo del recorrido de orden medio de acuerdo con el recorrido posterior al pedido
private int root( String mid, String beh) {
char[] midc = mid.toCharArray()
char[] behc = beh.toCharArray; ();
for (int i = behc.length-1; i gt; -1; i--) {
for (int j = 0; j lt; midc .length; j ) {
if (behc[i] == midc[j])
return j; >}
return -1;
}
public static void main(String[] args) {
Tree2Bef tree=new Tree2Bef();
String mid="84925163A7B";
String bef="894526AB731"
System.out.println(tree.getBef(mid). , antes));
>}
}
La estructura de árbol es como se muestra en la figura:
1
|------- |
2 3
|---|---| |
8 9 A B