Plantillas de algoritmos recursivos y no recursivos para atravesar el frente, el medio y la parte posterior del árbol binario

1. Estructura de datos del nodo

 Nodo de clase pública { 

    valor int público  ;
    Nodo público a la izquierda;
    derecho de nodo público ; Nodo público ( datos int ) {
         this .value = value; 
    } 
}

    

2. Recursion

public  class Recur { 

    public  void preOrderRecur (Node head) {
         if (head == null ) {
             return ; 
        } 

        System.out.println (head.value + "" ); 
        preOrderRecur (head.left); 
        preOrderRecur (head.right); 
    } 

    public  void inOrderRecur (Node head) {
         if (head == null ) {
             return ; 
        } 

        inOrderRecur (head.left); 
        System.out.println (head.value + "" );
        inOrderRecur (head.right); 
    } 

    public  void posOrderRecur (Node head) {
         if (head == null ) {
             return ; 
        } 

        posOrderRecur (head.left); 
        posOrderRecur (head.right); 
        System.out.println (head.value + "" ); 
    } 
}

3. No recursivo

import java.util.Stack; 

public  class UnRecur { 

    public  void preOrderUnRecur (Node head) { 
        System.out.println ( "pre-order:" );
        if (head! = null ) { 
            Stack <Node> stack = new Stack <> (); 
            stack.add (cabeza); 
            while (! stack.isEmpty ()) { 
                head = stack.pop (); 
                System.out.println (head.value + "" );
                if (head.right! = null ) {
                    stack.push (head.right); 
                } 
                if (head.left! = null ) { 
                    stack.push (head.left); 
                } 
            } 
        } 
    } 

    public  void inOrderUnRecur (Cabezal de nodo) { 
        System.out.println ( "en orden:" );
        if (head! = null ) { 
            Stack <Node> stack = new Stack <> ();
            while (! stack.isEmpty () || head! = null ) {
                 if (head! = null) { 
                    stack.push (cabeza); 
                    cabeza = cabeza izquierda; 
                } else { 
                    head = stack.pop (); 
                    System.out.println (head.value + "" ); 
                    cabeza = cabeza.derecho; 
                } 
            } 
        } 
    } 

    public  void posOrderUnRecur (Cabezal de nodo) { 
        System.out.println ( "pos-order:" );
        if (head! = null ) { 
            Pila<Nodo> pila = nueva Pila <> (); 
            stack.push (cabeza); 
            Nodo c; 
            while (! stack.isEmpty ()) { 
                c = stack.peek ();
                if (c.left! = nulo && head! = c.left && head! = c.right) { 
                    stack.push (c.left); 
                } else  if (c.right! = null && head! = c.right) { 
                    stack.push (c.right); 
                } else { 
                    System.out.println (stack.pop (). value + ""); 
                    cabeza = c; 
                } 
            } 

        } 
    } 
}

 

Supongo que te gusta

Origin www.cnblogs.com/zhihaospace/p/12751275.html
Recomendado
Clasificación